Interfaces


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

 

Unless I say otherwise, all classes should have at least a default constructor, accessors and mutators for all instance variables (which should be private), and a toString() method.


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, an int health, and a method sleep() which prints a message saying the Animal is sleeping and adds 2 to health.  (Animals are neither Predator nor Prey)

 Now create three classes inheriting from Animal and representing creatures of different kinds. 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.

Mostly, the hunt and eat  methods can just print to indicate who is doing what to whom.  But, in at least one case, use random numbers to determine whether the Predator hunts successfully.   Do not have the hunt method call the eat method. In at least one case, have the Predator use how nutritious the Prey is in some way when eating it (e.g., using this to add to its health).

Also add my class Machine to your project.  Then create a class HunterBot that inherits from Machine and implements Predator.  If a HunterBot is turned on, it always hunts successfully, but never if it is turned off.   When told to eat, they vaporize their prey with lasers. 

In your main, create all of your types of creature and a HunterBot, and call the nature method for all possible combinations.

Part C EC +25

Create another interface, representing some group of creatures 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 three of your creatures. Have this interface require at least one appropriate method, and have at least one of your 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 creature 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.