Spaces:
Running
Running
Create 1b. Calculator
Browse files
Week 3: Objects, files and exceptions/1b. Calculator
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Write the method
|
| 2 |
+
|
| 3 |
+
int calculate(int num1, int num2, String operator)
|
| 4 |
+
|
| 5 |
+
...which takes as parameters two integers and a string.
|
| 6 |
+
The string has 4 possible values: "+", "-", "*" or "/"
|
| 7 |
+
The method calculates and returns the operator-defined arithmetic operation on the numbers.
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
Examples on method calls:
|
| 11 |
+
public static void main(String[] parameters){
|
| 12 |
+
System.out.println(calculate(4, 5, "+"));
|
| 13 |
+
System.out.println(calculate(8, 2, "-"));
|
| 14 |
+
System.out.println(calculate(3, 4, "*"));
|
| 15 |
+
System.out.println(calculate(10, 2, "/"));
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
Program outputs:
|
| 20 |
+
9
|
| 21 |
+
6
|
| 22 |
+
12
|
| 23 |
+
5
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
import java.util.Random;
|
| 32 |
+
|
| 33 |
+
public class Test{
|
| 34 |
+
public static void main(String[] args){
|
| 35 |
+
final Random r = new Random();
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
Object[][] p = {{1,4,"+"}, {121,145,"-"}, {5,8,"*"}, {9,3,"/"}, {99,77,"-"},
|
| 39 |
+
{2,4,"*"}, {20,5,"/"}, {1,2,"-"}, {9,3,"*"}};
|
| 40 |
+
for (Object[] pa : p) {
|
| 41 |
+
System.out.print("Testing with parameters ");
|
| 42 |
+
System.out.println(pa[0] + ", " + pa[1] + ", " + pa[2]);
|
| 43 |
+
int tulos = calculate((Integer) pa[0], (Integer) pa[1], (String) pa[2]);
|
| 44 |
+
System.out.println("Result: " + tulos);
|
| 45 |
+
System.out.println("");
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
public static int calculate(int num1, int num2, String operator) {
|
| 51 |
+
int result;
|
| 52 |
+
if (operator.equals("+")) {
|
| 53 |
+
result = num1 + num2;
|
| 54 |
+
}
|
| 55 |
+
else if (operator.equals("-")) {
|
| 56 |
+
result = num1 - num2;
|
| 57 |
+
}
|
| 58 |
+
else if (operator.equals("*")) {
|
| 59 |
+
result = num1 * num2;
|
| 60 |
+
}
|
| 61 |
+
else if (operator.equals("/")) {
|
| 62 |
+
result = num1 / num2;
|
| 63 |
+
}
|
| 64 |
+
else {
|
| 65 |
+
result = 0;
|
| 66 |
+
}
|
| 67 |
+
return result;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
Testing with parameters 1, 4, +
|
| 84 |
+
Result: 5
|
| 85 |
+
|
| 86 |
+
Testing with parameters 121, 145, -
|
| 87 |
+
Result: -24
|
| 88 |
+
|
| 89 |
+
Testing with parameters 5, 8, *
|
| 90 |
+
Result: 40
|
| 91 |
+
|
| 92 |
+
Testing with parameters 9, 3, /
|
| 93 |
+
Result: 3
|
| 94 |
+
|
| 95 |
+
Testing with parameters 99, 77, -
|
| 96 |
+
Result: 22
|
| 97 |
+
|
| 98 |
+
Testing with parameters 2, 4, *
|
| 99 |
+
Result: 8
|
| 100 |
+
|
| 101 |
+
Testing with parameters 20, 5, /
|
| 102 |
+
Result: 4
|
| 103 |
+
|
| 104 |
+
Testing with parameters 1, 2, -
|
| 105 |
+
Result: -1
|
| 106 |
+
|
| 107 |
+
Testing with parameters 9, 3, *
|
| 108 |
+
Result: 27
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|