CSCE 146

Lab 11

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:

while (there is more data in the file ‘newfile.dat’)

{

         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

}

3.      To obtain a sorted list of Student objects, simply remove objects from the Heap, using the ‘downward reheapification’ algorithm, and insert the removed objects into an array. This will give you an array of students arranged in decreasing order of their SSN.

4.      The final step in this lab is to implement a binary search on the sorted array of students that you have obtained in step 3 above. Write a method with the following signature:

private binarySearch(long[] a, int first, int size, long ssn)

{

         // 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.

        Enjoy!!