In: Computer Science
Java Program. Please read carefully. i need the exact same output as below. if you unable to write the code according to the question, please dont do it. thanks
To the HighArray class in the highArray.java program (Listing 2.3), add the following methods:
1. getMax() that returns the value of the highest key (value) in the array without removing it from the array, or –1 if the array is empty.
2. removeMax() that removes the item with the highest key from the array.
3. reverse() method for the HighArray class to reverse the order of elements of the array.
Sample output of HighArray class once you do the above methods:
current array items: 77 99 44 55 22 88 11 0 66 33
Can't find 35
array items after delete some values: 77 44 22 88 11 66 33
the Max is 88
the array after calling max method: 77 44 22 88 11 66 33
the array after calling remove max method: 77 44 22 11 66 33
the new max is 77
the array after calling reverse method 33 66 11 22 44 77
class HighArray { private long[] a; // ref to array a private int nElems; // number of data items //----------------------------------------------------------- public HighArray(int max) // constructor { a = new long[max]; // create the array nElems = 0; // no items yet } //----------------------------------------------------------- public boolean find(long searchKey) { // find specified value int j; for (j = 0; j < nElems; j++) // for each element, if (a[j] == searchKey) // found item? break; // exit loop before end if (j == nElems) // gone to end? return false; // yes, can't find it else return true; // no, found it } // end find() //----------------------------------------------------------- public void insert(long value) // put element into array { a[nElems] = value; // insert it nElems++; // increment size } //----------------------------------------------------------- public boolean delete(long value) { int j; for (j = 0; j < nElems; j++) // look for it if (value == a[j]) break; if (j == nElems) // can't find it return false; else // found it { for (int k = j; k < nElems; k++) // move higher ones down a[k] = a[k + 1]; nElems--; // decrement size return true; } } // end delete() //----------------------------------------------------------- public void display() // displays array contents { for (int j = 0; j < nElems; j++) // for each element, System.out.print(a[j] + " "); // display it System.out.println(""); } public long getMax() { if (nElems == 0) { return -1; } else { long max = a[0]; for (int i = 0; i < nElems; i++) { if (a[i] > max) { max = a[i]; } } return max; } } public long removeMax() { if (nElems == 0) { return -1; } else { int maxIndex = 0; for (int i = 0; i < nElems; i++) { if (a[i] > a[maxIndex]) { maxIndex = i; } } long result = a[maxIndex]; for (int i = 0; i < nElems; i++) { if (a[i] == result) { for (int j = i; j < nElems - 1; j++) { a[j] = a[j + 1]; } nElems--; i--; } } return result; } } public void reverse() { long temp; for (int i = 0; i < nElems; i++) { temp = a[i]; a[i] = a[nElems - i - 1]; a[nElems - i - 1] = temp; } } //----------------------------------------------------------- } // end class HighArray