Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Project Help? Don't know how to write eat(). Didn't get the same output. I put the whole project here. The language here is Java. Thank

Project Help?

Don't know how to write eat(). Didn't get the same output. I put the whole project here.

The language here is Java. Thank you.image text in transcribed

image text in transcribed

image text in transcribed

In this part, you are required to create classes that represent our game objects. The UML design is given below followed by a brief description of each class. Farm Test --------... Farm Animal has-a (composition) is-a (inheritance) Chicken Chicken Cow Cow Llama Llama [20 marks] (A) Animal class contains: 0 [+1] Six private attributes: name (String) - the animal's name. energy (double) - the animal's energy level (must be from 0 to 100). alive (boolean) - true only when energy>0. meal Amount (double) - the maximum amount of food the animal can eat when we call its eat method (must be from 0 to 100). X and y (double) - the animal's location (no restrictions on the values). speedX and speedy (double) - the speed in x and y directions (no restrictions on the values). 0 [+1] One zero-arg constructor that sets energy to 100. o Several public methods: [+2] Setters and getters for all attributes except alive which should have only a getter method. Note the following: [+2] your code must respect the restrictions on the values as indicated above. [+2] The energy's setter method should also display a message whenever the animal is hungry, e.g. "Cowl is hungry", or starving, e.g. "Cowl is starving", where Cow1 is the animal's name. An animal is starving when its energy s 17 and it is hungry when its energy is between 17 and 50. [+2] toString method that returns a string representation of the animal's status similar to the following format: Cowl: alive at (0.0,0.0) Energy=100.0 [+2] void move(): if the animal is alive, increment x by speed and y by speedy , and decrement energy by 0.1. If the animal is dead, display a message to indicate that it is dead, e.g. "Cowl can't move. It is dead!" [+2] void speak (String msg) which displays the given msg on the console only if the animal is alive, e.g. "Cowl says: Hi." [+2] void sound(): if the animal is alive, display on the console "Unknown sound". [+4] double eat() which increments energy according to the following rules: An animal can only eat if it is alive and not full. If it is dead, display a message such as "Cowl is dead!". If it is full, display a message such as "Cowl didn't eat as it is full!" When we call eat(), the animal will eats a maximum of mealAmount. For example, if Cowl's mealAmount is 20 and its energy is 60, then the amount it eats after calling the eat () method will be 20 and its energy will be 80. On the other hand, if Cowl's energy is 95, it will eat only 5 units, bringing its energy to 100. The eat () method returns whatever amount of food the animal eats. This will be deducted from the amount of available food on the farm (will discuss this shortly). The eat () method should print on the console the amount eaten, e.g. Cowl ate 5 units, now it is full! s] (B) Cow, Chicken, and Llama classes: in each class, do the following: o [+3] Override the sound () method in order to display: "Moo" for Cows, "Hmmm!" for Llamas, and "Cluck!" for chicken. An animal can make a sound only if it is alive. o [+3] Add one more method to each class: void swim () to Chicken, Jump () to Llama, and Milk () to Cow. Each method should display some text about how the animal can perform these actions (few words should do). 0 Add a zero-arg constructor that: [+1] Sets mealAmount to: 20 for Cow, 9 for Llama, and 5 for Chicken, and initializes SpeedX and Speedy to reasonable values (e.g. 2 for cows and .5 for chicken) (+3] Automatically assigns a name to every new object that is composed of the class name followed by a number that is incremented automatically for every new object. For example, assume you create 5 animals in this order: 2 cows, a chicken, another cow, and another chicken. Your code should automatically give the first two cows the names Cowl and Cow2, then the name Chicken1 to the first chicken, then name Cow3 to the third cow, and finally the name Chicken2 to the second chicken. s] (C) the Farm class contains: 0 [+1] An array animals which when initialized will hold a list of 4 animals: a Chicken, a Cow, and 2 Llamas. 0 [+1] A private double attribute available Food which represents the amount of food available in the farm for the animals (must be from 0 to 1000). O [+2] A zero-arg constructor that sets available Food to 1000 and initializes animals. 0 O [+2] A public method void makeNoise () that causes all animals to sound(). [+2] A public method void feedAnmials () that iterates over each animal, calls its eat () method, and then decrements available Food by the amount eaten. Once the available Food is not enough to feed the animal, your code should display a message that there is not enough food in the farm. [+2] Setters and getters for all attributes except animals which should have only a getter method. o 1 (D) FarmTest class is used solely to test other classes and ensure the code works properly. For example, copy the code below to your program and run it: public class FarmTest { public static void main(String[] args) { Farm myFarm = new Farm(); for (Animal a: myFarm.getAnimals () )//move each animal by random amount for (int i=0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

T Sql Fundamentals

Authors: Itzik Ben Gan

4th Edition

0138102104, 978-0138102104

More Books

Students also viewed these Databases questions