Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. public static int[][] splitAtFive(int[] array){
  2. int smallerLength = 0;
  3. int lagerLength = 0;
  4. int smallerIndex = 0;
  5. int largerIndex = 1;
  6. int SMALLER = 0;
  7. int LARGER = 1;
  8. for(int i = 0; i <array.length; i++) {
  9. if (array[i] < 5) {
  10. smallerLength++;
  11. } else if (array[i] >= 5) {
  12. lagerLength++;
  13. }
  14. }
  15. int[][] results = new int[2][];
  16. results[SMALLER] = new int[smallerLength];
  17. results[LARGER] = new int [lagerLength];
  18. for (int i = 0; i <array.length; i++){
  19. if(array[i] < 5){
  20. //Access by normal array access, so double access for a two dimensional
  21. results[SMALLER][smallerIndex] = array[i];
  22. smallerIndex++;
  23.  
  24. }else if(array[i] >= 5){
  25. results[LARGER][largerIndex] = array[i];
  26. largerIndex++;
  27. }
  28. } return results;
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. ---------test--------------
  37. public void testSplitAt5() {
  38. int[][] output1 = new int[][] { {0, 1, 2, 3, 4}, {5, 6, 7} };
  39. int[][] output2 = new int[][] { {-3, -17}, {} };
  40. int[][] output3 = new int[][] { {}, {55, 2323, 102, 7} };
  41. int[][] output4 = new int[][] { {2, 4, 3, 1, 3}, {5, 7, 6, 9, 10, 13, 56, 43, 17, 89, 24, 37, 12, 101, 112} };
  42.  
  43. String failMessage = "Expected method to return an array with values less than 5";
  44. assertArrayEquals(failMessage, output1, App.splitAtFive(intTest1));
  45. assertArrayEquals(failMessage, output2, App.splitAtFive(intTest2));
  46. assertArrayEquals(failMessage, output3, App.splitAtFive(intTest3));
  47. assertArrayEquals(failMessage, output4, App.splitAtFive(intTest4));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement