Question
answer question in Java language. Starter code for question will be below starter code last image is the expected output public class Farm { private
answer question in Java language. Starter code for question will be below
starter code last image is the expected output
public class Farm { private double availableFood; private Animal[] animals; public Farm() { setAvailableFood(1000); animals = new Animal[4]; animals[0] = new Chicken(); animals[1] = new Cow(); animals[2] = new Llama(); animals[3] = new Llama(); } public void makeNoise(){ // all animals make their sound (Moo, Cluck, etc) for(Animal animal: animals) animal.sound(); } public void feedAnimals(){ // restore energy of all animals and deduct amount eaten from availableFood for(Animal animal : animals) if(availableFood >= Math.min(animal.getMealAmount(), (100-animal.getEnergy()))) // no penalty if student uses: if(availableFood >= animal.getMealAmount()) availableFood -= animal.eat(); else System.out.println("Not enough food for your animals! You need to collect more food items."); } public double getAvailableFood() { return availableFood; } public void setAvailableFood(double availableFood) { if(availableFood>=0 && availableFood
public class Animal { private String name; private double energy, mealAmount, x, y, speedX=1, speedY=1; private boolean alive; public Animal() { setEnergy(100); } public void speak(String msg){ if (isAlive()) System.out.println(getName() + " says: " + msg); } public double eat(){ if (isAlive()) { //get amount needed and round to 2 decimals double amount = Math.round((100-getEnergy())*100)/100.0; if (amount >= mealAmount) { System.out.println(getName() + " ate " + mealAmount + " units"); setEnergy(getEnergy() + mealAmount); return mealAmount; } else if (amount > 0) { System.out.println(getName() + " ate " + amount + " units. Now it is full!"); setEnergy(100); return amount; } else { System.out.println(getName() + " didn't eat. It is full!"); return 0; } } else { System.out.println(getName() + " is dead!"); return 0; } } public void move() { if(isAlive()){ x += speedX; y += speedY; setEnergy(getEnergy() - 0.1); }else System.out.println(getName() + "can't move. It is dead!"); } public void sound(){if(isAlive()) System.out.println("Unknown sound!");} //setters, getters, toString public String getName() { return name; } public double getEnergy() { return energy; } public void setName(String name) { this.name = name; } public void setEnergy(double energy) { if(energy>0 && energy 0); } public double getMealAmount() { return mealAmount; } public void setMealAmount(double mealAmount) { if(mealAmount>0 && mealAmount
public class FarmTest { public static void main(String[] args) throws CloneNotSupportedException { Farm myFarm = new Farm(); for(Animal a: myFarm.getAnimals()) a.setEnergy(Math.random()*100); System.out.println(" Initial list of animals: -------------------------"); myFarm.displayAnimals(); System.out.println(" Adding a clone of the second animal -----------------------------------"); myFarm.addClone(myFarm.getAnimals()[1]); myFarm.displayAnimals(); System.out.println(" After SORTING: --------------"); myFarm.animSort(); myFarm.displayAnimals(); System.out.println(" Farm summary: --------------"); myFarm.printSummary();
} } public class Chicken extends Animal{ private static int id; public Chicken() { setName("Chicken" + ++id); setMealAmount(5); } public void sound(){ if(isAlive()) System.out.println("Cluck!"); } public void swim(){ if(isAlive()) System.out.println("Chicken can swim as following:...!"); } }
public class Cow extends Animal{ private static int id; public Cow() { setName("Cow" + ++id); setMealAmount(20); } public void sound(){ if(isAlive()) System.out.println("Moo!"); } public void milk(){ if(isAlive()) System.out.println("We can milk cows as following:...!"); } } public class Llama extends Animal{ private static int id; public Llama() { setName("Llama" + ++id); setMealAmount(9); } public void sound(){ if(isAlive()) System.out.println("Hmmm!"); } public void jump(){ if(isAlive()) System.out.println("Llamas can jump as following:...!"); } }
public class Cow extends Animal{ private static int id; public Cow() { setName("Cow" + ++id); setMealAmount(20); } public void sound(){ if(isAlive()) System.out.println("Moo!"); } public void milk(){ if(isAlive()) System.out.println("We can milk cows as following:...!"); } }
(A) the Animal class: O [+1] Change Animal to an abstract class. [+2] Explain in a comment in your program why it makes sense for this class to be abstract. O [+1] Change sound() to an abstract method. [+2] Explain in a comment why it makes sense for this method to be abstract. Why don't we just remove it from Animal and keep it in the subclasses? 51 (B) the Farm class: o (+1] change your code so that the animals array will have enough space for a 100 animals. (No need to add a 100 animals in the Farm's constructor - see the next two points). o [+4] Add a method boolean add (Animal anim) that adds an animal to the next empty spot in the animals array. For example, when you first create animals, it will have a 100 empty spots (i.e. all null). The code below adds a new chicken at index 0 and a cow at index 1. The remaining spots will remain empty (i.e. equal to null). myFarm.add(new Chicken ()); myFarm.add(new Cow ()); The add method should return true when the given animal is added successfully, and false when the farm is full (i.e. no empty spots in animals array). o o [+1] modify the Farm's constructor to use the above add method in order to add a chicken, a cow, and two llamas. The animals array should now have exactly 4 animals and 96 empty spots. [+3] modify the getAnimals method so that it returns an array with existing animals only (i.e. don't return the full animals array if it has empty spots). For example, if you have only 4 animals in your farm, getAnimals will return a new array of the size 4 with these 4 animals. o [+] Add a method void animSort() that sorts the animals in the animals array based on their aner You must use the Arrass Sort method in your implementation. For 1+3] moally the getAnimals method so that it returns an array with existing animals only (i.e. don't return the full animals array if it has empty spots). For example, if you have only 4 animals in your farm, getAnimals will return a new array of the size 4 with these 4 animals. +41 Add a method void animSort () that sorts the animals in the animals array based on their energy. You must use the Arrays. sort method in your implementation. For example, suppose that animals = [Cowl, Chickeni, Llamal, null, null, ..., null] and assume that the order based on the energy is LlamalStep 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