Menu

(Solved) : Add Method Boolean Addclone Animal Anim Clones Given Animal Anim Adds Animals Array Exampl Q44105467 . . .

Add a method boolean addClone(Animal anim) that clones the givenanimal,anim, and adds it to the animals array. For example, ifanimals has 4 animals and we callmyFarm.addClone(animals[2]), aclone of animals[2] would be created and added to animals at thenext available spot. The method addClone returns true if the animalis added successfully to animals and false if the farm is full.

[+4] Implement any changes outside the Farm class in order foraddClone to work properly.

o [+3] Add a method displayAnimals that will print the list ofanimals currently living in the farm. For example, if myFarm hasonly two animals, then myFarm.printAnimals() may print thisoutput:

Chicken1: alive at (0.0,0.0) Energy=28.9 Cow1 : alive at (0.0,0.0) Energy=87.5

o [+4] Add a method int getNumChicken()that returns the numberof chicken in the farm.[+2] Add two more methods, getNumCows()andgetNumLlamas() that return the number of

cows and llamas on the farm. (hint: use instanceof).

o [+2] Add a method void displaySummary() that prints the totalnumber of animals, the number of each animal type, and the amountof available food (see sample output in part C below).

START CODE

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 FarmTest {
public static void main(String[] args) throwsCloneNotSupportedException {

Farm myFarm = new Farm(); for(Animal a: myFarm.getAnimals())

a.setEnergy(Math.random()*100);
System.out.println(“nInitial list ofanimals:n————————-“); myFarm.printAnimals();
System.out.println(“nAdding a clone of the secondanimaln——————— ————–“);
myFarm.addClone(myFarm.getAnimals()[1]);
myFarm.printAnimals();
System.out.println(“nAfter SORTING:n————–“);
myFarm.animSort();
myFarm.printAnimals();
System.out.println(“nFarm summary:n————–“);
myFarm.printSummary();

} }

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 theirsound (Moo, Cluck, etc)
       for(Animal animal: animals)
          animal.sound();
   }
   public void feedAnimals(){           // restore energy of allanimals and deduct amount eaten from availableFood
       for(Animal animal : animals)
           if(availableFood>= Math.min(animal.getMealAmount(),(100-animal.getEnergy())))
           // no penalty ifstudent uses: if(availableFood >= animal.getMealAmount())
              availableFood -= animal.eat();
           else
              System.out.println(“Not enough food for youranimals! You need to collect more food items.”);
   }
   public double getAvailableFood() {
       return availableFood;
   }
   public void setAvailableFood(double availableFood){
       if(availableFood>=0 &&availableFood<=1000)
          this.availableFood = availableFood;
   }
   public Animal[] getAnimals() {
       return animals;
   }  
}

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 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 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 amountneeded 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. Itis 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<=100)
           this.energy =energy;
       if(this.energy <= 17 )
          System.out.println(getName() + ” says: I’m STARVING”);
       else if(this.energy <= 50)
          System.out.println(getName() + ” says: I’m hungry”);
       this.alive = (energy > 0);  
   }
   public double getMealAmount() {
       return mealAmount;
   }
   public void setMealAmount(double mealAmount) {
       if(mealAmount>0 &&mealAmount<100) this.mealAmount = mealAmount;
   }
   public double getX() {
       return x;
   }
   public void setX(double x) {
       this.x = x;
   }
   public double getY() {
       return y;
   }
   public void setY(double y) {
       this.y = y;
   }
   public double getSpeedX() {
       return speedX;
   }
   public void setSpeedX(double speedX) {
       this.speedX = speedX;
   }
   public double getSpeedY() {
       return speedY;
   }
   public void setSpeedY(double speedY) {
       this.speedY = speedY;
   }
   public boolean isAlive() {
       return alive;
   }
   public String toString(){
       //return String.format(“Alive:%bName:%-10sEnergy:%-7.1fLocation:(%-2.1f,%-2.1f)”, isAlive(), name,energy,x,y);
       return String.format(“%-8s: %-5s at(%-2.1f,%-2.1f) Energy=%-7.1f”, name,isAlive()?”alive”:”dead”,x,y,energy);
   }  
}

Expert Answer


Answer to Add a method boolean addClone(Animal anim) that clones the given animal,anim, and adds it to the animals array. For exam…

OR