Skip to content

AY2019 2020 Sem 2 Solutions

colintkn edited this page Nov 27, 2020 · 2 revisions

Question 1 Polymorphism

1
5
3
4
3
1
3
1
5
3
1
3

Question 2 Stack and Heap

Account a1 = new Account("James");
Account a2 = new Account("William");
Transaction t = new Transaction(a1, a2, 37.3);
Bank b = new Bank();
b.doTransfer(t);
Addressee a = new Addressee("John". "13 Computing Drive");
Parcel p = new Parcel(a);
Courier c = new Courier(p);
Depot d = new Depot();
d.outForDelivery(c);
Library lib = new Library();
Book bk = new Book("Book Title", "Sam");
Borrower br = new Borrower("John", 1234);
lib.loanBook(bk, br)

Question 3 Assertions

public double calculateInterest(double balance, double rate) {
    assert balance > 0 : "Insufficient balance";
    assert rate >= 0 && rate <= 1.0 : "Wrong rate";

    ... Interest Calculation Code
    
    assert result <= 50 && result >= 0: "Invalid interest";
}
Clone this wiki locally