Quick Quiz
Please define the minimal Stackable interface compatible with the following Stack class.
class Stack { private int numberStacked; ... public boolean stackItem (Stackable item) { // If the stack isn't too high, add it. if (numberStacked < item.getMaxPerStack()) { item.setPosition(++numberStacked); return true; } // If it is too high, don't return false; } public boolean removeItem (Stackable item) { // Only remove if at top if (item.getPosition() == numberStack) { numberStacked--; item.setPosition (0); return true; } // Don't remove if anywhere else. return false; }The minimal interface is as follows. Please note that we are inferring that the return type of setPosition() is void. It could, in fact, be anything. But, in the code above, the return was never used -- so we'll go with that.
interface Stackable { public int getMaxPerStack(); public void setPosition(int position); public int getPosition(); }
Exam Topics
Exam: What to expect
The bulk of the exam will be implementing a whole class, likely Comparable. But, there will also be some shorter questions designed to test specific things not covered by the "big question", and also to try to give people who might get lost in a big example credit for some of what they know.
Today's Example
We spent most of today discussing the quiz and the exam, but we also built the following example as reinforcement:
class CheckingAccount { private double balance; private int checkNumber; public CheckingAccount(double balance, int startingCheckNumber) { this.balance = balance; this.checkNumber = startingCheckNumber; } public String toString() { return "$" + balance + ", " + checkNumber; } public boolean equals (Object o) { CheckingAccount c= (CheckingAccount) o; if (this.balance != c.balance) return false; if (this.checkNumber != c.checkNumber) return false; return true; } public void deposit (double amount) { balance += amount; } public int writeCheck (double amount) { balance -= amount; return checkNumber--; } public int compareTo (Object o) { CheckingAccount c = (CheckingAccount) o; double balanceDifference = 100*(this.balance - c.balance); if (balanceDifference != 0) return (int) balanceDifference; // You can do this: return (this.checkNumber - c.checkNumber); /* * Or, if you prefer the parallelism, below works, also int checkNumberDifference = (this.checkNumber - c.checkNumber); if (checkNumberDifference != 0) return checkNumberDifference; return 0; */ } }