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; } }
interface Bank1 { void withdraw (int amount) throws BankAccountError, NotEnoughMoney; } interface Bank2 { void deposit (int amount) throws BankAccountError; }
interface Financial { int HOURLY_RATE = 10; int MAX_CASH = 100; /** * Returns the current amount of money in cash that the person has. */ int amount_in_cash (); /** * Returns the current balance in the person's bank account. */ int amount_in_bank (); void work (int hours); // see below void pay (int amount); // see below }When invoking the method work, a Person earns an amount of money equals to the HOURLY_RATE times the number of hours they work. This money may be kept in cash or in the bank account subject to the constraint that the maximum cash does not exceed MAX_CASH. When invoking the method pay, a Person attempts to find enough money either in cash or in the bank account to pay for the amount needed. If there is not enough money, the Person may have to work first (i.e., invoke the method work) to acquire enough money.
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:
class Person implements Financial { int cash; BankAccount bank; // constructor that does something sensible // four method declarations that match the ones in the interface }This structure is worth 20 points.
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.
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.
sabry@cs.uoregon.edu