Lab 11 More on OO Programming

advertisement
Lab 11
CS2312 Problem Solving and Programming (2015/2016 Semester B) | www.cs.cityu.edu.hk/~helena
Lab 11 More on OO Programming
(Q1) Tracing
(Q2) Course Reg. System - checking classmate
(Q3) Class Listing
Q1. What is the output of the following program?
[Question borrowed from Dr. Sam NG]
public class Main { public static void main(String [] args) { A a; a = new E(new D(new B())); a.m(); a.print(); } } public abstract class A { private static int value; public abstract void m(); public void print() { System.out.println(value); } protected void add(int v) { value +=v; } } public class B extends A{ public void m() { add(23); } } public abstract class C extends A { protected A comp; public C(A a) { comp = a; } public abstract void m(); } public class D extends C { public D(A a) { super(a); } public void m() { comp.m(); add(31); } } public class E extends C { public E(A a) { super(a); } public void m() { comp.m(); add(46); } } -1-
Lab 11
Q2.
CS2312 Problem Solving and Programming (2015/2016 Semester B) | www.cs.cityu.edu.hk/~helena
[Case study] The ABC University runs a course registration system for managing students'
registrations of courses. Each course is offered from time to time in different semesters. Note that
after a student has taken a course in a semester, he/she may retake the same course in another
semester (ie. another offering of the course).
Your tasks:
1. Draft all classes involved in the case study. (Draw a UML)
2. You are given the main() method like below, which checks whether pairs of students have
been classmates before. Create all classes to complete the program.
public static void main(String[] args) { Student helena = new Student("50123456"); //ID is 50123456 Student kit = new Student("50888999"); Student jason = new Student("50101010"); Course course = new Course("CS2312"); course.addOffering("2013‐14 A"); course.addOffering("2012‐13 B"); helena.takeCourse(course, "2013‐14A"); kit.takeCourse(course, "2012‐13B"); jason.takeCourse(course, "2012‐13B"); System.out.println(helena.hasBeenClassmateOf(kit)); //show boolean: false System.out.println(helena.hasBeenClassmateOf(jason)); //show boolean: false System.out.println(kit.hasBeenClassmateOf(jason)); //show boolean: true
.. Submit your finished program to PASS.
Ref: Lazy but workable step :
With only Main.java in the project,
open Main.java, then choose
Eclipse' suggestion to quickly create
required classes, constructors,
fields, and methods
Note: After you edit, save the file! (Hot key: Ctrl-S, or Alt-F, S)
Array lists are useful for aggregations (e.g. the offerings of each course)
Q3.
Extend your program for Q2 to list the IDs of students in each offering of a course.
(Refer to given Main.java and Rundown.txt)
-2-
Download