/* * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) * Date: 2022-04-18 * Description: This is the Course class. */ public abstract class Course { /** * The code of the course. */ private String code; /** * The title of the course. */ private String title; /** * The pre-requisite of the course. * * A course always has only one pre-requisite course and the pre-requisite * course cannot be null. * * @see Course */ private Course preRequisite; /** * Constructor. * * @param code The code of the course. * @param title The title of the course. * @param preRequisite The pre-requisite of the course. If the pre-requisite * course is null, the constructor will print a message of * "pre-requisite course cannot be null!" and return * directly. */ public Course(String code, String title, Course preRequisite) { if (preRequisite == null) { System.out.println("pre-requisite course cannot be null!"); return; } this.code = code; this.title = title; this.preRequisite = preRequisite; } /** * Get the code of the course. * * @return The code of the course. */ public String getCode() { return code; } /** * Get the title of the course. * * @return The title of the course. */ public String getTitle() { return title; } /** * Get the pre-requisite of the course. * * @return The pre-requisite of the course. */ public Course getPreRequisite() { return preRequisite; } /** * Indicate whether a course is required or not. * * One course could either be a required course or a elective course. Some * courses are required, while the others are not required. * * @return True if the course is required, false if the course is not required. */ public abstract boolean isRequired(); /** * Test. */ public static void testCourse() { Course mr = new Course("CS101", "Intro to Java", null) { @Override public boolean isRequired() { return true; } }; System.out.println(mr.getCode() == null); System.out.println(mr.getTitle() == null); System.out.println(mr.getPreRequisite() == null); System.out.println(mr.isRequired() == true); Course me = new Course("CS102", "Intro to Python", mr) { @Override public boolean isRequired() { return false; } }; System.out.println(me.getCode() == "CS102"); System.out.println(me.getTitle() == "Intro to Python"); System.out.println(me.getPreRequisite() == mr); System.out.println(me.isRequired() == false); } }