Spaces:
Running
Running
| Write the method | |
| printMiddle | |
| ...which takes three integers as parameters. | |
| The method prints the middle of the numbers in order of magnitude. | |
| For example, if the parameters of the method were 1, 3 and 2, the method would print 2. | |
| import java.util.Random; | |
| public class Test{ | |
| public static void main(String[] args){ | |
| final Random r = new Random(); | |
| Object[][] p = {{1,4,3}, {121,145,94}, {20,30,40}, {9,7,8}, {99,77,88}}; | |
| for (Object[] pa : p) { | |
| System.out.print("Testing with parameters "); | |
| System.out.println(pa[0] + ", " + pa[1] + ", " + pa[2]); | |
| printMiddle((Integer) pa[0], (Integer) pa[1], (Integer) pa[2]); | |
| System.out.println(""); | |
| } | |
| } | |
| //CONTINUE HERE | |
| // ASSUME int1/2/3 are different | |
| //6 different combinations | |
| // 1-2-3 | |
| // 1-3-2 | |
| // 2-1-3 | |
| // 2-3-1 | |
| // 3-1-2 | |
| // 3-2-1 | |
| // 121, 145, 94 | |
| public static void printMiddle(int int1, int int2, int int3) { | |
| int middle = 0; | |
| // 2-1-3, 3-1-2 | |
| if ((int1 > int2 && int1 < int3) || (int3 < int1 && int1 < int2)) { | |
| middle = int1; | |
| } | |
| // 1-2-3, 3-2-1 | |
| else if ((int2 > int1 && int2 < int3) || (int2 > int3 && int2 < int1)) { | |
| middle = int2; | |
| } | |
| // 2-3-1, 1-3-2 | |
| else { | |
| middle = int3; | |
| } | |
| System.out.println(middle); | |
| } | |
| } | |
| Testing with parameters 1, 4, 3 | |
| 3 | |
| Testing with parameters 121, 145, 94 | |
| 121 | |
| Testing with parameters 20, 30, 40 | |
| 30 | |
| Testing with parameters 9, 7, 8 | |
| 8 | |
| Testing with parameters 99, 77, 88 | |
| 88 | |