Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MethodsExercises;
- import org.jetbrains.annotations.NotNull;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.IllegalFormatCodePointException;
- import java.util.Scanner;
- public class ArrayManipulator {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int[] intArray = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
- String command = scanner.nextLine();
- while (!command.equals("end")) {
- String[] input = command.split(" ");
- switch (input[0]) {
- case "exchange":
- int swapIndex = Integer.parseInt(input[1]);
- if (swapIndex < 0 || swapIndex >= intArray.length) {
- System.out.println("invalid index");
- } else {
- intArray = getSwappedArray(intArray, swapIndex);
- }
- break;
- case "first":
- case "last":
- int element = Integer.parseInt(input[1]);
- switch (input[2]) {
- case "odd":
- case "even":
- }
- break;
- case "min":
- case "mix":
- switch (input[1]) {
- case "odd":
- case "even":
- printMaxOddOrEven(intArray, input[0], input[1]);
- }
- break;
- }
- command = scanner.nextLine();
- }
- System.out.println("That's it!");
- }
- private static void printMaxOddOrEven(int[] intArray, String s1, String s) {
- switch (s) {
- case "odd":
- case "even":
- int maxOdd = Integer.MIN_VALUE;
- int maxEven = Integer.MIN_VALUE;
- int minOdd = Integer.MAX_VALUE;
- int minEven = Integer.MAX_VALUE;
- boolean maxOddFound = false;
- boolean maxEvenFound = false;
- boolean minOddFound = false;
- boolean minEvenFound = false;
- for (int i = 0; i < intArray.length; i++) {
- int current = intArray[i];
- if (current % 2 != 0 ) {
- if (current > maxOdd) {
- maxOddFound = true;
- maxOdd = current;
- }
- if (current < minOdd){
- minOddFound = true;
- minOdd = current;
- }
- } else {
- if (current > maxEven) {
- maxEvenFound = true;
- maxEven = current;
- }
- if (current < minEven){
- minEvenFound = true;
- minEven = current;
- }
- }
- }
- break;
- }
- }
- private static int[] getSwappedArray(int @NotNull [] intArray, int swapIndex) {
- if (swapIndex == intArray.length - 1) {
- // validation if index to swap is last index in array...
- return intArray;
- } else {
- int[] tempArr = new int[intArray.length];
- int[] subArr1 = new int[swapIndex + 1];
- int[] subArr2 = new int[intArray.length - (swapIndex + 1)];
- // fill up subArr1..
- if (swapIndex + 1 >= 0) System.arraycopy(intArray, 0, subArr1, 0, swapIndex + 1);
- // fill up subArr2.. using temp variable to start filling for 1st index in subArr2..
- int tempIndex = 0;
- for (int i = swapIndex + 1; i < intArray.length; i++) {
- subArr2[tempIndex] = intArray[i];
- if (tempIndex < subArr2.length - 1) {
- tempIndex++;
- }
- }
- // fill up newArr with subArr2 + subArr1..
- // using temp variable to start filling newArr for 1st index
- int tempIndex1 = 0;
- for (int i = 0; i < tempArr.length; i++) {
- if (i <= subArr2.length - 1) {
- tempArr[i] = subArr2[i];
- } else {
- tempArr[i] = subArr1[tempIndex1];
- tempIndex1++;
- }
- }
- return tempArr;
- }
- }
- }
Add Comment
Please, Sign In to add comment