Files From Today's Class
Today's Example
In class today, we developed a simple example designed to reinforce what we already know about developing class specification in Java. It was the first time we overloaded a constructor and also the first time we made some use of some minor language features.
Fair Warning
Check the schedule. Notice an interesting event next week!
Notable Items
Today's Example
class Wallet { private String owner; private long amountInCents; public Wallet (String owner) { this.owner = owner; this.amountInCents = 0l; } public Wallet (String owner, long amountInCents) { this.owner = owner; this.amountInCents = amountInCents; } public String getOwner() { return owner; } public long getAmount() { return amountInCents; } public void addMoney(long cents) { amountInCents += cents; } public boolean removeMoney (long cents) { if (amountInCents < cents) return false; amountInCents -= cents; return true; } public boolean isFatter (Wallet w) { if (amountInCents > w.amountInCents) return true; return false; } public String toString() { return owner + ": $" + (amountInCents / 100) + "." + (amountInCents % 100); } public boolean equals(Object o) { Wallet w = (Wallet) o; if (!owner.equals(w.owner)) return false; if (amountInCents != w.amountInCents) return false; return true; } public static void main(String[] args) { Wallet gregsWallet = new Wallet ("Gregory Kesden"); System.out.println (gregsWallet); Wallet forbesWallet = new Wallet ("Steve Forbes", 100); // One dollar System.out.println (forbesWallet); System.out.println (forbesWallet.getOwner() + ": $" + (forbesWallet.getAmount() / 100) + "." + (forbesWallet.getAmount() % 100)); forbesWallet.addMoney (150); System.out.println (forbesWallet); System.out.println (gregsWallet); if (forbesWallet.removeMoney(250)) System.out.println ("Expected success removing from Forbes Wallet."); else System.out.println ("Unxpected failure removing from Forbes Wallet."); if (!forbesWallet.removeMoney(1)) // Notice the !, remember, think NOT System.out.println ("Expected failure removing from Forbes Wallet."); else System.out.println ("Unxpected success removing from Forbes Wallet."); // Test one way if (!forbesWallet.isFatter(gregsWallet)) System.out.println ("Expected: forbeswallet NOT fatter than gregswallet."); else System.out.println ("Unxpected: forbeswallet fatter than gregswallet"); // Test symmetric way if (!gregsWallet.isFatter(forbesWallet)) System.out.println ("Expected: forbeswallet fatter than gregswallet."); else System.out.println ("Unxpected: forbeswallet NOT fatter than gregswallet"); // Test equal (not one greater tha the other) case forbesWallet.removeMoney(250); if (!gregsWallet.isFatter(forbesWallet)) System.out.println ("Expected: gregswallet NOT fatter than forbeswallet."); else System.out.println ("Unxpected: gregswallet fatter than forbeswallet"); if (!forbesWallet.isFatter(forbesWallet)) System.out.println ("Expected: forbeswallet fatter than gregswallet."); else System.out.println ("Unxpected: forbeswallet NOT fatter than gregswallet"); } }