Problem

The Heap class given in Section 11.3 sorts only the integers. Improve the class by makin...

The Heap class given in Section 11.3 sorts only the integers. Improve the class by making it possible to sort any objects that recognize the compareTo method, which is described in Section 11.4, so the new Heap class will be able to sort Person, Integer, Double, and String objects among others. Since the elements in the internal array can be any object, declare an array of Object objects. All Java classes are automatically a subclass of Object, unless they are declared explicitly as a subclass of another class. The declaration of heap will be like this

private Object[ ] heap;

and the setData method will be like this:

public void setData(Object[ ] data) {

heap = new Object[data.length];

sortedList = new Object[data.length];

for ( int i = 0; i < data.length; i++) {

heap[i] = data[i];

}

}

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 11