a4 close #6
This commit is contained in:
58
assignment4/Question6/Base.java
Normal file
58
assignment4/Question6/Base.java
Normal file
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-18
|
||||
* Description: This is the Base Class.
|
||||
*/
|
||||
|
||||
public class Base implements Learnable {
|
||||
private String code;
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param code The code of the course
|
||||
* @param title The title of the course
|
||||
*/
|
||||
public Base(String code, String title) {
|
||||
this.code = code;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of code
|
||||
*
|
||||
* @return The code of the course
|
||||
*/
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of title
|
||||
*
|
||||
* @return The title of the course
|
||||
*/
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pre-requisite course. It will return itself.
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
public Learnable getPreRequisite() {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testBase() {
|
||||
Base base = new Base("COMP3013", "Database Management System");
|
||||
System.out.println(base.getCode() == "COMP3013");
|
||||
System.out.println(base.getTitle() == "Database Management System");
|
||||
System.out.println(base.getPreRequisite() == base);
|
||||
}
|
||||
}
|
||||
113
assignment4/Question6/Course.java
Normal file
113
assignment4/Question6/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 implements Learnable {
|
||||
/**
|
||||
* 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 Learnable 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, Learnable 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 Learnable 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);
|
||||
|
||||
}
|
||||
}
|
||||
13
assignment4/Question6/Learnable.java
Normal file
13
assignment4/Question6/Learnable.java
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-18
|
||||
* Description: This is the Learnable interface.
|
||||
*/
|
||||
|
||||
public interface Learnable {
|
||||
public String getCode();
|
||||
|
||||
public String getTitle();
|
||||
|
||||
public Learnable getPreRequisite();
|
||||
}
|
||||
56
assignment4/Question6/MajorElective.java
Normal file
56
assignment4/Question6/MajorElective.java
Normal file
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* 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, Learnable 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);
|
||||
|
||||
Base base = new Base("COMP4023", "Software Engineering");
|
||||
System.out.println(base.getCode() == "COMP4023");
|
||||
System.out.println(base.getTitle() == "Software Engineering");
|
||||
System.out.println(base.getPreRequisite() == base);
|
||||
|
||||
MajorElective me = new MajorElective("CSC123", "Big Data", base);
|
||||
System.out.println(me.getCode() == "CSC123");
|
||||
System.out.println(me.getTitle() == "Big Data");
|
||||
System.out.println(me.getPreRequisite() == base);
|
||||
System.out.println(me.isRequired() == false);
|
||||
}
|
||||
}
|
||||
57
assignment4/Question6/MajorRequired.java
Normal file
57
assignment4/Question6/MajorRequired.java
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* 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, Learnable 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);
|
||||
|
||||
Base base = new Base("COMP4023", "Software Engineering");
|
||||
System.out.println(base.getCode() == "COMP4023");
|
||||
System.out.println(base.getTitle() == "Software Engineering");
|
||||
System.out.println(base.getPreRequisite() == base);
|
||||
|
||||
MajorRequired mr3 = new MajorRequired("COMP4101", "Quality Assurance", base);
|
||||
System.out.println(mr3.getCode() == "COMP4101");
|
||||
System.out.println(mr3.getTitle() == "Quality Assurance");
|
||||
System.out.println(mr3.getPreRequisite() == base);
|
||||
System.out.println(mr3.isRequired() == true);
|
||||
}
|
||||
}
|
||||
51
assignment4/Question6/ManyCourses.java
Normal file
51
assignment4/Question6/ManyCourses.java
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
|
||||
* Date: 2022-04-18
|
||||
* Description: This is the ManyCourses class.
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ManyCourses {
|
||||
private ArrayList<Learnable> courses;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public ManyCourses() {
|
||||
courses = new ArrayList<Learnable>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a course to the list.
|
||||
*
|
||||
* @param course
|
||||
*/
|
||||
public void addCourse(Learnable c) {
|
||||
courses.add(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints on the screen the course code and title.
|
||||
*/
|
||||
public void listCourses() {
|
||||
for (Learnable c : courses) {
|
||||
System.out.println(
|
||||
String.format("%s, %s", c.getCode(), c.getTitle()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test.
|
||||
*/
|
||||
public static void testManyCourses() {
|
||||
Base b = new Base("DS1001", "EntryCourse");
|
||||
MajorRequired mr1 = new MajorRequired("DS200X", "OOP", b);
|
||||
MajorElective me1 = new MajorElective("DS300X", "Data Mining", mr1);
|
||||
ManyCourses mc = new ManyCourses();
|
||||
mc.addCourse(b);
|
||||
mc.addCourse(mr1);
|
||||
mc.addCourse(me1);
|
||||
mc.listCourses();
|
||||
}
|
||||
}
|
||||
28
assignment4/Question6/Start.java
Normal file
28
assignment4/Question6/Start.java
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* 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();
|
||||
Base.testBase();
|
||||
ManyCourses.testManyCourses();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user