All code given below is free of compilation errors. ... Your task: Consider the following statements one by one. ... main

advertisement
CS2312 Problem Solving and Programming [2014-15 Semester B]
Quiz 2
Write your answers for Q1-4 in the answer paper.
Pay attention to code design, naming and code formatting.
Duration: 40 minutes. Total mark: 40 marks
Q1: 8 marks
Q2: 10 marks
Q3: 22 marks
Q4: 0.1 marks
Question 1 (8 marks)
All code given below is free of compilation errors. Upon execution, statements S1 and S2 of the
main method can successfully create 2 objects which are referred by a and b respectively.
public class A
{
public void m1() {}
}
public class B extends A
{
public void m2() {}
}
public class Main
{
public static void main(String [] args)
{
A a = new A();
//S1
B b = new B();
//S2
One statement to be filled
//S3
}
}
Your task: Consider the following statements one by one. Which one(s) would cause
compilation error or runtime error if it is inserted into the blank as statement S3? Explain.
i.
ii.
iii.
iv.
v.
vi.
vii.
viii.
a.m1();
a.m2();
b.m1();
b.m2();
A ta = b;
B tb = a;
A ta = ((A)b);
B tb = ((B)a);
Question 2 (10 marks)
(7 marks)
(i) What are the outputs from statements I-V below?
public class A {
public int value=500;
public class B extends A {
public int value=300;
public int getValue() {
return value;
}
public int getValue() {
return value;
}
public int algorithm() {
return this.getValue()-this.value;
}
public int algorithm() {
return super.algorithm()*2;
}
}
}
public class Main {
public static void main(String [] args)
{
A x = new B();
System.out.println(x.value); //Statement I
System.out.println(((B)x).value); //Statement II
System.out.println(x.getValue()); //Statement III
System.out.println(((B)x).getValue()); //Statement IV
System.out.println(x.algorithm()); //Statement V
}
}
(ii) Using any statement from the above program as an example, explain how dynamic binding happens.
Note: marks will be - given based on how good you express.
deducted for any incorrect wording.
(3 marks)
Question 3 (22 marks)
Complete the following program by implementing the Product class and the Customer class.
Explanation and Requirements:
-
The program creates objects for customers and products (classes Customer, and Product).
A customer will buy a promoted product if he has not bought the product yet, and if he has enough money.
-
Each customer has an amount of money initially, and each product has a fixed price.
-
For simplicity, int is used to handle money amount and pricing.
-
You should use an Arraylist to keep the products which have been bought by a customer.
You should apply for-each loop whenever it is appropriate.
-
The main program and output are given below:
public class Main {
public static void main(String [] args)
{
Customer c1 = new Customer(200); //has $200
Customer c2 = new Customer(150); //has $150
Product p1 = new Product(80);
Product p2 = new Product(100);
//default price is $80
//default price is $100
c1.shop(p1); //Output: OK. I buy the product. I have $120 left.
c1.shop(p1); //Output: No. I have got one already.
c1.shop(p2); //Output: OK. I buy the product. I have $20 left.
c2.shop(p2); //Output: OK. I buy the product.
c2.shop(p1); //Output: No. I am out of money.
I have $50 left.
}
}
Output:
OK. I buy the product.
I have $120 left.
No. I have got one already.
OK. I buy the product.
I have $20 left.
OK. I buy the product.
I have $50 left.
No. I am out of money.
Question 4 (+0.1 for Q4 (partial mark up to 0.05 may be given for partial correctness /
[Question is given in P.1 of the answer paper.]
improper style )
CS2312 Problem Solving and Programming [2014-15 Semester B]
Quiz 2 [Answer paper]
Student ID No: ______________ (Seat Number: ______)
Question 1 (8 marks)
Which statement(s) will cause compilation error? List and briefly explain:
Which statement(s) will cause runtime error? List and briefly explain:
[Q1-3 only] Drawing for extra bonus of up to 2 mark per question; applicable only if the total doesn't sum
up beyond half of the total mark of that question.
Student ID No: ______________ (Seat Number: ______)
Question 2 (10 marks)
(i) What are the outputs from statements I-V?
I: ________
V: ________
II: ________
III: ________
IV: ________
(4 marks)
(3 marks)
(ii) Using any statement from the above program as an example, explain how dynamic binding happens.
Note: marks will be - given based on how good you express.
deducted for any incorrect wording.
(3 marks)
[Q1-3 only] Drawing for extra bonus of up to 2 mark per question; applicable only if the total doesn't sum
up beyond half of the total mark of that question.
Question 3 (22 marks)
-
Implement the Product class below (8 marks)
Implement the Customer class on next page. (14 marks)
public class Product {
(8 marks)
[Q1-3 only] Drawing for extra bonus of up to 2 mark per question; applicable only if the total doesn't sum
up beyond half of the total mark of that question.
Student ID No: ______________ (Seat Number: ______)
(14 marks)
Note:
- You should use an Arraylist to keep
the products which have been bought
by the customer.
- You should apply for-each loop
whenever it is appropriate.
import java.util.ArrayList;
public class Customer {
-- end of paper --
Total:
____ / 40
Download