KaiquanMah commited on
Commit
710651d
·
verified ·
1 Parent(s): e11bd8c

Create 4. Print the middle number

Browse files
Week 2: Methods, strings and lists/4. Print the middle number ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Write the method
2
+
3
+ printMiddle
4
+
5
+ ...which takes three integers as parameters.
6
+ The method prints the middle of the numbers in order of magnitude.
7
+ For example, if the parameters of the method were 1, 3 and 2, the method would print 2.
8
+
9
+
10
+
11
+
12
+ import java.util.Random;
13
+
14
+ public class Test{
15
+ public static void main(String[] args){
16
+ final Random r = new Random();
17
+
18
+
19
+ Object[][] p = {{1,4,3}, {121,145,94}, {20,30,40}, {9,7,8}, {99,77,88}};
20
+ for (Object[] pa : p) {
21
+ System.out.print("Testing with parameters ");
22
+ System.out.println(pa[0] + ", " + pa[1] + ", " + pa[2]);
23
+ printMiddle((Integer) pa[0], (Integer) pa[1], (Integer) pa[2]);
24
+ System.out.println("");
25
+ }
26
+ }
27
+
28
+ //CONTINUE HERE
29
+ // ASSUME int1/2/3 are different
30
+ //6 different combinations
31
+ // 1-2-3
32
+ // 1-3-2
33
+ // 2-1-3
34
+ // 2-3-1
35
+ // 3-1-2
36
+ // 3-2-1
37
+ // 121, 145, 94
38
+ public static void printMiddle(int int1, int int2, int int3) {
39
+ int middle = 0;
40
+
41
+ // 2-1-3, 3-1-2
42
+ if ((int1 > int2 && int1 < int3) || (int3 < int1 && int1 < int2)) {
43
+ middle = int1;
44
+ }
45
+ // 1-2-3, 3-2-1
46
+ else if ((int2 > int1 && int2 < int3) || (int2 > int3 && int2 < int1)) {
47
+ middle = int2;
48
+ }
49
+ // 2-3-1, 1-3-2
50
+ else {
51
+ middle = int3;
52
+ }
53
+
54
+ System.out.println(middle);
55
+ }
56
+
57
+
58
+ }
59
+
60
+
61
+
62
+
63
+
64
+
65
+ Testing with parameters 1, 4, 3
66
+ 3
67
+
68
+ Testing with parameters 121, 145, 94
69
+ 121
70
+
71
+ Testing with parameters 20, 30, 40
72
+ 30
73
+
74
+ Testing with parameters 9, 7, 8
75
+ 8
76
+
77
+ Testing with parameters 99, 77, 88
78
+ 88
79
+
80
+