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