Question: Coding Language is Java. The provided Array.java file is missing two methods, findValueIndex and consolidate. Complete each method so that it functions according to the

Coding Language is Java. The provided Array.java file is missing two methods, findValueIndex and consolidate. Complete each method so that it functions according to the commented description associated with each method. findIndexValue returns the index of the array that contains the integer with the specified value. consolidate consolidates the array by moving integers so that the non-zero integers are in adjacent arr index locations, starting at index 0, with no 0 value between any two non-zero integers. A temporary variable is recommended for consolidate to store the value that will be replaced.

public class Array { private int[] arr; /** * Constructor that takes the number of potential integers * * @param initInts - the number of potential integers in the array */ public Array(int initInts) { arr = new int[initInts]; } /** * Returns the index of the array that contains the integer with the * specified value.* Precondition: No two non-zero integers in the array have the same * value. * * @param value the value of the integer to find * @return the index of the array containing the integer with the specified * value; -1 if no integer with the specified value is in the q. */ public int findValueIndex(int value) { //Unit 6.2 } /** * Consolidates the array by moving integers so that the non-zero integers * are in adjacent array index locations, starting at index 0, with no 0 value between any two * non-zero integers. Postcondition: The order of the integers is the same * as before the consolidation. */ public void consolidate() { //Unit 6.4 } @Override public String toString() { String result = ""; for (int i = 0; i < arr.length; i++) { result = result + "Array index " + i + " contains the value " + arr[i] + " "; } return result; } public static void main(String[] args) { Array q = new Array(7); q.arr[0] = 2; q.arr[2] = -7; q.arr[3] = 11; q.arr[5] = -2; q.arr[6] = 42; // print out what is in the q System.out.println(q); // test System.out.println("Index of 2 should be 0 and is " + q.findValueIndex(2)); System.out.println("Index of 11 should be 3 and is " + q.findValueIndex(11)); System.out.println("Index of 15 should be -1 and is " + q.findValueIndex(15)); System.out.println(" Before consolidate"); System.out.println(q); q.consolidate(); System.out.println("After consolidate"); System.out.println(q); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!