Your job is to make the code in Test (shown below) produce the output that follows.
In doing so, you must:
• Use the Test code provided
• Make sure your output looks EXACTLY like the expected output
• Implement the classes Course, CourseSchedule, Registrar. Use a List to
implement CourseSchedule
• Use a Map to implement Registrar
EXTRA CREDIT (3 points total)
• 1 point: Support deRegister (removes CourseSchedule from registrar)
• 2 points: Sort the CourseSchedule every time a Course is added
NOTES:
• All of this material has been covered in past lectures. Please review the slides
if you need to.
• To do the sorting, one of your classes will need to implement Comparable,
just as we demonstrated in class. We also covered how to sort collections,
which again you should review in the slides if you need to.
• This assignment has 3 other pages that follow this one, please mke sure you
review
• You should also consider looking at the Javadoc pages for List and Map, in
case you need those (do a Google search)
Test code:
public class Test {
public static void main(String[] args) {
// Build Annie’s schedule
CourseSchedule annieSchedule = new CourseSchedule(“Annie Atom”);
annieSchedule.add(new Course(“CS 100”, “Prof, Smith”));
annieSchedule.add(new Course(“ANTHRO 10”, “Prof. Jones”));
annieSchedule.add(new Course(“MATH 125”, “Prof. Aardvark”));
annieSchedule.display();
// Build Myra’s schedule
CourseSchedule myraSchedule = new CourseSchedule(“Myra Moore”);
myraSchedule.add(new Course(“CS 233”, “Prof, Jensen”));
myraSchedule.add(new Course(“CS 234”, “Prof. Thomas”));
myraSchedule.display();
// Build Zach’s schedule
CourseSchedule zachSchedule = new CourseSchedule(“Zach Zween”);
zachSchedule.add(new Course(“PSYCH 140”, “Prof, Arnold”));
zachSchedule.add(new Course(“ANTHRO 10”, “Prof. Jones”));
zachSchedule.add(new Course(“FRENCH 1”, “Prof. Renior”));
zachSchedule.display();
// Register all of them
Registrar r = new Registrar();
r.register(annieSchedule);
r.register(myraSchedule);
r.register(zachSchedule);
// EXTRA CREDIT (1 pt)
r.deRegister(“Myra Moore”);
r.display();
}
}
Expert Answer
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
class CourseSchedule {
String courseName;
List<Course> courses;
CourseSchedule(String courseName) {
this.courses = new LinkedList<>();
this.courseName = courseName;
}
void display() {
System.out.println(“Course name: ” + courseName);
System.out.println(“Courses:”);
int n = 1;
for (Course course : courses) {
System.out.println(n++ + “. ” + course);
}
}
void add(Course course) {
courses.add(course);
}
public String getCourseName() {
return courseName;
}
public List<Course> getCourses() {
return courses;
}
}
class Course {
private final String subject;
private final String prof;
Course(String subject, String prof) {
this.subject = subject;
this.prof = prof;
}
@Override
public String toString() {
return subject + ” by ” + prof;
}
public String getSubject() {
return subject;
}
public String getProf() {
return prof;
}
}
class Registrar {
Map<String, List<Course>> courses;
public Registrar() {
courses = new HashMap<>();
}
void register(CourseSchedule courseSchedule) {
courses.put(courseSchedule.getCourseName(), courseSchedule.getCourses());
}
void deRegister(String courseName) {
courses.remove(courseName);
}
void display() {
Set<Map.Entry<String, List<Course>>> set = courses.entrySet();
Iterator<Map.Entry<String, List<Course>>> iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry<String, List<Course>> entry = iterator.next();
System.out.println(“Course name: ” + entry.getKey());
System.out.println(“Courses:”);
int n = 1;
for (Course course : entry.getValue()) {
System.out.println(n++ + “. ” + course);
}
}
}
}
output:
run:
Course name: Annie Atom
Courses:
1. CS 100 by Prof, Smith
2. ANTHRO 10 by Prof. Jones
3. MATH 125 by Prof. Aardvark
Course name: Myra Moore
Courses:
1. CS 233 by Prof, Jensen
2. CS 234 by Prof. Thomas
Course name: Zach Zween
Courses:
1. PSYCH 140 by Prof, Arnold
2. ANTHRO 10 by Prof. Jones
3. FRENCH 1 by Prof. Renior
Course name: Annie Atom
Courses:
1. CS 100 by Prof, Smith
2. ANTHRO 10 by Prof. Jones
3. MATH 125 by Prof. Aardvark
Course name: Zach Zween
Courses:
1. PSYCH 140 by Prof, Arnold
2. ANTHRO 10 by Prof. Jones
3. FRENCH 1 by Prof. Renior