Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in Java that prompts the user for a sequence of integers and sto

ID: 645479 • Letter: W

Question

Write a program in Java that prompts the user for a sequence of integers and stores them in an array. Then write the following functions and have your program call them with the array as parameter (you must use the original array in each case, so you have to make copies). Each of the following must be a separate function:

a. replace all even elements with 0

b. replace each element except the first and last by the largest of its two neighbors

c. move all even elements to the front, otherwise preserving the order of the elements

Explanation / Answer

/*Write array methods that carry out the following tasks for an array of integers. For each method, provide a test program. a. 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, 1 4 9 16 25 would be transformed into 25 1 4 9 16. c. Replace all even elements with 0. d. Replace each element except the first and last by the larger of its two neighbors. e. Remove the middle element if the array length is odd, or the middle two elements if the length is even. f. Move all even elements to the front, otherwise preserving the order of the elements. g. Return the second-largest element in the array. h. Return true if the array is currently sorted in increasing order. 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).*/ public class P6_02 { // A public static void swap(int[] seq, int a_index, int b_index) { int temp = seq[a_index]; seq[a_index] = seq[b_index]; seq[b_index] = temp; } // B public static void shift(int[] seq) { int last = seq[seq.length - 1]; System.arraycopy(seq, 0, seq, 1, seq.length - 1); seq[0] = last; } // C public static void replace(int[] seq, int replacement) { for (int i = 0; i seq[i + 1]) { seq[i] = seq[i - 1]; } } return seq; } // E public static void removeMiddleElement(int[] seq) { int middle = seq.length / 2; if (seq.length % 2 == 0) { for (int i = middle; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote