Question
I have this assignment for class, I have looked through my textbook, Chegg and other websites but I still can't figure out why the program
I have this assignment for class, I have looked through my textbook, Chegg and other websites but I still can't figure out why the program doesn't run. It says there is an error at Part 1 and part J. If anyone could offer some suggestions or point me in the right direction that would be awesome!
import java.util.Arrays; import java.util.Random;
public class ArrayMethods { public static void swapFirstAndLast(int[] values) { int temp = values[0]; values[0] = values[values.length-1]; values[values.length-1] = temp; } //part b, fill in this method public static void shiftRight(int[] values) { int temp = values[values.length-1]; for(int i = values.length-1; i > 0; i--) values[i] = values[i-1]; values[0] = temp; } //part c, set all even elements to 0. public static void setEvensToZero(int[] values) { for(int i = 0; i values[i+1]) values[i] = values[i-1]; else values[i] = values[i+1]; } //part e, remove middle el if odd length, else remove middle two els. public static int[] removeMiddle(int[] values) { int result[];
int len = values.length;
if(len % 2 == 0) {
result = new int[len-2];
int k=0;
for(int i=0; i if(i != len/2 && i != len/2 - 1) { result[k++] = values[i]; } } } else { result = new int[len-1]; int k=0; for(int i=0; i if(i != len/2 ) { result[k++] = values[i]; } } } return result; } public static void moveEvensToFront(int[] values) { int temp=0; int a=0; for(int i=0; i if(values[i] % 2 == 0){ for (int j=i; j>a; j--){ temp = values[j-1]; values[j-1] = values[j]; values[j] = temp; } a ++; } } } //part g - return second largest element in array public static int ret2ndLargest(int[] values) { int i, first, second; int len = values.length; if (len { System.out.print(" Invalid Input "); return -1; } first = second = Integer.MIN_VALUE; for (i = 0; i { if (values[i] > first) { second = first; first = values[i]; } else if (values[i] > second && values[i] != first) second = values[i]; } return second; } //part H - returns true if array is sorted in increasing order public static boolean isSorted(int[] values) { for(int i=1; i import java.util.Arrays; import java.util.Random; public class ArrayMethodsTester { public static void printArray(int[] values) { System.out.println(Arrays.toString(values)); } public static void main(String[] args) { //In your main method you should test your array methods //Create an array of size 10 //****** HERE int[] a = new int[10]; //array of size 1 Random random = new Random(); for(int i=0; i int rand = random.nextInt(50); a[i] = rand; } int[] b = new int[9]; //array of size 1 //**** Fill the array with random values (use a loop, and a //Random object) for(int i=0; i int rand = random.nextInt(50); b[i] = rand; } System.out.println("Initial Array:"); /ote the usage of the "toString()" method here to print the array System.out.println(Arrays.toString(a)); //Could replace the previous line with this: //printArray(testValues); //blank line System.out.println(); //Test methods below this line. //Test of swapFirstAndLast() System.out.println("Before call to swapFirstAndLast():"); printArray(a); //swap first and last element //this method modifies the array referenced by "testValues" ArrayMethods.swapFirstAndLast(a); System.out.println("After call to swapFirstAndLast()"); printArray(a); //printing the same array but it has changed System.out.println(); //Test of shiftRight() System.out.println("Before call to shiftRight():"); printArray(a); //this method modifies the array referenced by "testValues" ArrayMethods.shiftRight(a); System.out.println("After call to shiftRight()"); printArray(a); System.out.println(); System.out.println("Before call to removeMiddle() for array of even length:"); printArray(a); a = ArrayMethods.removeMiddle(a); System.out.println("After call to removeMiddle()"); printArray(a); System.out.println(); System.out.println("Before call to removeMiddle() for array of odd length:"); printArray(b); b = ArrayMethods.removeMiddle(b); System.out.println("After call to removeMiddle()"); printArray(b); System.out.println(); System.out.println("Before call to ret2ndLargest():"); printArray(a); int secLargest = ArrayMethods.ret2ndLargest(a); System.out.println("2nd Largest element = " + secLargest); System.out.println(); System.out.println("Before call to isSorted():"); printArray(a); boolean sorted = ArrayMethods.isSorted(a); System.out.println("Is a sorted ? : " + sorted); System.out.println(); Arrays.sort(a); System.out.println("Before call to isSorted():"); printArray(a); sorted = ArrayMethods.isSorted(a); System.out.println("Is a sorted ? : " + sorted); System.out.println(); System.out.println("Before call to hasAdjDuplicates():"); printArray(a); boolean hasAdjDuplicates = ArrayMethods.hasAdjDuplicates(a); System.out.println("Contains adjacent duplicate? : " + adjDup); System.out.println(); System.out.println("Before call to hasDuplicates():"); printArray(a); boolean dup = ArrayMethods.hasDuplicates(a); System.out.println("Contains duplicate? : " + dup); System.out.println(); System.out.println("Before call to setEvensToZero():"); printArray(a); ArrayMethods.setEvensToZero(a); System.out.println("After call to setEvensToZero()"); printArray(a); System.out.println(); System.out.println("Before call to largerOfAdjacents():"); printArray(a); ArrayMethods.largerOfAdjacents(a); System.out.println("After call to largerOfAdjacents()"); printArray(a); System.out.println(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started