Solution for Quiz I

I am including my solutions and the guidelines I used to grade your problems. If you feel I didn't grade you according to these guidelines, please stop by.

Problem I

Write two different interfaces that the following class implements:
class BankAccount implements Bank1, Bank2 {
    private int balance;
    
    public BankAccount (int balance) {
	this.balance = balance;
    }

    public int getBalance () { return balance; }

    public void withdraw (int amount) throws BankAccountError, NotEnoughMoney {
	if (amount <= 0) throw new BankAccountError();
	else if (amount <= balance) balance -= amount;
	else throw new NotEnoughMoney(amount-balance);
    }

    public void deposit (int amount) throws BankAccountError {
	if (amount <= 0) throw new BankAccountError();
	else balance += amount;
    }
}

Solution

For a class to implement an interface, it must have an implementation of every method in the interface. Variable declarations and constructors cannot appear in interfaces. The class BankAccount has three methods: put some method declarations in one interface and some other method declarations in the other interface. For example,
interface Bank1 {
  void withdraw (int amount) throws BankAccountError, NotEnoughMoney;
}

interface Bank2 {
  void deposit (int amount) throws BankAccountError;
}


Problem II

Design and implement a class to represent a Person using the following guidelines:

Solution

There is quite a bit of freedom in how you would implement. Here is one solution:
 
class Person implements Financial {
    private BankAccount account;
    private int cash;

    public Person () {
	this.account = new BankAccount(0);
	cash = 0;
    }

    public int amount_in_cash () { 
	return cash; 
    }

    public int amount_in_bank () {
	return account.getBalance();
    }

    public void work (int hours) {
	try {
	    cash += HOURLY_RATE * hours;
	    if (cash > MAX_CASH) {
		account.deposit(cash - MAX_CASH);
		cash = MAX_CASH;
	    }
	}
	catch (BankAccountError e) {
	}
    }

    public void pay (int amount) {
	if (cash > amount) cash -= amount;
	else {
	    try {
		account.withdraw (amount - cash);
		cash = 0;
	    }
	    catch (NotEnoughMoney e) {
		int amountNeeded = e.getAmount();
		work ((amountNeeded / HOURLY_RATE) + 1);
		pay (amountNeeded);
	    }
	    catch (BankAccountError e) {
	    }
	}
    }
}
Here is how I graded this problem. First I don't really care about the details of how the money is divided between cash and bank. The important points were:


Problem III

Consider the following class:
class Lunch {
    Food salad, entree, dessert;

    Lunch () {
        salad = new GreenSalad();
        entree = new Pizza();
        dessert = new IceCream();
    }

    int price () {
        return salad.price() + entree.price() + dessert.price();
    }
}
The class makes use of four other classes: Food, GreenSalad, Pizza, and IceCream. Write definitions for these four classes that would make everything compile and run correctly.

Solution

abstract class Food {
    abstract int price ();
}

class GreenSalad extends Food {
    int price () { return 3; }
}

class Pizza extends Food {
    int price () { return 9; }
}

class IceCream extends Food {
    int price () { return 2; }
}
I divided the 30 points as follows. Having a hierarchy is worth 10 points. Having a method price in each class is worth 10 points. The details of the code are worth the remaining 10 points.


Visited times since March 23, 1999 (or the last crash).

sabry@cs.uoregon.edu