In: Computer Science
Define and implement class Course. This class should contain the following fields: course name, course description, department, time the course starts, weekday the course is held on (for simplicity, let us assume the course only meets once a week). This class should contain getters and setters for all its attributes. This class also needs at least one constructor. Save this class and its definition into a file named Course.java.
Define and implement class Student. This class should contain the following fields: first name, last name, age, gpa, major, department, courses. Age should be an integer value. GPA should be a floating point value. Courses should be a linked list of Course variables. Class Student should contain getters and setters for all its attributes. This class also needs at least one constructor. In class Student implement the following methods: addCourse() to add a new course, removeCourse() to remove a course from this student, sortCourses() to print out a sorted list of courses. Method sortCourses() should accept parameters to specify whether the sorting should be ascending or descending and also based on which course attribute to sort the courses. The output should be printed to command line standard output. Save this class and its definition into a file named Student.java.
Course.java
class Course {
private String courseName;
private String courseDescription;
private String department;
// assuming format YYYY-MM-DD for easier sorting
private String courseStartTime;
private String weekday;
public Course() {}
public Course(String courseName, String courseDescription, String department, String courseStartTime, String weekday) {
this.courseName = courseName;
this.courseDescription = courseDescription;
this.department = department;
this.courseStartTime = courseStartTime;
this.weekday = weekday;
}
public String getCourseName() {
return courseName;
}
public String getCourseDescription() {
return courseDescription;
}
public String getDepartment() {
return department;
}
public String getStartTime() {
return courseStartTime;
}
public String getDay() {
return weekday;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public void setCourseDescription(String courseDescription) {
this.courseDescription = courseDescription;
}
public void setDepartment(String department) {
this.department = department;
}
public void setStartTime(String courseStartTime) {
this.courseStartTime = courseStartTime;
}
public void setDay(String weekday) {
this.weekday = weekday;
}
@Override
public String toString() {
// print the course object as defined below
return "{{\"Course\": " + courseName + "}, {\"Description\": " + courseDescription + "}, {\"Department\": " +
department + "}, {\"Start Time\": " + courseStartTime + "}, {\"Day\": " + weekday + "}}";
}
}
Student.java
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.HashMap;
class Student {
private String firstName;
private String lastName;
private int age;
private float gpa;
private String major;
private String department;
LinkedList<Course> enrollments;
public Student() {
enrollments = new LinkedList<Course>();
}
public Student(String firstName, String lastName, int age, float gpa, String major, String department) {
// this() calls the non-parametrized constructor which initializes the linked list
this();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gpa = gpa;
this.major = major;
this.department = department;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public float getGPA() {
return gpa;
}
public String getMajor() {
return major;
}
public String getDepartment() {
return department;
}
public LinkedList<Course> getCourseEnrollments() {
return enrollments;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAge(int age) {
this.age = age;
}
public void setGPA(float gpa) {
this.gpa = gpa;
}
public void setMajor(String major) {
this.major = major;
}
public void setDepartment(String department) {
this.department = department;
}
public void addCourse(Course course) {
// do nothing if the linked list already contains the course
if(!enrollments.contains(course)) {
enrollments.add(course);
}
}
public void removeCourse(Course course) {
// course can be removed only if it is present
if(enrollments.contains(course)) {
enrollments.remove(course);
}
}
public void sortCourses(String attribute, boolean sortDescending) {
// dayMapping is used below to sort when sorted using day of the week
// i.e. on sorting by day in ascending order, courses starting on Monday should come before courses starting on Wednesday for e.g.
// hence we will use the value corresponding to the day for sorting
HashMap<String, Integer> dayMapping = new HashMap<String, Integer>();
dayMapping.put("Sunday", 0);
dayMapping.put("Monday", 1);
dayMapping.put("Tuesday", 2);
dayMapping.put("Wednesday", 3);
dayMapping.put("Thursday", 4);
dayMapping.put("Friday", 5);
dayMapping.put("Saturday", 6);
// define how to sort the linked list by overriding the compare() method
Collections.sort(enrollments, new Comparator<Course>() {
@Override
public int compare(Course o1, Course o2) {
// sorting depending on the attribute specified
switch(attribute) {
case "name":
// course names are strings
// compareTo is a Java string method for comparing strings
return o1.getCourseName().compareTo(o2.getCourseName());
case "department":
return o1.getDepartment().compareTo(o2.getDepartment());
case "time":
return o1.getStartTime().compareTo(o2.getStartTime());
case "day":
return (dayMapping.get(o1.getDay()) -
dayMapping.get(o2.getDay()));
default:
// the default sorting behavior is based on course name
return o1.getCourseName().compareTo(o2.getCourseName());
}
}
});
// reverse the resultant linked list if sorting in descending order was required
if(sortDescending) {
Collections.reverse(enrollments);
}
// print the list of courses
for(Course c : enrollments) {
System.out.println(c);
}
}
}
Tester.java
public class Tester {
public static void main(String[] args) {
Course course = new Course("DSA-101", "Introduction to Algorithms", "CSED", "2020-10-19", "Monday");
Course course2 = new Course("OOM-101", "Object Oriented Modelling", "CSED", "2020-10-21", "Wednesday");
Course course3 = new Course("ST-101", "Statistics", "CSED", "2020-10-16", "Friday");
Student student = new Student("John", "Doe", 21, 3.5f, "CS", "CSED");
student.addCourse(course);
student.addCourse(course2);
student.addCourse(course3);
student.sortCourses("time", true);
System.out.println("\n");
student.sortCourses("day", false);
}
}
Sample Output (Corresponding to code in Tester.java).