Spaces:
Running
Running
first*1.0 / second
Browse files
Week 1: Types, condition clauses and loops/5. Calculate sum, multiplication and division
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
In the program below, the variables first and second are initialized with random initial values.
|
| 3 |
+
The variables are of integer type.
|
| 4 |
+
|
| 5 |
+
Your task is to print the sum, the product and the partial sum of the numbers (i.e. first divided by second).
|
| 6 |
+
|
| 7 |
+
Print each number on its own line.
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
import java.util.Random;
|
| 12 |
+
|
| 13 |
+
public class Test{
|
| 14 |
+
public static void main(String[] args){
|
| 15 |
+
final Random r = new Random();
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
int first = 5;
|
| 20 |
+
int second = 2;
|
| 21 |
+
|
| 22 |
+
System.out.println("Testing with values:");
|
| 23 |
+
System.out.println("first == " + first);
|
| 24 |
+
System.out.println("second == " + second);
|
| 25 |
+
|
| 26 |
+
int sum = first + second;
|
| 27 |
+
int prod = first * second;
|
| 28 |
+
double partialsum = first*1.0 / second;
|
| 29 |
+
|
| 30 |
+
// System.out.println("sum == " + sum);
|
| 31 |
+
// System.out.println("prod == " + prod);
|
| 32 |
+
// System.out.println("partialsum == " + partialsum);
|
| 33 |
+
System.out.println(sum);
|
| 34 |
+
System.out.println(prod);
|
| 35 |
+
System.out.println(partialsum);
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
System.out.println("");
|
| 39 |
+
|
| 40 |
+
}
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
"""actual, expected output
|
| 47 |
+
Testing with values:
|
| 48 |
+
first == 5
|
| 49 |
+
second == 2
|
| 50 |
+
7
|
| 51 |
+
10
|
| 52 |
+
2.5
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
Testing with values:
|
| 57 |
+
first == 10
|
| 58 |
+
second == 3
|
| 59 |
+
13
|
| 60 |
+
30
|
| 61 |
+
3.3333333333333335
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
Testing with values:
|
| 66 |
+
first == 12
|
| 67 |
+
second == 5
|
| 68 |
+
17
|
| 69 |
+
60
|
| 70 |
+
2.4
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
"""
|