Document 11492577

advertisement
[2013-14 Semester B]
CS2312 Problem Solving and Programming
Quiz 2
Student ID: ______________ (Seat Number: ______)
Please complete all three questions with Java code. Duration: 40 minutes. Total mark: 40 marks
Q1. [10 marks] Simple Class
Complete the code in the Flower class below, such that the program will show the output as required (listing of
created flowers and the count). Do not change the main() method. Your answer should follow proper class
design and good programming practices. Add the override annotation where appropriate.
(However, you do not need to add comments.)
public class Flower { private String type; private String color; } public static void main(String[] args) { Flower f1 = new Flower("rose", "red"); Flower f2 = new Flower("lily", "white"); System.out.println(f1); System.out.println(f2); System.out.println("Current total: "+Flower.getTotal()); Flower f3 = new Flower("lily", "pink"); System.out.println(f3); System.out.println("Current total: "+Flower.getTotal()); } (10 Marks)
Output:
red rose
white lily
Current total: 2
pink lily
Current total: 3
Q2. [17 marks] Inheritance
Create four classes: Assessment, Exam, Quiz, Project, which model the assessment components
of a course. Also complete the main() method so that it lists the components and calculates the total
weight (which may not be 100), as shown in the output below.
Below are data stored for each component:
Exam: weight (e.g. 80), exam duration (eg. 2.0 hours)
Quiz: weight (e.g. 20), quiz duration (e.g. 1.5 hours), when (ie. which week's lecture, e.g. "Week 4")
Project: weight (e.g. 30), title (e.g. "Library System"), group work (true: group work, false: individual)
The code written by you should fullfill all requirements stated above. No hardcoding is allowed. E.g., you
should NOT write code like this: System.out.println("Total weight: 150"); You should also follow proper OO design and good programming practices. Add the override annotation
where appropriate. (However, you do not need to add comments.)
(7 Marks)
public static void main(String[] args) { ArrayList<Assessment> ass = new ArrayList<Assessment>(); ass.add(new Exam(80, 2.0)); ass.add(new Quiz(20, 1.5, "Week 4")); ass.add(new Quiz(20, 1.5, "Week 8")); ass.add(new Project(30, "Library System", false)); //Your task: add a loop to go through each component // list them and calculate total weight // (add variables where appropriate) Output:
} Exam (80)
Week 4 Quiz (20)
Week 8 Quiz (20)
Project: Library System (30)
Total weight: 150
Student ID: ______________ (Seat Number: ______)
(5 Marks)
(5 Marks)
(0 Marks ‐ do this part if you have nothing else to do )
(0 Marks ‐ do this part if you have nothing else to do )
Output of the main() method:
Exam (80)
Week 4 Quiz (20)
Week 8 Quiz (20)
Project: Library System (30)
Total weight: 150
Q3. [13 marks] Polymorphism and Dynamic Binding - Read the code below and answer all questions.
class C1 { protected String v1="x"; protected static String v2="x"; public C1(String v) {v1=v1+v;v2=v2+v;} public void output1() {System.out.println(v1+v2);} public static void output2() {System.out.println(v2);} public void go1() {output1();} } class C2 extends C1 { protected String v1="#"; protected static String v2="#"; } public C2(String v) {super(v);v1=v+v1;v2=v+v2;} public void output1() {System.out.println("("+v1+v2+")");} public static void output2() {System.out.println("("+v2+")");} public void go2() {output2();} 
public static void main(String[] args) { C1 a = new C2("Car"); //statement S1 C1 b = new C2("Dog"); //statement S2 a.output1(); //statement S3 b.output2(); //statement S4 C1 b1=______b______; //statement S5 - make b1 reference what b is referencing
C2 b2=______b______; //statement S6 - make b2 reference what b is referencing
________b2_______.go1(); //statement S7 ________b1_______.go2(); //statement S8 } (a)Draw all fields and objects which appear when the
program executes up to  (ie.. after running S1 and S2)
(b) Output of S3 :______________________
(c) Output of S4 :______________________
(d) Add necessary but minimal code to S5-S8
to make the program free of compilation
error.
(Do not delete any existing code)
(e) Output of S7 :______________________
(f) Output of S8 :______________________
(b‐f: 8 Marks)
(5 Marks)
Total mark: _____ (/40)
Download