Restore
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
|
||||
<attributes>
|
||||
<attribute name="module" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Question6</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,2 @@
|
||||
eclipse.preferences.version=1
|
||||
encoding/<project>=UTF-8
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,30 @@
|
||||
|
||||
public class Base implements Learnable{
|
||||
//instance variables
|
||||
private String code;
|
||||
private String title;
|
||||
|
||||
//constructor
|
||||
public Base(String code, String title) {
|
||||
this.code = code;
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
//methods
|
||||
@Override
|
||||
public String getCode() {return this.code;}
|
||||
|
||||
@Override
|
||||
public String getTitle() {return this.title;}
|
||||
|
||||
@Override
|
||||
public Base getPreRequisite() {return Base.this;}
|
||||
|
||||
//test
|
||||
public static void testBase() {
|
||||
Base b = new Base("DS1001", "EntryCourse");
|
||||
System.out.println(b.getCode() == "DS1001");
|
||||
System.out.println(b.getTitle() == "EntryCourse");
|
||||
System.out.println(b.getPreRequisite() == b);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
|
||||
public abstract class Course implements Learnable{
|
||||
//instance variables
|
||||
private String code;
|
||||
private String title;
|
||||
private Learnable preRequisite;
|
||||
|
||||
//constructor
|
||||
public Course(String code, String title, Learnable preRequisite) {
|
||||
//test if preRequisite satisfied the condition
|
||||
if(preRequisite != null && (preRequisite instanceof MajorRequired || preRequisite instanceof Base)) {
|
||||
this.code = code;
|
||||
this.title = title;
|
||||
this.preRequisite = preRequisite;
|
||||
}
|
||||
else {
|
||||
System.out.println("pre-requisite course cannot null!");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//methods
|
||||
@Override
|
||||
public String getCode() {return this.code;}
|
||||
|
||||
@Override
|
||||
public String getTitle() {return this.title;}
|
||||
|
||||
@Override
|
||||
public Learnable getPreRequisite() {return this.preRequisite;}
|
||||
|
||||
public abstract boolean isRequired();
|
||||
|
||||
//test
|
||||
public static void testCourse() {
|
||||
// Since we cannot create any Course object, the testCourse method of the Course class must be empty.
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
public interface Learnable {
|
||||
public String getCode();
|
||||
public String getTitle();
|
||||
public Learnable getPreRequisite();
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
|
||||
public class MajorElective extends Course{
|
||||
//constructor
|
||||
public MajorElective(String code, String title, Learnable preRequisite) {
|
||||
super(code, title, preRequisite);
|
||||
}
|
||||
|
||||
//method
|
||||
@Override
|
||||
public boolean isRequired() {return true;}
|
||||
|
||||
//test
|
||||
public static void testMajorElective() {
|
||||
// Create a Base object
|
||||
Base b = new Base("DS1001","EntryCourse");
|
||||
// Create a first MajorRequired object with the base course as pre-requisite
|
||||
MajorRequired mr1 = new MajorRequired("DS200X","OOP",b);
|
||||
// Create a MajorElective object with the major required course as pre-requisite
|
||||
MajorElective me1 = new MajorElective("DS300X","Data Mining",mr1);
|
||||
// Test mr1
|
||||
System.out.println(mr1.getCode() == "DS200X");
|
||||
System.out.println(mr1.getTitle() == "OOP");
|
||||
System.out.println(mr1.getPreRequisite() == b);
|
||||
// Test mr2
|
||||
System.out.println(me1.getCode() == "DS300X");
|
||||
System.out.println(me1.getTitle() == "Data Mining");
|
||||
System.out.println(me1.getPreRequisite() == mr1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
|
||||
public class MajorRequired extends Course{
|
||||
//constructor
|
||||
public MajorRequired(String code, String title, Learnable preRequisite) {
|
||||
super(code, title, preRequisite);
|
||||
}
|
||||
|
||||
//method
|
||||
@Override
|
||||
public boolean isRequired() {return true;}
|
||||
|
||||
//test
|
||||
public static void testMajorRequired() {
|
||||
// Create a Base object
|
||||
Base b = new Base("DS1001","EntryCourse");
|
||||
// Create a first MajorRequired object with the base course as pre-requisite
|
||||
MajorRequired mr1 = new MajorRequired("DS200X","OOP",b);
|
||||
// Create a second MajorRequired object with the first major required course as pre-requisite
|
||||
MajorRequired mr2 = new MajorRequired("DS300X","Data Mining",mr1);
|
||||
// Test mr1
|
||||
System.out.println(mr1.getCode() == "DS200X");
|
||||
System.out.println(mr1.getTitle() == "OOP");
|
||||
System.out.println(mr1.getPreRequisite() == b);
|
||||
// Test mr2
|
||||
System.out.println(mr2.getCode() == "DS300X");
|
||||
System.out.println(mr2.getTitle() == "Data Mining");
|
||||
System.out.println(mr2.getPreRequisite() == mr1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ManyCourses {
|
||||
//instance variable
|
||||
private ArrayList<Learnable> courses;
|
||||
|
||||
//constructor
|
||||
public ManyCourses() {
|
||||
this.courses = new ArrayList<Learnable>();
|
||||
}
|
||||
|
||||
//methods
|
||||
public void addCourse(Learnable c) {
|
||||
this.courses.add(c);
|
||||
}
|
||||
|
||||
public void listCourses() {
|
||||
for(int i = 0; i < courses.size(); ++i) {
|
||||
Learnable c = (Learnable) courses.get(i); //downcast from object to Learnable
|
||||
System.out.println(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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
|
||||
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