Question
(In Java) Write a Plant class with a name (string) attribute, a size attribute (initially 1), a grow method which increases the plant's size by
(In Java)
Write a Plant class with a name (string) attribute, a size attribute (initially 1), a grow method which increases the plant's size by 1, and a die method which destroys/deletes that Plant.
Write an Animal class with a name (string) attribute, a size attribute (initially 1), an eat method which takes either a Plant instance or an Animal instance as the argument and returns nothing, and a die method which destroys/deletes that Animal.
Write a Carnivore class which inherits from Animal. Its eat method should invoke die on the Plant or Animal eaten, and do the following:
If the object eaten was a Plant, the Carnivore should decrease its size by 1 (if it reaches 0, it should die).
If the object eaten was an Animal, it should increase its size by the size of the animal eaten.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started