SYSC2004_Midterm_C_Solns
SYSC2004_Midterm_C_Solns
SECTION 1 (…/10)
Q1 –Which of the following errors is a run-time error in Java?
a) The program has an expression with a division by zero
b) The program violates the rules of Java
c) The program does not produce the expected result
d) All of the above are run-time errors.
a) **/
b) //
c) /
d) */
Page 1 of 6
Q6 – Assume all the required classes are implemented, and the default constructor is available on all classes. How
many objects are in the following program after #line A is executed?
public class Client{
public static void main (String[] args ){
Die d1 = new Die();
Die d2 = new Die();
Die d3 = d2;
d1 = d2;
Player p1 = new Player();
Player p2 = new Player();
Player p3 = p2;
Game g = new Game(); #Line A
}
}
a) 3
b) 4
c) 5
d) 6
e) 7
Q8 – An attribute declared as protected can be accessed from within the class where it is declared and also from:
a) Only classes in the same package
b) Only classes in the same package, and the superclasses of the class where it is declared
c) Only classes in the same package, and the subclasses of the class where it is declared
d) Only the superclasses of the class where it is declared
e) Only the subclasses of the class where it is declared
Q9 – Which convention does Java follow when passing primitive data types as input parameters to methods?
a) Pass-by-value
b) Pass-by-reference
Page 2 of 6
SECTION 2: getCost (/4)
// Javadoc 1.5 broken down as per below
/** // 0.2
* getCost returns the cost of the customer's order, or 0 if the
* customer hasn't started an order. // 0.3: must include method name
* (We assume the parameters are not null.) // not required in the Javadoc
*
* @param customer The customer adding to an order
// 0.5 (tag (0.2), name of the parameter (0.15), explanation (0.15))
* @return The cost of the customer's order
// 0.5 (tag (0.2), data type not included (0.15), explanation (0.15))
*/
public double getCost(Customer customer) { // 0.5
ArrayList<Product> products = orders.get(customer); // get products // 0.5
if (products==null) { // 0.25
return 0; // customer doesn't have an order
}
double cost = 0;
for (Product product: products) { // walk through all products // 0.5: for each required, max of 2 otherwise
cost += product.getCost(); // 0.5
}
return cost; // initializing and returning cost 0.25
}
Page 3 of 6
public boolean addProduct(Customer customer, Product product) { // out of 1.5; 0.5 for signature
ArrayList<Product> products = orders.get(customer); // 0.25
if (products==null) { // 0.25
return false;
}
products.add(product); // 0.5 for the add and returning true
orders.put(customer,products); // not required but no deduction if put or replace used here
return true;
}
public boolean removeProduct(Customer customer, Product product) { // out of 4; 0.5 for signature
ArrayList<Product> products = orders.get(customer); // 0.25
if (products==null) { // 0.25
return false;
}
Iterator<Product> iter = products.iterator(); // 0.5: if they do not use iterator, the max grade is 2
while (iter.hasNext()) { // 0.5
if (iter.next().equals(product)) { // 1: must use equals
iter.remove(); // 0.5
return true; // 0.25
}
}
return false; // 0.25
}
}
Page 4 of 6
SECTION 4: UML (/6)
6 marks.
(.../0.5) - Four classes with correct names
Relationships (…/1.5):
(.../0.5) - Inheritance relationship between Business and Retailer (0.25 correct arrow, 0.25 correct direction)
(.../0.5) - Aggregation relationship between Product and Retailer (0.25 correct arrow, 0.25 correct direction)
(.../0.5) - Aggregation relationship between Customer and Retailer (0.25 correct arrow, 0.25 correct direction)
Note: No deduction for using composition instead of aggregation for both of the above, i.e. solid diamonds, or
for using association instead of aggregation. For association the arrow will be a regular arrow with a “v”
arrowhead, but in the opposite direction (i.e. the “v”s next to Product and Customer).
-0.25 marks for each extra arrow. 0/1.5 if there are arrows between all classes.
Page 6 of 6