a4 close #6
This commit is contained in:
113
assignment4/Question3/Course.java
Normal file
113
assignment4/Question3/Course.java
Normal file
@@ -0,0 +1,113 @@
|
||||
/*
|
||||
* 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);
|
||||
|
||||
}
|
||||
}
|
||||
45
assignment4/Question3/MajorElective.java
Normal file
45
assignment4/Question3/MajorElective.java
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-18
|
||||
* Description: This is the MajorElective class which is elective for students.
|
||||
*/
|
||||
|
||||
public class MajorElective extends Course {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param code The code of the course.
|
||||
* @param title The title of the course.
|
||||
* @param preRequisite The pre-requisite of the course. Type of MajorElective
|
||||
* and cannot be null.
|
||||
*/
|
||||
public MajorElective(String code, String title, MajorElective preRequisite) {
|
||||
super(code, title, preRequisite);
|
||||
}
|
||||
|
||||
/**
|
||||
* MajorRequired course is elective for all students.
|
||||
*
|
||||
* @return false
|
||||
*/
|
||||
public boolean isRequired() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMajorElective() {
|
||||
MajorElective pre = new MajorElective("CSC108", "Intro to Programming", null);
|
||||
System.out.println(pre.getCode() == null);
|
||||
System.out.println(pre.getTitle() == null);
|
||||
System.out.println(pre.getPreRequisite() == null);
|
||||
System.out.println(pre.isRequired() == false);
|
||||
|
||||
MajorElective pre2 = new MajorElective("CSC108", "Intro to Programming", pre);
|
||||
System.out.println(pre2.getCode() == "CSC108");
|
||||
System.out.println(pre2.getTitle() == "Intro to Programming");
|
||||
System.out.println(pre2.getPreRequisite() == pre);
|
||||
System.out.println(pre2.isRequired() == false);
|
||||
}
|
||||
}
|
||||
46
assignment4/Question3/MajorRequired.java
Normal file
46
assignment4/Question3/MajorRequired.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-18
|
||||
* Description: This is the MajorRequired class which is used to store the major and required courses.
|
||||
*/
|
||||
|
||||
public class MajorRequired extends Course {
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param code The code of the course.
|
||||
* @param title The title of the course.
|
||||
* @param preRequisite The pre-requisite of the course. Type of MajorRequired
|
||||
* and cannot be null.
|
||||
*/
|
||||
public MajorRequired(String code, String title, MajorRequired preRequisite) {
|
||||
super(code, title, preRequisite);
|
||||
}
|
||||
|
||||
/**
|
||||
* MajorRequired course is required for all students.
|
||||
*
|
||||
* @return true
|
||||
*/
|
||||
@Override
|
||||
public boolean isRequired() {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testMajorRequired() {
|
||||
MajorRequired mr1 = new MajorRequired("COMP3013", "Database Management System", null);
|
||||
System.out.println(mr1.getCode() == null);
|
||||
System.out.println(mr1.getTitle() == null);
|
||||
System.out.println(mr1.getPreRequisite() == null);
|
||||
System.out.println(mr1.isRequired() == true);
|
||||
|
||||
MajorRequired mr2 = new MajorRequired("COMP3013", "Database Management System", mr1);
|
||||
System.out.println(mr2.getCode() == "COMP3013");
|
||||
System.out.println(mr2.getTitle() == "Database Management System");
|
||||
System.out.println(mr2.getPreRequisite() == mr1);
|
||||
System.out.println(mr2.isRequired() == true);
|
||||
}
|
||||
}
|
||||
26
assignment4/Question3/Start.java
Normal file
26
assignment4/Question3/Start.java
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Answer to the questions.
|
||||
*
|
||||
* - Q: What kinds of tests can you write inside the testCourse method?
|
||||
*
|
||||
* - A: You can inherit the Couse class to annonymous class and override the
|
||||
* isRequired() method.
|
||||
*
|
||||
* - Q: What kinds of tests can you write inside the testMajorRequired method?
|
||||
*
|
||||
* - A: You can first create a MajorRequired object with null preRequisite and
|
||||
* do the test as normal.
|
||||
*
|
||||
* - Q: What kinds of tests can you write inside the testMajorElective method?
|
||||
*
|
||||
* - A: You can first create a MajorElective object with null preRequisite and
|
||||
* do the test as normal.
|
||||
*/
|
||||
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Course.testCourse();
|
||||
MajorRequired.testMajorRequired();
|
||||
MajorElective.testMajorElective();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user