Create Xref-Html Frames                    Remove All Frames
file:/home/jason/teach/226/jason/toilet/Toilet.java        (Mon Jan 27 05:42:13 2003 )


   1: /**
   2:  * This class approximates the standard interface to a physical
   3:  * toilet.  The current implementation is a <i>stub</i> that mostly
   4:  * just prints stuff.  We could replace it later with a more detailed
   5:  * implementation that manipulates the chain, the siphon jet, the ball
   6:  * float, the filler valve, etc.  All of these would be handled with
   7:  * <b>private</b> methods and variables, because the user shouldn't
   8:  * have to know how the toilet works in order to use it.
   9:  *
  10:  * @author Jason Eisner
  11:  * @version 1.0, 2003-01-26
  12:  */
  13: 
  14: public class Toilet {
  15: 
  16:   /** Whatever's in the toilet. */
  17:   protected Waste contents;  // starts out as null, i.e., empty
  18: 
  19:   /** True iff the seat is up.  Can be changed but isn't really used yet. */
  20:   protected boolean seatup;  // starts out as false
  21: 
  22:   /** Probability that the toilet will clog and overflow if you try to
  23:     * flush TrashWaste.  This is a static field, so it is shared by
  24:     * all toilets - it cannot vary from toilet to toilet.  It is also
  25:     * a final field, i.e., a constant that can't be changed or overridden. 
  26:     */
  27: 
  28:   private static final float CLOG_PROBABILITY = 0.5F;  // static, so shared by all Toilets
  29:   private static java.util.Random random = new java.util.Random();  
  30: 
  31:   // We have no special constructor - so we'll get a 0-argument
  32:   // constructor Toilet() by default.
  33:   
  34:   /** Place something in the toilet.
  35:    * @param w The waste to deposit.
  36:    */
  37: 
  38:   public void deposit(Waste w) {
  39:     if (isYucky()) 
  40:       System.out.print("Yuck ... should have flushed first ... oh well here goes ... ");
  41: 
  42:     contents = w;   // simply replace old contents - not realistic
  43:     System.out.println("Splash!  " + w);  
  44:     // Java treats the argument as "Splash!  " + w.toString()
  45:   }
  46: 
  47:   /** Does the toilet need flushing? 
  48:    *  @return <code>true</code> if it does, <code>false</code> otherwise. 
  49:    */
  50:   public boolean isYucky() {
  51:     return (contents instanceof SolidWaste);
  52:   }
  53: 
  54:   /** Make the contents of the toilet (if any) go away. 
  55:    * @throws FloatingOverflowException If the toilet happened to clog.  
  56:    *         In this case it needs to be reflushed (after you clean up the
  57:    *         bathroom), although it might clog again.
  58:    */
  59:   public void flush() throws FloatingOverflowException {
  60:     System.out.print("Pretending to execute complicated flush procedure ... ");
  61: 
  62:     if (contents instanceof TrashWaste 
  63:         && random.nextFloat() < CLOG_PROBABILITY) {
  64:       System.out.print("Overflowed " + contents + " all over the place ... ");
  65:       throw new FloatingOverflowException();
  66:     }
  67:     contents = null;
  68: 
  69:     System.out.println("all gone");
  70:   }
  71: 
  72:   /** Change the seat position.
  73:    * @param b true to raise it, false to lower it.
  74:    */
  75:   public void raiseSeat(boolean b) {
  76:     seatup = b;
  77:     System.out.println( b ? "Seat up" : "Seat down" );
  78:   }
  79: 
  80: 
  81:   /** This isn't necessarily the real main program, but it
  82:    * will be run if we type "java Toilet", so it is a good
  83:    * way to test the Toilet class.  If the toilet overflows,
  84:    * we will crash with a FloatingOverflowException; the method's 
  85:    * signature warns about this, as required. */
  86: 
  87:   public static void main(String args[]) throws FloatingOverflowException {   
  88:     Toilet throne = new Toilet();
  89: 
  90:     throne.raiseSeat(true);                         // considerate male user
  91:     throne.deposit(new LiquidWaste("pee"));         // call Toilet.deposit
  92:     (new LiquidWaste("more pee")).deposit(throne);  // same thing via Waste.deposit
  93: 
  94:     throne.raiseSeat(false);
  95:     throne.deposit(new SolidWaste("poo"));       // make toilet yucky
  96:     throne.deposit(new SolidWaste("more poo"));  // so wish we'd flushed first
  97:     throne.flush();
  98:     // assert !throne.isYucky();                 // works in java 1.4
  99: 
 100:     throne.deposit(new TrashWaste("cigarettes"));  
 101:     throne.flush();                              // might overflow
 102: 
 103:     System.out.println("All done.  No need to destroy toilet; it will be garbage-collected.");
 104:   }
 105: }
 106: 








































Html form generated by Xref-Java2Html version 1.4.0 on Mon Jan 27 13:59:15 2003