Machine
/ \
/ \
Appliance Computer
/ \ / \
/ \ / \
/ \ / \
WashMach Dryer MainFrame PC
// Machine: a machine has a power rating, and a method
// to increase its power.
class Machine {
public int power; // power rating for this machine
public Machine (int power) {
this.power = power;
}
// incPower: increase power rating by 20.
public void incPower () {
power += 20;
}
// getPower: get machine's power rating.
public int getPower () {
return power;
}
}
// Appliance: an appliance is a machine.
class Appliance extends Machine {
public String name;
public String loadSize; // small, medium, large load
public Appliance (String name, String loadSize, int power) {
super (power);
this.name = name;
this.loadSize = loadSize;
}
// incLoad: increase size of load
public void incLoad () {
if (loadSize.equals("small"))
loadSize = "medium";
else if (loadSize.equals("medium"))
loadSize = "large";
}
// decLoad: decrease size of load
public void decLoad () {
if (loadSize.equals("large"))
loadSize = "medium";
else if (loadSize.equals("medium"))
loadSize = "small";
}
// getLoadSize: returns current size of the load.
public String getLoadSize () {
return loadSize;
}
}
// Computer: a computer is a machine.
class Computer extends Machine {
public static int num = 0; // how many Computer objects have been
// created?
public Computer (int power) {
super (power);
num = num + 1;
}
// getNum: get count of how many Computer objects have been created.
public int getNum () {
return num;
}
}
// WashMach: a washing machine is an appliance.
class WashMach extends Appliance {
public WashMach (String name, String loadSize, int power) {
super (name, loadSize, power);
}
}
// Dryer: a dryer is an appliance.
class Dryer extends Appliance {
public static int num = 0; // how many Dryer objects have been created?
public Dryer (String name, String loadSize, int power) {
super (name, loadSize, power);
num = num + 1;
}
// getNum: get count of how many Dryer objects have been created.
public int getNum () {
return num;
}
}
// MainFrame: a main frame is a computer.
class MainFrame extends Computer {
public int numUsers; // how many users on this mainframe?
public MainFrame (int users, int power) {
super (power);
numUsers = users;
}
// incUsers: add the specified number of users to the user count.
public void incUsers (int more) {
numUsers += more;
}
// getUsers: get count of users on this mainframe
public int getUsers () {
return numUsers;
}
}
// PC: a personal computer is a computer.
class PC extends Computer {
public PC (int power) {
super (power);
}
}
// Stuff: this is the test class used to create a bunch of different
// stuff from the hierarchy described above.
class Stuff {
public static void main (String[] args) {
Appliance app;
WashMach wash = new WashMach ("Kenmore", "small", 200);
Dryer dry = new Dryer ("Miracle", "medium", 100);
Dryer fluff = new Dryer ("GE", "small", 100);
System.out.println ("wash " + wash.getLoadSize());
System.out.println ("dry " + dry.getLoadSize());
app = wash;
app.incPower();
wash.incPower();
wash.incLoad();
dry.decLoad();
System.out.println ("wash " + wash.getLoadSize());
System.out.println ("dry " + dry.getLoadSize());
System.out.println ("wash " + wash.getPower());
MainFrame dec = new MainFrame (30, 300);
PC acer = new PC (200);
PC ibm = new PC (200);
acer.incPower();
acer.incPower();
dec.incUsers (25);
System.out.println();
System.out.println ("acer " + acer.getPower());
System.out.println ("ibm " + ibm.getPower());
System.out.println ("dec " + dec.getPower());
System.out.println ("dec users " + dec.getUsers());
System.out.println();
System.out.println ("Number of Computers " + dec.getNum());
System.out.println ("Number of Dryers " + dry.getNum());
}
}
wash small dry medium wash medium dry small wash 240 acer 240 ibm 200 dec 300 dec users 55 Number of Computers 3 Number of Dryers 2
[ CSC108 Home | Outline | Announcements | Newsgroup | Assignments | Lectures | Marks | Links ]

© Copyright 2000. All Rights Reserved.