Question: To be done in Java: 2.1. To the HighArray class in the highArray.java program (Listing 2.3), add a method called getMax() that returns the value
To be done in Java:
2.1. To the HighArray class in the highArray.java program (Listing 2.3), add a
method called getMax() that returns the value of the highest key in the array,
or 1 if the array is empty. Add some code in main() to exercise this method.
You can assume all the keys are positive numbers.
2.2 Modify the method in Programming Project 2.1 so that the item with the
highest key is not only returned by the method, but also removed from the
array. Call the method removeMax().
2.3 The removeMax() method in Programming Project 2.2 suggests a way to sort
the contents of an array by key value. Implement a sorting scheme that does
not require modifying the HighArray class, but only the code in main(). Youll
need a second array, which will end up inversely sorted. (This scheme is a
rather crude variant of the selection sort in Chapter 3, Simple Sorting.)
2.5 Add a merge() method to the OrdArray class in the orderedArray.java
program (Listing 2.4) so that you can merge two ordered source arrays into an
ordered destination array. Write code in main() that inserts some random
numbers into the two source arrays, invokes merge(), and displays the contents
of the resulting destination array. The source arrays may hold different
numbers of data items. In your algorithm you will need to compare the keys of
the source arrays, picking the smallest one to copy to the destination. Youll
also need to handle the situation when one source array exhausts its contents
before the other.
Code to be used:
// highArray.java // demonstrates array class with high-level interface // to run this program: C>java HighArrayApp //////////////////////////////////////////////////////////////// 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 arr.insert(77); // insert 10 items arr.insert(99); arr.insert(44); arr.insert(55); arr.insert(22); arr.insert(88); arr.insert(11); arr.insert(00); arr.insert(66); arr.insert(33); arr.display(); // display items int searchKey = 35; // search for item if( arr.find(searchKey) ) System.out.println("Found " + searchKey); else System.out.println("Can't find " + searchKey); arr.delete(00); // delete 3 items arr.delete(55); arr.delete(99); arr.display(); // display items again } // end main() } // end class HighArrayApp
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
