Create Xref-Html Frames Remove All Frames
file:/home/jason/teach/226/jason/toilet/AutoToilet.java (Sun Jan 26 21:46:31 2003
)
1: /**
2: * A better Toilet. It automatically cleans up after itself if it
3: * overflows, so it never throws an exception. It also tries to raise
4: * and lower the seat for you, although this turned out to be a bad idea.
5: *
6: * @author Jason Eisner
7: * @version 1.0, 2003-01-26
8: */
9:
10:
11: public class AutoToilet extends Toilet {
12:
13: /** Like {@link Toilet#flush}, but cleans up instead of throwing
14: an exception. */
15:
16: public void flush() {
17: try {
18: super.flush();
19: }
20: catch (FloatingOverflowException e) {
21: System.out.println("Caught exception, cleaning up mess.");
22: System.out.print("Retrying flush: ");
23: this.flush();
24: }
25: }
26:
27: /** Although we inherit the general {@link Toilet#deposit(Waste)} method
28: * from our parent class, we also override it with this more specific
29: * method for <i>liquid</i> waste. The method automatically raises and lowers
30: * the seat for you when you pee. Alas, tests show dissatisfaction
31: * among female users.
32: * @param lw The liquid waste
33: */
34: public void deposit(LiquidWaste lw) {
35: raiseSeatAuto(true);
36: deposit((Waste) lw); // call the general method; alternatively, super.deposit(lw)
37: raiseSeatAuto(false);
38: }
39:
40: /** Since this kind of toilet has an automatic seat raiser, the user
41: * can't move the seat. So we override the inherited <code>raiseSeat</code>
42: * method with one that has no effect. (We do print a warning message;
43: * alternatively, we could throw an exception.)<p>
44: *
45: * Really we shouldn't even be able to call <code>raiseSeat</code>
46: * at all on an {@link AutoToilet}. Unfortunately, by defining a
47: * <code>raiseSeat</code> method for {@link Toilet}, we have told
48: * the compiler that it's legal to call <code>raiseSeat</code> on
49: * any {@link Toilet} object, including subclasses. So we'd have
50: * to change that -- i.e., only some subclasses of {@link Toilet} should
51: * support this method.
52: */
53:
54: public void raiseSeat(boolean b) { // overrides general method.
55: System.out.println("Thanks for trying to adjust the seat, but it's not necessary for this fancy toilet.");
56: }
57:
58: /** Internally, the class's implementation will call this
59: * protected method to move the seat. The user can't call
60: * this.
61: */
62: protected void raiseSeatAuto(boolean b) {
63: System.out.print("Whirr ... creak ... "); // sound of the motor
64: super.raiseSeat(b); // call superclass method, as if user had moved seat
65: }
66:
67: /** Test function similar to {@link Toilet#main}. */
68:
69: public static void main(String args[]) { // doesn't throw anything
70: AutoToilet throne = new AutoToilet();
71:
72: throne.raiseSeat(true); // unnecessary now
73:
74: throne.deposit(new LiquidWaste("pee")); // auto raises/lowers seat
75: (new LiquidWaste("more pee")).deposit(throne); // auto raises/lowers seat
76:
77: throne.deposit(new SolidWaste("poo")); // no auto-flush in this version
78: throne.deposit(new SolidWaste("more poo"));
79: throne.flush();
80:
81: throne.deposit(new TrashWaste("cigarettes"));
82: throne.flush(); // auto-cleans if overflow
83:
84: System.out.println("All done. No need to destroy toilet; it will be garbage-collected.");
85: }
86: }
87:
Html form generated by Xref-Java2Html version 1.4.0 on Mon Jan 27 13:59:15 2003