Interfaces


Interfaces

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


Classwork


Part A

Create two interfaces, Predator and Prey. Predators should have a method to eat(Prey p) and a method to hunt(Prey p). hunt should return a boolean (indicating whether the hunt was successful or not) Prey should have a method nutrition() which returns an int representing how nutritious it was.  (This is just to give you the idea -- you won't be writing the method bodies in the interfaces!!!)

In a class with a main, write a method nature which takes a Predator and a Prey as parameters. In nature, have the Predator hunt the prey, and if the hunt is successful, have the predator eat the prey.  (We can't call this method yet, because we don't have any classes that implement the interface.)


Part B

Create a class Animal.  Animals have a name, and an int health.

 Now create two classes inheriting from Animal and representing creatures of different kinds. One should be a Predator, one should be Prey ([EC] add a third that is both).   For each class, fill in the appropriate methods for the interface(s) it implements.

Prey might return their health when asked how nutritious they are, but might use some other value instead.

The hunt and eat  methods should just print a message about who is hunting/eating whom, but either use the predator's health or a random number to determine whether the Predator hunts successfully.     Do not have the hunt method call the eat method.  Also have the Predator use how nutritious the Prey is in some way when eating it (e.g., using this to add to its health).  


Part C

Add my class Machine to your project.  Then create a class HunterBot that inherits from Machine and implements Predator.  A HunterBot hunts successfully if and only if it is turned on.   When told to eat, they vaporize their prey with lasers. 

In your main call the nature method using the all combinations of the Predator and Prey types you made.


Part D [EC]

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

In your main program, write a method that takes one or more parameters of your new interface type, and uses the method(s) your interface requires. Test by calling it in your main method.

Create one more class which implements your new interface, but does not implement Predator or Prey. In your main method, create an instance of this class, and try it out with the method you just wrote as well.