Student ID : ________________________ Course code & title

advertisement
Student ID : ________________________
Seat Number : ________________________
CITY UNIVERSITY OF HONG KONG
Course code & title
:
CS2312 Problem Solving and Programming
Session
:
Semester B 2013-14
Time allowed
:
Two (2) Hours
This paper has 15 pages (including this page).
Note: 1. Answer ALL 5 questions in the space provided within each question.
Question
1
2
3
4
5
Total
100
Marks
Max
20
20
20
20
20
Corr. CILOs
#1,3
#2,3,5
#3,4
#1,3
#2,3
2. Apply best practices and skills of object-oriented programming when you answer
the questions.
3. Do NOT remove the staple or separate the paper.
4. This question paper should NOT be taken away.
Remarks:
For all written code required by the questions, you should give JAVA code with proper
programming styles, in particular, appropriate naming and code formatting.
This is a closed-book examination.
No materials or aids are allowed during the whole examination. If any unauthorized
materials or aids are found on a candidate during the examination, the candidate will be
subject to disciplinary action.
Student ID: ____________________________
Answer all questions in this paper
Question 1 ( 20 marks)
(a) (i) Which statement(s) inside the main method is/are invalid? Explain briefly.
(2 marks)
class C1 {}
class C2 extends C1 {} public class Main { public static void main(String[] args) { C1 a = new C2(); //Statement S1 C2 b = new C1(); //Statement S2 } } Your answer: (ii) Add the class C2 below with minimal code such that the whole program is free of error (i.e., free of
(3 marks)
compile-time and run-time error) .
interface F { } abstract class C1 { public void m() { C1 a = new C2(); } } public class Main { public static void main(String[] args) { F x = new C2(); } } 2
Nice! and Good!
(b) Complete the code below without adding any field, such that the program will output: Good! and Nice!
class C1 { (5 marks)
protected String m; public C1(String s) {m=s;} } class C2 extends C1 { private String m; public void say() { System.out.printf("%s! and %s!\n", ___________,m); } } public class Main { public static void main(String[] args) { C2 a = new C2("Nice","Good"); C2 b = new C2("Good","Nice"); a.say(); //output "Nice! and Good!" b.say(); //output "Good! and Nice!" } } (c) Read the given code and answer the following question.
Can the following operations be performed at
position (A)? If yes, write down the code and
the program output. If no, state the reason.
(i) Invoke the method say1() on obj:
(5 marks)
class C1 {
private String m=""; public void say1() { System.out.println(m); } } class C2 extends C1 { private String m="Nice"; (ii) Invoke the method say2() on obj:
public C1(){ m="OK"; } public void say2() { System.out.println(m); } } public class Main { }
public static void main(String[] args) { C1 obj = new C2(); (A) } 3
Student ID: ____________________________
(d) Read the given code and
answer the following
questions.
(5 marks)
(i) Statement A creates an
object. What are the
field(s) and value(s)
contained in the object?
(ii) Statement B attempts to
run the say() method. Is it
valid and can be executed?
If yes, write down the
output. If no, state the
reason.
Your answers:
class C1 {
private int m=100; public int get_m() { return m; } public int get_mHalf() { return get_m()/2; } public void say() { System.out.println(get_m()); } } class C2 extends C1 { private int m=500; public int get_m() { return m; } public void say() { System.out.println(get_mHalf()); } } public class Main { public static void main(String[] args) { C1 a = new C2(); // Statement A a.say(); // Statement B }
(i)
(ii)
Question 2 ( 20 marks)
(a) The main() method below creates the objects for two customers who would like to make loans, and the
bank is to approve/disapprove them.
Study the given code in main() and the program output below, as well as the Customer and the Bank
classes on next page, add all missing code in Customer and Bank. You should implement the Bank class
using the Singleton Pattern. The approval of loans must be conducted through calling the approve method
of the Bank class.
public class Main { public static void main(String[] args) { Customer customer1=new Customer("C801"); //customer ID is "C801" Customer customer2=new Customer("C802"); //customer ID is "C802" customer1.applyLoan(Bank.getInstance(),5000); //loan amount is $5000 customer2.applyLoan(Bank.getInstance(),15000); //loan amount is $15000 } Output: } C801: Approved C802: Disapproved
4
class Customer { private String id; private int loan; } (9 marks)
class Bank { ________________________________int LIMIT=10000; public void approve(Customer customer, int amount) { if (amount < LIMIT) { customer.setLoan(amount); System.out.println(__________________________": Approved"); } else { System.out.println(__________________________": Disapproved"); } } } (8 marks)
5
Student ID: ____________________________
(b) Explain how your code for the Bank class achieves the purpose of the Singleton Pattern.
(3 marks)
Question 3 ( 20 marks)
(a) The main() method below lists an array of song names in 3 styles. Complete all missing code (methods and
classes) on next page, such that it will produce the following output.
Note:
- Do not add any field in the Lister class which is given on next page.
- The output method of Lister is final, that means it cannot be overridden in subclasses.
public class Main { public static void main(String[] args) { String [] songs={"All Bells in Paradise", "How Beautiful", "Look at the world"}; Lister lister1,lister2,lister3; lister1= new Lister(songs); lister2= new BulletLister(songs); lister3= new NumberLister(songs); lister1.output(); lister2.output(); Output:
lister3.output(); All Bells in Paradise } How Beautiful } Look at the world * All Bells in Paradise * How Beautiful * Look at the world 1 All Bells in Paradise 2 How Beautiful 3 Look at the world 6
class Lister { private String[] arrItems; public final void output() { for (int i=0;i<arrItems.length;i++) System.out.println(getSymbol(i)+arrItems[i]); } (15 marks)
7
Student ID: ____________________________
(b) Explain how dynamic binding occurs when the program runs, in not more than 60 words.
(5 marks)
Question 4 ( 20 marks)
The program below models Boxes and Items, such that if a box is nonempty, then it contains a thing which is
either an inner box or an item.
The code for the Thing interface
(You will write in part (b))
class Box implements Thing { private Thing thing; private String title; public Box(String t) {title=t;} public void store(Thing t) {thing=t;} public void display(String header) { System.out.println(header+title+"Box"); if (thing!=null) thing.display(header+" "); //" " is a string of 2 spaces } } class Item implements Thing { int itemCode; public Item(int code) {itemCode=code;} public void display(String header) {System.out.println(header+"Item" +itemCode);} } public class Main { public static void main(String[] args) { Box b1=new Box("Small"); Box b2=new Box("Medium"); Box b3=new Box("Big"); b1.store(new Item(801)); b2.store(b1); b3.store(b2); b1.display(""); System.out.println(); b2.display(""); System.out.println(); b3.display(""); System.out.println(); } } 8
(a) Give the output of the program. (Suggestion: Draw the objects! You may get some marks for the drawing even if you give wrong answer for the output.)
(6 marks)
(b) Write the Thing interface. (4 marks)
9
Student ID: ____________________________
(c) Add a class called Rack to model
racks.
A rack has a title like a box. It
can store a variable number of
things, which can be inner racks,
boxes, and items, which are kept
in an array‐list in the Rack object.
The usage of Rack is illustrated
in the modified main() method
on the right.
You should apply for‐each
loop where appropriate. import java.util.ArrayList; class Rack implements Thing { } public static void main(String[] args) { Box b1=new Box("Small"); Box b2=new Box("Medium"); Box b3=new Box("Big"); b1.store(new Item(801)); b2.store(b1); b3.store(b2); Rack r1=new Rack("Short"); Rack r2=new Rack("Tall"); r1.store(new Item(802));
// An item is added into rack r1 r2.store(r1); // Rack r1 is added into r2 r2.store(b3); // Box b3 is added into r2 r2.store(new Box("Tiny")); r2.store(new Item(803)); r2.display(""); //Displays "TallRack", then each thing in the rack.
// ie. each inner rack, box (can be an inner box) ,
// and item (can be outside or inside any box)
//Output order and format are not cared.
} (10 marks)
10
Question 5 ( 20 marks)
Consider an application which handles annual leave requests in a company. The initial leave balance for each staff
member is 14 days. Upon making a leave request, the personnel office will check whether the requested leave
duration is not over the balance, and decide whether to forward the request for approval, or to reject the request.
On forwarding to approval, the leave balance of the staff member will be deducted immediately. (The balance will
be restored if it is then disapproved by the supervisor.)
(a) Complete the Staff class below with all these features:
-
A field, named id, of the String type to keep a staff ID
-
A field, named bal, of the int type to keep the leave balance, which should be initiated as 14 in the
Staff constructor.
-
The constructor which accepts only one parameter for the staff ID.
-
An accessor method for bal
-
A public void method , named deductBalance, for deducting the balance by a given amount which is
input through a parameter.
public class Staff { The createLeaveApplication method
(You will write in part (b))
} (4 marks)
11
Student ID: ____________________________
(b) Refer to the given PersonnelOffice class and the main() method for testing, add the
createLeaveApplication method in the Staff class and implement the LeaveApp class for leave
applications. You should also add necessary interface/classes to model the status of a leave application using
the State Pattern.
public class PersonnelOffice //The personnel office
{ public void process(LeaveApp la) { if (la.balanceEnough()) { System.out.println("Forward for approval"); la.proceed(); } else { System.out.println("Invalid (requested leave > balance)"); la.rejected(); } } } public class Main //for testing { public static void main(String[] args) { Staff w=new Staff("S001"); PersonnelOffice po = new PersonnelOffice(); po.process(w.createLeaveApplication("01-Mar-2014",3));//output "Forward for approval"
po.process(w.createLeaveApplication("11-Apr-2014",7));//output "Forward for approval"
po.process(w.createLeaveApplication("21-May-2014",6));//output "Invalid (requested leave > balance)"
po.process(w.createLeaveApplication("12-Jun-2014",4));//output "Forward for approval"
po.process(w.createLeaveApplication("31-Jul-2014",3));//output "Invalid (requested leave > balance)"
}
} public class Staff { The code you wrote in par t (a)
(Do not write again)
//Your task: Write the createLeaveApplication method below
} (3 marks)
12
public class LeaveApp { } (10 marks)
13
Student ID: ____________________________
(3 marks)
// Write the interface/classes to model the status of a leave application using the State Pattern:
14
Download