In the last lab, you implemented methods to add and remove elements from a Heap data structure. In this lab, we’ll use a Heap to sort a list of objects. The lab will consist of the following steps:
1. Create a ‘Student’ class to represent a student entity. The data regarding students is contained in the ‘newfile.dat’ file that you used in the last lab. The Student class must have the following class variables:
long ssn;
string name;
int numHours;
double gpa;
Your class must implement the Comparable interface. This interface requires the class to have the compareTo() method, which compares two objects of the Student class. The basis for ordering objects of the Student class is the SSN of the student.
2. Now
we are ready to sort a list of Student
objects using Heaps. To begin with, we need to construct a heap of students. Modify
the Heap class that you wrote in
lab-10 so that it is capable of holding objects. The pseudo-code for constructing
a heap is given below:
{
Read data
corresponding to the next student.
Instantiate a
Student object with this data
Add the Student
object to a heap using the ‘upward reheapification’ algorithm
}
{
// method implementation
}
This method
takes the SSN as a parameter and returns a reference to the Student object if a student by that
SSN exists in the array, else it returns null. Test this method by
searching for a particular student and then printing out her details.