Interface


Interface



Machine Class
Robot Class
Bee Class
Hyena Class
Ninja Class
Worker Interface
Fighter Interface
Noisemaker Interface
LivingBeing Interface
Harness

Read chapter 8, 9, 10


Classwork


Part A

Create two interfaces, Predator and Prey. Predators should have

Prey should have

When you later write these methods, eat and hunt should print brief statements so we can tell what is going on.

In a main program, write a method nature which takes a Predator and a Prey as parameters. In nature, have the given Predator hunt the given Prey, and if the hunt is successful, eat it.  You do not need to have any existing classes that implement the interfaces to do this, but you can't test your method yet.

Add a class RoboHunter which is a Predator.  RoboHunters always hunt successfully, and when they "eat" their prey, report that they have blown the prey up with a laser.  Also add a class Bait which is a Prey and always delivers 0 nutrition.  You don't need to add anything else to these classes except a very short toString that tells what they are. 

In your main, call nature for a RoboHunter and a Bait.

Part B

Create an abstract class Animal with an int instance variable health.  Animal is neither Predator nor Prey.

Create three (non-abstract) classes inheriting from Animal representing animals of some kind. 

Have your classes implement the interfaces: one should be a Predator, one should be Prey, one should be both. For each class, fill in the appropriate methods for the interface(s) it implements.

In your main, create an array of  Predators and another of Prey and fill them with your types, including RoboHunter and Bait. Call nature for all possible combinations in your arrays.

[EC] Add to Animal  an abstract method sleep that the individual Animal classes have to fill in.  Add a method naptime in the main class that takes an array of Animals and makes them all sleep.  Test that this works.

 

[EC]

Create another interface, representing some group of creatures with something in common (eg flying things, swimming things, things that lay eggs, ...). It should apply to at least one, but not all three of the animals you already wrote. Have this interface require at least one appropriate method, and have at least one of your classes implement it.

In your main program, write a method that takes one or more parameters of your interface type, and uses the method(s) your interface requires. In your main, call this method for all the possibilities for which it works.

Create one more class inheriting from Animal which implements your new interface, but not Predator or Prey. In your main, create an instance of this class, and try it out with your method