INTERFACES Helpful URLs: https://www.w3schools.com/java/java_interface.asp https://docs.oracle.com/javase/tutorial/java/concepts/interface.html ...



INTERFACES



Helpful URLs:




https://www.w3schools.com/java/java_interface.asp





https://docs.oracle.com/javase/tutorial/java/concepts/interface.html





http://www.beginwithjava.com/java/inheritance/interfaces.html







Part A


Create two interfaces, Predator and Prey. Predators should have



  • a method eat that takes a parameter of type Prey and

  • a method hunt that takes a parameter of type Prey and returns a boolean representing whether the hunt was successful or not.


Prey should have



  • a method nutrition returning a number representing how nutritious the prey is.


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.



  • Make sure the hunt and eat methods print information about what is happening so that your eventual output makes sense.

  • Do not have the hunt method call the eat method.

  • Do not assume that all Predators and all Prey are also Animals -- the methods should also work if there were a RoboHunter or a Bait involved.

  • In at least one case, randomly determine whether the Predator hunts successfully.

  • In at least one case, have the Predator use how nutritious the Prey is in some way when eating it.

  • In at least one case, use health in determining how nutritious the Prey is.


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.


Apr 19, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here