Question: 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


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- (inheritance) Chicken Cow Llama . . . ks (A) Animal class contains: 0 I 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. mealAmount (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). o 11 One zero-arg constructor that sets energy to 100 and both, speed and speedy to 1 o Several public methods: 21 Setters and getters for all attributes except alive which should have only a getter method. Note the following: 112 your code must respect the restrictions on the values as indicated above 12] The energy's setter method should also display a message whenever the animal is hungry, e.g. "Cowl is hungry" or starving, eg "Cowi is starving", where Cowi 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. 1-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-200.0 1-2) void move(): if the animal is alive, increment x by speecx 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 mag) which displays the given msg on the console only if the animal is alive, e.g. "Cowl says: Hi." 1+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 "Cowi is deadt". If it is full, display a message such as "Cowi 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 cat () method should print on the console the amount eaten, eg. Cowl ate 5 units, now it is full! o 10 marks! (B) Cow, Chicken, and Llama classes: In each class, do the following: o 113) Override the sound() method in order to display: "Moo" for Cows, "Hmmml" for Llamas, and "Cluck!" for chicken. An animal can make a sound only if it is alive. (431 Add one more method to each class: vold 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). Add a zero-arg constructor that: (+1| Sets mealAmount to: 20 for Cow, 9 for Llama, and S 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 Chickenl to the first chicken, then name Cow3 to the third cow, and finally the name Chicken2 to the second chicken [10 marks (C) the Farm class contains: o T+11 An array animals which when initialized will hold a list of 4 animals: a Chicken, a cow, and 2 Llamas. o H11 A private double attribute availableFood which represents the amount of food available in the farm for the animals (must be from 0 to 1000). 121 A zero-arg constructor that sets availableFood to 1000 and initializes animals. [:21 A public method void makeNoise() that causes all animals to sound(). 0 0 1 2) A public method void feedArmials (that iterates over each animal, calls its eat() method, and then decrements availableFood by the amount eaten. Once the availableFood is not enough to feed the animal, your code should display a message that there is not enough food in the farm. 1+2) Setters and getters for all attributes except animals which should have only a getter method o aps (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.getAnimals037/move each animal by random amount for (int i; iint) (Math.random (2000): 1) a.move(); System.out.printin ("Before feeding: myEarn.getAvailableFood()); myFarm. CeedAnimals(); System.out.println("After feeding: + Eyfarm.getAvailableFood()); myFarm.makeNoise(); myFarm.getAnimals (10) speak "Bello! :) System.out.println(Arrays.tostring (nyears.getAnimals())); Above code should print an output similar to the one below. Note that this output may be slightly different based on several factors such as your animals' energy and the available food in the farm. Before feeding: 1000.0 Chickenl ate 5.0 units Cowl ate 0.3 units. Now It is ull! Llamal ate 5.5 units. Now it is full! Llama2 ate 1.8 units. Now it is full! After feeding: 987.4 Cluck! Moo! Hmm! Chickeni says: Hello!! I Chicken1: alive at (50.0,50.0) Energy-95.0 Cow1 : alive at 13.0,3.0) Energy-100.0 Llamal : alive at 55.0, 55.0) Energy=100.0 Llama 2 alive at (18.0.18.0) Energy=100.0 1 Note the last four lines will actually be printed on the same line on the console, but I put them here on four separate lines for darity. Also, double values may have minor precision error on your machine (eg. 987 4 may display as 987.400000000001). Feel free to change the code in Far test to test other methods in your classes. You will get the full mark for this part (.e. part (D)) # you just try the code given above and it works
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
