/** * WashMach models the behaviour of a washing machine by printing * strings to represent actions, clothes being washed, etc. */ class WashMach { // ----- constructors ----- public WashMach () { name = ""; contents = ""; } public WashMach (String newName) { name = newName; contents = ""; } // ----- methods ----- public void open () { System.out.println ("The lid is open."); } public void close () { System.out.println ("The lid is closed."); } public void start () { System.out.println ("Start washing ..."); System.out.println ("Contents are clean."); } public void printIdentity () { System.out.println("Identity: machine "+name); } public void setIdentity (String id) { name = id; } public String getIdentity () { return name; } public void add (String item) { System.out.println ("Added " + item); contents = item; } public void printContents () { System.out.println("Inside you will find " + contents); } public String getContents () { return contents; } // ----- instance variables ----- private String name; // which machine is it? private String contents; // what's in the machine }
// This version shows how to use the // constructors to create two washing machines. public class TestWash2 { public static void main (String[] args) { WashMach myWashMach, yourWashMach; myWashMach = new WashMach ("55"); yourWashMach = new WashMach (); System.out.println ("My washing machine:"); myWashMach.printIdentity(); System.out.println ("Your washing machine:"); yourWashMach.printIdentity(); // Set names of washing machines yourWashMach.setIdentity ("108"); myWashMach.setIdentity ("56"); // Now let's add something to each machine myWashMach.add ("sock"); yourWashMach.add ("towel"); // Print names & contents of each machine System.out.println ("My washing machine:"); myWashMach.printIdentity(); myWashMach.printContents(); System.out.println ("Your washing machine:"); yourWashMach.printIdentity(); yourWashMach.printContents(); } }
My washing machine: Identity: machine 55 Your washing machine: Identity: machine Added sock Added towel My washing machine: Identity: machine 56 Inside you will find sock Your washing machine: Identity: machine 108 Inside you will find towel
[ Home | Outline | Announcements | Newsgroup | Assignments | Exams | Lectures | Links ]
© Copyright 2000. All Rights Reserved.