From 941b3d26d46a2907035ff09a237606310d58e6f1 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 21 Apr 2022 15:43:38 +0800 Subject: [PATCH] a4 close #6 --- assignment4/Question1/Course.java | 113 +++++++++++++++++++++++ assignment4/Question1/Start.java | 14 +++ assignment4/Question2/Course.java | 113 +++++++++++++++++++++++ assignment4/Question2/MajorRequired.java | 46 +++++++++ assignment4/Question2/Start.java | 20 ++++ assignment4/Question3/Course.java | 113 +++++++++++++++++++++++ assignment4/Question3/MajorElective.java | 45 +++++++++ assignment4/Question3/MajorRequired.java | 46 +++++++++ assignment4/Question3/Start.java | 26 ++++++ assignment4/Question4/Base.java | 58 ++++++++++++ assignment4/Question4/Course.java | 113 +++++++++++++++++++++++ assignment4/Question4/MajorElective.java | 45 +++++++++ assignment4/Question4/MajorRequired.java | 46 +++++++++ assignment4/Question4/Start.java | 27 ++++++ assignment4/Question5/Base.java | 58 ++++++++++++ assignment4/Question5/Course.java | 113 +++++++++++++++++++++++ assignment4/Question5/Learnable.java | 13 +++ assignment4/Question5/MajorElective.java | 56 +++++++++++ assignment4/Question5/MajorRequired.java | 57 ++++++++++++ assignment4/Question5/Start.java | 27 ++++++ assignment4/Question6/Base.java | 58 ++++++++++++ assignment4/Question6/Course.java | 113 +++++++++++++++++++++++ assignment4/Question6/Learnable.java | 13 +++ assignment4/Question6/MajorElective.java | 56 +++++++++++ assignment4/Question6/MajorRequired.java | 57 ++++++++++++ assignment4/Question6/ManyCourses.java | 51 ++++++++++ assignment4/Question6/Start.java | 28 ++++++ 27 files changed, 1525 insertions(+) create mode 100644 assignment4/Question1/Course.java create mode 100644 assignment4/Question1/Start.java create mode 100644 assignment4/Question2/Course.java create mode 100644 assignment4/Question2/MajorRequired.java create mode 100644 assignment4/Question2/Start.java create mode 100644 assignment4/Question3/Course.java create mode 100644 assignment4/Question3/MajorElective.java create mode 100644 assignment4/Question3/MajorRequired.java create mode 100644 assignment4/Question3/Start.java create mode 100644 assignment4/Question4/Base.java create mode 100644 assignment4/Question4/Course.java create mode 100644 assignment4/Question4/MajorElective.java create mode 100644 assignment4/Question4/MajorRequired.java create mode 100644 assignment4/Question4/Start.java create mode 100644 assignment4/Question5/Base.java create mode 100644 assignment4/Question5/Course.java create mode 100644 assignment4/Question5/Learnable.java create mode 100644 assignment4/Question5/MajorElective.java create mode 100644 assignment4/Question5/MajorRequired.java create mode 100644 assignment4/Question5/Start.java create mode 100644 assignment4/Question6/Base.java create mode 100644 assignment4/Question6/Course.java create mode 100644 assignment4/Question6/Learnable.java create mode 100644 assignment4/Question6/MajorElective.java create mode 100644 assignment4/Question6/MajorRequired.java create mode 100644 assignment4/Question6/ManyCourses.java create mode 100644 assignment4/Question6/Start.java diff --git a/assignment4/Question1/Course.java b/assignment4/Question1/Course.java new file mode 100644 index 0000000..f1a379f --- /dev/null +++ b/assignment4/Question1/Course.java @@ -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); + + } +} diff --git a/assignment4/Question1/Start.java b/assignment4/Question1/Start.java new file mode 100644 index 0000000..e3ad3c4 --- /dev/null +++ b/assignment4/Question1/Start.java @@ -0,0 +1,14 @@ +/** + * 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. + */ + +public class Start { + public static void main(String[] args) { + Course.testCourse(); + } +} diff --git a/assignment4/Question2/Course.java b/assignment4/Question2/Course.java new file mode 100644 index 0000000..f1a379f --- /dev/null +++ b/assignment4/Question2/Course.java @@ -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); + + } +} diff --git a/assignment4/Question2/MajorRequired.java b/assignment4/Question2/MajorRequired.java new file mode 100644 index 0000000..33fe2d0 --- /dev/null +++ b/assignment4/Question2/MajorRequired.java @@ -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() == false); + } +} diff --git a/assignment4/Question2/Start.java b/assignment4/Question2/Start.java new file mode 100644 index 0000000..c5fa04a --- /dev/null +++ b/assignment4/Question2/Start.java @@ -0,0 +1,20 @@ +/** + * 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. + */ + +public class Start { + public static void main(String[] args) { + Course.testCourse(); + MajorRequired.testMajorRequired(); + } +} diff --git a/assignment4/Question3/Course.java b/assignment4/Question3/Course.java new file mode 100644 index 0000000..f1a379f --- /dev/null +++ b/assignment4/Question3/Course.java @@ -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); + + } +} diff --git a/assignment4/Question3/MajorElective.java b/assignment4/Question3/MajorElective.java new file mode 100644 index 0000000..cb9a4ef --- /dev/null +++ b/assignment4/Question3/MajorElective.java @@ -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); + } +} diff --git a/assignment4/Question3/MajorRequired.java b/assignment4/Question3/MajorRequired.java new file mode 100644 index 0000000..bc4b63a --- /dev/null +++ b/assignment4/Question3/MajorRequired.java @@ -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); + } +} diff --git a/assignment4/Question3/Start.java b/assignment4/Question3/Start.java new file mode 100644 index 0000000..3cdafef --- /dev/null +++ b/assignment4/Question3/Start.java @@ -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(); + } +} diff --git a/assignment4/Question4/Base.java b/assignment4/Question4/Base.java new file mode 100644 index 0000000..a71a415 --- /dev/null +++ b/assignment4/Question4/Base.java @@ -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 { + 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 Base 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); + } +} diff --git a/assignment4/Question4/Course.java b/assignment4/Question4/Course.java new file mode 100644 index 0000000..f1a379f --- /dev/null +++ b/assignment4/Question4/Course.java @@ -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); + + } +} diff --git a/assignment4/Question4/MajorElective.java b/assignment4/Question4/MajorElective.java new file mode 100644 index 0000000..cb9a4ef --- /dev/null +++ b/assignment4/Question4/MajorElective.java @@ -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); + } +} diff --git a/assignment4/Question4/MajorRequired.java b/assignment4/Question4/MajorRequired.java new file mode 100644 index 0000000..bc4b63a --- /dev/null +++ b/assignment4/Question4/MajorRequired.java @@ -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); + } +} diff --git a/assignment4/Question4/Start.java b/assignment4/Question4/Start.java new file mode 100644 index 0000000..cd44305 --- /dev/null +++ b/assignment4/Question4/Start.java @@ -0,0 +1,27 @@ +/** + * 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(); + } +} diff --git a/assignment4/Question5/Base.java b/assignment4/Question5/Base.java new file mode 100644 index 0000000..93755c6 --- /dev/null +++ b/assignment4/Question5/Base.java @@ -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); + } +} diff --git a/assignment4/Question5/Course.java b/assignment4/Question5/Course.java new file mode 100644 index 0000000..3c2e303 --- /dev/null +++ b/assignment4/Question5/Course.java @@ -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); + + } +} diff --git a/assignment4/Question5/Learnable.java b/assignment4/Question5/Learnable.java new file mode 100644 index 0000000..a576a6d --- /dev/null +++ b/assignment4/Question5/Learnable.java @@ -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(); +} diff --git a/assignment4/Question5/MajorElective.java b/assignment4/Question5/MajorElective.java new file mode 100644 index 0000000..ee97de6 --- /dev/null +++ b/assignment4/Question5/MajorElective.java @@ -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); + } +} diff --git a/assignment4/Question5/MajorRequired.java b/assignment4/Question5/MajorRequired.java new file mode 100644 index 0000000..f61d922 --- /dev/null +++ b/assignment4/Question5/MajorRequired.java @@ -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); + } +} diff --git a/assignment4/Question5/Start.java b/assignment4/Question5/Start.java new file mode 100644 index 0000000..cd44305 --- /dev/null +++ b/assignment4/Question5/Start.java @@ -0,0 +1,27 @@ +/** + * 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(); + } +} diff --git a/assignment4/Question6/Base.java b/assignment4/Question6/Base.java new file mode 100644 index 0000000..93755c6 --- /dev/null +++ b/assignment4/Question6/Base.java @@ -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); + } +} diff --git a/assignment4/Question6/Course.java b/assignment4/Question6/Course.java new file mode 100644 index 0000000..3c2e303 --- /dev/null +++ b/assignment4/Question6/Course.java @@ -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); + + } +} diff --git a/assignment4/Question6/Learnable.java b/assignment4/Question6/Learnable.java new file mode 100644 index 0000000..a576a6d --- /dev/null +++ b/assignment4/Question6/Learnable.java @@ -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(); +} diff --git a/assignment4/Question6/MajorElective.java b/assignment4/Question6/MajorElective.java new file mode 100644 index 0000000..ee97de6 --- /dev/null +++ b/assignment4/Question6/MajorElective.java @@ -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); + } +} diff --git a/assignment4/Question6/MajorRequired.java b/assignment4/Question6/MajorRequired.java new file mode 100644 index 0000000..f61d922 --- /dev/null +++ b/assignment4/Question6/MajorRequired.java @@ -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); + } +} diff --git a/assignment4/Question6/ManyCourses.java b/assignment4/Question6/ManyCourses.java new file mode 100644 index 0000000..2b2a6b4 --- /dev/null +++ b/assignment4/Question6/ManyCourses.java @@ -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 courses; + + /** + * Constructor. + */ + public ManyCourses() { + courses = new ArrayList(); + } + + /** + * 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(); + } +} diff --git a/assignment4/Question6/Start.java b/assignment4/Question6/Start.java new file mode 100644 index 0000000..5ed186f --- /dev/null +++ b/assignment4/Question6/Start.java @@ -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(); + } +}