Polymorphism


Polymorphism

Monster
MuppetMonster
VampireBat
Test Harness
Deitel 9-10


Classwork

If a subclass adds no instance vars, it may be able to just use the toString it has inherited, but consider whether this will give useful output.


Part A

Create a class Appliance with instance variables

Add three methods to the class: startUp, run, and doMaintenance.  They should simply print out things like "The dishwasher is starting up." or "Doing maintenance on the dryer." using the description. doMaintenance should also set needsMaintenance to false.

In a class with a main, create a method autoKitchen, which takes an Appliance as parameter.  In the method, call startUp and run for the appliance, then  if the appliance needs maintenance call doMaintenance.

In the main method, create an array of appliances and put in it two Appliances with different descriptions.  In a loop, pass each element of the array to autoKitchen.  Test that this much runs and gives the output you expect.


Part B

Give each of the following classes a parameterized constructor that includes its description and any new instance variables it adds.

Create a class ElectricKettle that inherits from Appliance.  Add an instance variable cups, an int, which is between 0 and 5, representing how much water is in the machine. 

In ElectricKettle, override run to check if there is at least one cup of water in the machine.  If not, print a message saying the kettle has overheated and set needsMaintenance to true.  Otherwise, print a message saying that the water is boiling.

Override doMaintenance to print that the heating element is being reset. (still set needsMaintenance to false)

 

Create a class CoffeeMaker that inherits from Appliance.  Add an instance variable strength, an int, which can be from 1 to 10. 

Override run to repeat "Coffee is perking." the number of times given by strength.  (e.g. 3 times if strength is 3). 

 

Create a class BreadMachine that inherits from Appliance.  Add an instance variable size, an int, which can be 1, 2, or 3. 

In BreadMachine, override startUp, so that it prints a message saying what ingredients are being placed in the machine.  Include the correct amounts in what you print: the machine uses 3 half cups of flour (3/2 not 3 1/2) and 4 third cups of water per each size, plus yeast (so if size is 2: that's 3 cups flour, 2.66 cups water, and yeast).  If size is 3, set needsMaintenance to true.

[EC] show these amounts in the standard format instead of as decimals, e.g. 2 2/3 instead of 2.66.  Use only tools covered in this course to get java to find the values (no credit for just hardcoding in the possible values for the three possibilities)

In doMaintenance, print that the residue of an oversized loaf is being cleaned.  (and still set needsMaintenance to false)

In the main method, add to your array of Appliances at least one CoffeeMaker,  BreadMachine, and ElectricKettle.  When passed to autoKitchen these will act differently, despite the fact that the code in autoKitchen hasn't changed.