QUICKSORT: STUDENT MANAGEMENT Design and write an object-oriented program for ma
ID: 3567378 • Letter: Q
Question
QUICKSORT: STUDENT MANAGEMENT
Design and write an object-oriented program for managing the attendance of students in a course. Your solution must consist of a class named Student that has a stores information pertaining to a student (ie name, student identification number, and etc.). Their should be appropriate member functions available to communicate to a Student Member.
Additonally, the driver program should create an array instance of this class of size 30 and read this data into the members. Your driver program should take the Student object and perform all of the following:
A. Sort based on last name
B. Sort based on Student ID
C. Ask user to enter a Student ID number and return the user's information that matches that identification number.
You should remember to analyze the quicksort function and solve the recurrence relation.
HERE IS THE DATA THAT IS SUPPOSE TO BE READ INTO THE MEMBERS
Timothy Holston 201211202
LaTonya Garner 201200292
Paul Wu 200012112
Lee Redmond 201112065
Sandra Course 201112174
Constance Bland 201300085
Stacy White 201452688
Darryl James 200202177
Margaret Reynolds 201142582
Garfield Burke 200925675
Raymond Williams 200856967
Qiang He 201452470
Marcus Golden 200065979
Explanation / Answer
Text file: StudentData
Timothy Holston 201211202
LaTonya Garner 201200292
Paul Wu 200012112
Lee Redmond 201112065
Sandra Course 201112174
Constance Bland 201300085
Stacy White 201452688
Darryl James 200202177
Margaret Reynolds 201142582
Garfield Burke 200925675
Raymond Williams 200856967
Qiang He 201452470
Marcus Golden 200065979
---------------------------------------------------------------------------------------------------------------------------
Program Code:
//Student class that holds details of each student
public class Student
{
String lname;
String fname;
String id;
public Student(String ln,String fn, String idnum )
{
lname=ln;
fname=fn;
id=idnum;
}
//get methods to retrieve the values of instance variables
public String getLname()
{
return lname;
}
public String getFname()
{
return fname;
}
public String getId()
{
return id;
}
}
//import the required packages
//those are, io package to deal with files
//util package to deal with Scanner class
import java.util.*;
import java.io.*;
//driver class
public class QuickSortOnStudentData
{
//main function
public static void main(String args[])throws Exception
{
//declare the variables
String fname;
String lname;
String id;
String sid;
//declare an array of size 30 of Student type
Student sArray[]=new Student[30];
//by using scanner object input read the data from
//the file
Scanner input=new Scanner(new FileReader(new File("StudentData")));
//by using scanner object in read the data
//from the console
Scanner in=new Scanner(System.in);
//declare a variable count
int count=0;
//loop to read each individual data
//from the text file
while(input.hasNext())
{
lname=input.next();
fname=input.next();
id=input.next();
//create object ot Student class
Student s=new Student(lname, fname, id);
//add the object to the sArray
sArray[count]=s;
//increment the count
count++;
}
//close the file
input.close();
//print the data present in the sArray before sorting
System.out.println("The students details before sorting are: ");
for(int i=0;i<count;i++)
System.out.println(sArray[i].getLname()+" "+sArray[i].getFname()+" "+sArray[i].getId());
System.out.println();
System.out.println("==========================================================");
//print the data present in the sArray after calling the
//quickSort so that the sArray is sort by last name
System.out.println("Sort the array by last name using Quick sort: ");
//call the quickSort
quickSort(sArray , 0, count);
for(int i=0;i<count;i++)
{
System.out.println(sArray[i].getLname()+" "+sArray[i].getFname()+" "+sArray[i].getId());
}
System.out.println();
System.out.println("==========================================================");
//print the data present in the sArray after calling the
//quickSort1 so that the sArray is sort by id
System.out.println("Sort the array by id using Quick sort: ");
quickSort1(sArray, 0, count);
for(int i=0;i<count;i++)
{
System.out.println(sArray[i].getLname()+" "+sArray[i].getFname()+" "+sArray[i].getId());
}
System.out.println();
System.out.println("==========================================================");
//prompt the user to enter an id to search and
//print the details
System.out.println(" Enter the Id of the student to search: ");
sid=in.next();
//call the seachId function and store the return index value in index variable
int index=searchId(sArray, count, sid);
//display the student details whose index is found
System.out.println("The student details at ["+(index+1)+"] are: ");
System.out.println(sArray[index].getLname()+" "+sArray[index].getFname()+" "+sArray[index].getId());
}
//partition method to get the pivot value and sort the data
public static int partition(Student[] a, int beg, int end)
{
int p=beg; ;
Student pivot=a[beg];
//for loop to retrieve the pivot element
for(int j=beg+1;j<end;j++)
{
if(pivot.getLname().compareTo(a[j].getLname())>0)
{
a[p]=a[j];
a[j]=a[p+1];
a[p+1]=pivot;
p=p+1;
}
}
return p;
}
//quickSort function
public static void quickSort(Student a[], int beg, int end)
{
if(beg<end)
{
//retrieving the pivot element
int p=partition(a,beg,end);
//calling the self quickSort function
quickSort(a,beg,p-1);
quickSort(a,p+1,end);
}
}
//quick sort function to sort by id
public static void quickSort1(Student a[], int beg, int end)
{
if(beg<end)
{
//retrieving the pivot element
int p=partition1(a,beg,end);
//calling the self quickSort function
quickSort1(a,beg,p-1);
quickSort1(a,p+1,end);
}
}
//partition to sort the array by id
public static int partition1(Student[] a, int beg, int end)
{
int p=beg; ;
Student pivot=a[beg];
//for loop to retrieve the pivot element
for(int j=beg+1;j<end;j++)
{
if(pivot.getId().compareTo(a[j].getId())<0)
{
a[p]=a[j];
a[j]=a[p+1];
a[p+1]=pivot;
p=p+1;
}
}
return p;
}
//searchid method to search the respective id and
//return its index
public static int searchId(Student a[],int size, String id)
{
int ind=0;
for(int i=0;i<size;i++)
{
if(a[i].getId().equals(id))
{
ind=i;
}
}
return ind;
}
}
----------------------------------------------------------------------------------------------------------------
Sample Output:
The students details before sorting are:
Timothy Holston 201211202
LaTonya Garner 201200292
Paul Wu 200012112
Lee Redmond 201112065
Sandra Course 201112174
Constance Bland 201300085
Stacy White 201452688
Darryl James 200202177
Margaret Reynolds 201142582
Garfield Burke 200925675
Raymond Williams 200856967
Qiang He 201452470
Marcus Golden 200065979
==========================================================
Sort the array by last name using Quick sort:
Constance Bland 201300085
Darryl James 200202177
Garfield Burke 200925675
LaTonya Garner 201200292
Lee Redmond 201112065
Margaret Reynolds 201142582
Paul Wu 200012112
Raymond Williams 200856967
Sandra Course 201112174
Qiang He 201452470
Stacy White 201452688
Marcus Golden 200065979
Timothy Holston 201211202
==========================================================
Sort the array by id using Quick sort:
Qiang He 201452470
Stacy White 201452688
Constance Bland 201300085
Timothy Holston 201211202
LaTonya Garner 201200292
Margaret Reynolds 201142582
Sandra Course 201112174
Garfield Burke 200925675
Raymond Williams 200856967
Darryl James 200202177
Marcus Golden 200065979
Lee Redmond 201112065
Paul Wu 200012112
==========================================================
Enter the Id of the student to search:
201142582
The student details at [6] are:
Margaret Reynolds 201142582