Class Registration The two methods have you use a PriorityQueue to order Student
ID: 3808204 • Letter: C
Question
Class Registration
The two methods have you use a PriorityQueue to order Student objects (already written for you) for class registration. To begin, implement the studentCompare method, which implements the registration priority policy (see the JavaDoc for details). Once that is complete, implement the classRegistration method, which inputs an array of Student objects, and then returns these same objects in the order in which they can register (i.e. greatest priority first).
You MUST use a PriorityQueue to order the Student objects in this method.
/**
* Student registration information
*
*/
public class Student {
final private String name;
final private boolean priority;
final private int credits;
/**
* Initializes the student
*
* @param name student name
* @param priority does the student have priority registration?
* @param credits completed credits
*/
public Student(String name, boolean priority, int credits) {
this.name = name;
this.priority = priority;
this.credits = credits;
}
/**
* Get the student's name
*
* @return student name
*/
public String getName() {
return name;
}
/**
* Get the student's priority status
*
* @return true if student has priority registration
*/
public boolean getPriority() {
return priority;
}
/**
* Get the student's completed credits
*
* @return student completed credits
*/
public int getCredits() {
return credits;
}
@Override
public String toString() {
return String.format("%s (%b, %d)", name, priority, credits);
}
}
The two methods:
/**
* Compares two students for registration priority:
* - If both have priority, higher credits is greater
* - If one has priority and the other doesn't, the priority is greater
* - If neither have priority, higher credits is greater
*
* @param s1 student 1
* @param s2 student 2
* @return 0 if students are equal, 1 if s1 is greater, -1 if s2 is greater
*/
public static int studentCompare(Student s1, Student s2) {
return 100; // replace with your code
}
/**
* Orders students according to
* registration priority
*
* @param students students to order
* @return students sorted by greatest priority first (see studentCompare)
*/
public static Student[] classRegistration(Student[] students) {
// Suggested algorithm:
// 1. Create a priority queue that uses studentCompare,
// at last in part, for its comparator
// 2. Add all the students to the priority queue
// 3. Remove all the students from the queue in order
// into an array
// 4. Return the array
return new Student[] {};
}
Explanation / Answer
Hi,
Please see below the updated classes.
Registration.java
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;
public class Registration {
/**
* Compares two students for registration priority:
* - If both have priority, higher credits is greater
* - If one has priority and the other doesn't, the priority is greater
* - If neither have priority, higher credits is greater
*
* @param s1 student 1
* @param s2 student 2
* @return 0 if students are equal, 1 if s1 is greater, -1 if s2 is greater
*/
public static int studentCompare(Student s1, Student s2) {
int retVal =0;
if((s1.getPriority() && s2.getPriority()) || ((!s1.getPriority())&&(!s2.getPriority()))){
if (s1.getCredits()== s2.getCredits()){
retVal =0;
}
else if(s1.getCredits()>s2.getCredits()){
retVal =1;
}
else if(s1.getCredits()<s2.getCredits()){
retVal =-1;
}
}
else{
if(s1.getPriority() && ! s2.getPriority()){
retVal = 1;
}
else if( (!s1.getPriority()) && s2.getPriority()){
retVal = -1;
}
}
return retVal;
}
/**
* Orders students according to
* registration priority
*
* @param students students to order
* @return students sorted by greatest priority first (see studentCompare)
*/
public static Student[] classRegistration(Student[] students) {
// Suggested algorithm:
// 1. Create a priority queue that uses studentCompare,
// at last in part, for its comparator
// 2. Add all the students to the priority queue
// 3. Remove all the students from the queue in order
// into an array
// 4. Return the array
Student[] stArray;
PriorityQueue<Student> studentPriorityQueue = new PriorityQueue<>(students.length, studentComparator);
addDataToQueue (studentPriorityQueue, students);
stArray = pollDataFromQueue (studentPriorityQueue);
return stArray;
}
//Comparator anonymous class implementation
public static Comparator<Student> studentComparator = new Comparator<Student>(){
@Override
public int compare(Student c1, Student c2) {
return studentCompare(c1,c2);
}
};
private static void addDataToQueue(PriorityQueue<Student> studentPriorityQueue, Student[] stArray) {
for(int i=0; i<stArray.length; i++){
studentPriorityQueue.add(stArray[i]);
}
}
private static Student[] pollDataFromQueue(Queue<Student> studentPriorityQueue) {
Student[] stArray = new Student[studentPriorityQueue.size()];
int counter =0;
while(true){
Student student = studentPriorityQueue.poll();
if(student == null) break;
stArray[counter] = student;
counter++;
}
return stArray;
}
public static void main(String [] args){
Student [] stArray = new Student[3];
Student st1 = new Student("ABC", true, 10);
Student st2 = new Student("EBC", true, 20);
Student st3 = new Student("FBC", true, 30);
stArray[0] = st1;
stArray[1] = st2;
stArray[2] = st3;
printArray(classRegistration(stArray));
}
public static void printArray(Student[] stArr){
for(Student st: stArr){
System.out.println(st.toString());
}
}
}
Student.java
/**
* Student registration information
*
*/
public class Student {
final private String name;
final private boolean priority;
final private int credits;
/**
* Initializes the student
*
* @param name student name
* @param priority does the student have priority registration?
* @param credits completed credits
*/
public Student(String name, boolean priority, int credits) {
this.name = name;
this.priority = priority;
this.credits = credits;
}
/**
* Get the student's name
*
* @return student name
*/
public String getName() {
return name;
}
/**
* Get the student's priority status
*
* @return true if student has priority registration
*/
public boolean getPriority() {
return priority;
}
/**
* Get the student's completed credits
*
* @return student completed credits
*/
public int getCredits() {
return credits;
}
@Override
public String toString() {
return String.format("%s (%b, %d)", name, priority, credits);
}
}
Sample output:
ABC (true, 10)
EBC (true, 20)
FBC (true, 30)