Lab 5f Interface Practice

advertisement
Lab 5f: Interface Practices
The Animal5f.java is an example of an interface.
Horse5f.java, Human5f.java, Fish5f.java are examples of classes that use that interface.
Zoo5f.java is a runner class that creates instances of animals and performs their actions.
1) Create an Interface - For this lab, using Animal5f as an example, create your own interface. The interface can be anything you
choose. Think of the name of a group of items. Examples could be TShirt or IceCreamPint or Car. The interface must:
a)
Define 2 Methods – The interface must declare a contract of 2 things that classes of this type might do. Be creative.
Examples could be getSize() or getPrice(), or driveSomewhere(). Etc…
b) Declare 3 static constants – They can be Strings, ints, doubles or another type, but they can be similar. For example T-Shirt
sizes (“Small”, “Medium” , ”Large”) or prices for ice cream pints (Homemade = 2.99, UDF brand = 2.59, Generic=2.00 ) etc…
2) Create classes - Once you’ve done that, create 2 or 3 classes that implement your interface. They need to implement the
methods you declared above. Use Horse5f.java, Human5f.java, Fish5f.java as examples. If you’re really creative, you could use
the static constants you declared earlier.
3) Create a runner – Even though you’ve not been introduced to Lists, use Zoo5f.java as an example to create instances of both
classes. Run through each of the classes and call the methods you defined.
Page 1 of 1
Interfaces
Download