Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribedI 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();

}

}

E7.2 - Write array methods that carry out the following tasks for an array of integers by completing the ArrayMethods and ArrayMethodsTester classes posted on Canvas. For each method, provide a test (or tests) within the main method. (Follow the procedure outlined in the starter code, part a has been done for you). Do not use the starter code in the book for E7.2, copy it from UTC Learn. I have made a few changes to the code. a) (already completed for you) Swap the first and last elements in the array. b) Shift all elements by one to the right and move the last element into the first position. For example, 1491625 would become 2514916. c) Replace all even elements with 0. d) Replace each element except the first and last by the larger of its two neighbors. For the example in part (b), your output would be 19162525. e) Remove the middle element if the array length is odd, or the middle two elements if the length is even. (You will need to test two different arrays here to make sure your method works on both even and odd length arrays). f) Move all even elements to the front, otherwise preserving the order of the elements. For the example in part (b) your output should be 4161925 g) Return the second-largest element in the array. (Your method should return this value, and your main method should print the value out). h) Return true if the array is currently sorted in increasing order. (You can use Arrays.sort() to sort an array before you call your method for testing purposes. Make sure to test both a sorted array and an unsorted array as input). Your main method should print whether the array was in order or not. i) Return true if the array contains two adjacent duplicate elements. j) Return true if the array contains duplicate elements (which need not be adjacent)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

Students also viewed these Databases questions