steef_br

Untitled

Apr 11th, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. package MethodsExercises;
  2.  
  3. import org.jetbrains.annotations.NotNull;
  4.  
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. import java.util.IllegalFormatCodePointException;
  8. import java.util.Scanner;
  9.  
  10.  
  11. public class ArrayManipulator {
  12. public static void main(String[] args) {
  13. Scanner scanner = new Scanner(System.in);
  14.  
  15. int[] intArray = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
  16.  
  17. String command = scanner.nextLine();
  18. while (!command.equals("end")) {
  19. String[] input = command.split(" ");
  20.  
  21. switch (input[0]) {
  22.  
  23. case "exchange":
  24. int swapIndex = Integer.parseInt(input[1]);
  25.  
  26. if (swapIndex < 0 || swapIndex >= intArray.length) {
  27. System.out.println("invalid index");
  28. } else {
  29. intArray = getSwappedArray(intArray, swapIndex);
  30. }
  31. break;
  32. case "first":
  33. case "last":
  34. int element = Integer.parseInt(input[1]);
  35. switch (input[2]) {
  36. case "odd":
  37. case "even":
  38. }
  39. break;
  40. case "min":
  41. case "mix":
  42. switch (input[1]) {
  43. case "odd":
  44. case "even":
  45. printMaxOddOrEven(intArray, input[0], input[1]);
  46. }
  47. break;
  48.  
  49.  
  50. }
  51.  
  52.  
  53. command = scanner.nextLine();
  54. }
  55. System.out.println("That's it!");
  56. }
  57.  
  58. private static void printMaxOddOrEven(int[] intArray, String s1, String s) {
  59.  
  60. switch (s) {
  61. case "odd":
  62. case "even":
  63.  
  64. int maxOdd = Integer.MIN_VALUE;
  65. int maxEven = Integer.MIN_VALUE;
  66. int minOdd = Integer.MAX_VALUE;
  67. int minEven = Integer.MAX_VALUE;
  68. boolean maxOddFound = false;
  69. boolean maxEvenFound = false;
  70. boolean minOddFound = false;
  71. boolean minEvenFound = false;
  72.  
  73. for (int i = 0; i < intArray.length; i++) {
  74. int current = intArray[i];
  75. if (current % 2 != 0 ) {
  76. if (current > maxOdd) {
  77. maxOddFound = true;
  78. maxOdd = current;
  79. }
  80. if (current < minOdd){
  81. minOddFound = true;
  82. minOdd = current;
  83. }
  84. } else {
  85. if (current > maxEven) {
  86. maxEvenFound = true;
  87. maxEven = current;
  88. }
  89. if (current < minEven){
  90. minEvenFound = true;
  91. minEven = current;
  92. }
  93. }
  94. }
  95.  
  96.  
  97.  
  98.  
  99. break;
  100. }
  101. }
  102.  
  103.  
  104. private static int[] getSwappedArray(int @NotNull [] intArray, int swapIndex) {
  105.  
  106. if (swapIndex == intArray.length - 1) {
  107. // validation if index to swap is last index in array...
  108. return intArray;
  109. } else {
  110.  
  111. int[] tempArr = new int[intArray.length];
  112. int[] subArr1 = new int[swapIndex + 1];
  113. int[] subArr2 = new int[intArray.length - (swapIndex + 1)];
  114.  
  115. // fill up subArr1..
  116. if (swapIndex + 1 >= 0) System.arraycopy(intArray, 0, subArr1, 0, swapIndex + 1);
  117. // fill up subArr2.. using temp variable to start filling for 1st index in subArr2..
  118. int tempIndex = 0;
  119. for (int i = swapIndex + 1; i < intArray.length; i++) {
  120. subArr2[tempIndex] = intArray[i];
  121. if (tempIndex < subArr2.length - 1) {
  122. tempIndex++;
  123. }
  124. }
  125. // fill up newArr with subArr2 + subArr1..
  126. // using temp variable to start filling newArr for 1st index
  127. int tempIndex1 = 0;
  128. for (int i = 0; i < tempArr.length; i++) {
  129. if (i <= subArr2.length - 1) {
  130. tempArr[i] = subArr2[i];
  131. } else {
  132. tempArr[i] = subArr1[tempIndex1];
  133. tempIndex1++;
  134. }
  135. }
  136.  
  137. return tempArr;
  138. }
  139.  
  140. }
  141.  
  142. }
  143.  
  144.  
Add Comment
Please, Sign In to add comment