Question: Here's the starter code for farmer class: package P 3 ; import java.util.Arrays; public class Farm { private double availableFood; private Animal [ ] animals;

Here's the starter code for farmer class:
package P3;
import java.util.Arrays;
public class Farm {
private double availableFood;
private Animal[] animals;
private final int MAX_ANIMAL_COUNT =100;
private int animalsCount =0;
//animalsCount is used to track the number of animals in the animals array.
//animalsCount should be incremented whenever we add a new animal to animals array.
public Farm(){
setAvailableFood(1000);
animals = new Animal[MAX_ANIMAL_COUNT];
add(new Chicken());
add(new Cow());
add(new Llama());
add(new Llama());
}
public void makeNoise(){// all animals make their sound (Moo, Cluck, etc)
for(Animal animal: getAnimals())
animal.sound();
}
public void feedAnimals(){// restore energy of all animals and deduct amount eaten from availableFood
for(Animal animal : getAnimals())
if(availableFood >= Math.min(animal.getMealAmount(),(100-animal.getEnergy())))
availableFood -= animal.eat();
else
System.out.println("Not enough food for your animals! You need to collect more food items.");
}
public void animSort(){
/* sorts animals according to their natural ordering criteria
* Note that we cannot apply Arrays.sort to animals directly if it has null values (i.e. when the farm is not full).
*/
if(animalsCount=0 && availableFood=1000)
this.availableFood = availableFood;
}
public Animal[] getAnimals(){
//returns an array with only the animals existing in the farm - i.e. doesn't return the full animals array if it has null values.
if(animalsCount MAX_ANIMAL_COUNT){
Animal[] temp = new Animal[animalsCount];
System.arraycopy(animals,0, temp, 0, animalsCount);
return temp;
}else
return animals;
}
}
In this part, you will implement a load/save feature in your game. Start by downloading the starter_code
that comes with this assignment then do as required below.
(A) Farm class:
Adda methodvoid exit (String filename)
+4 The method saves the farm data to a file named filename. Farm data includes
at least availableFood and animals (i.e. the array with all animal instances)
[+1] Once saved, display a message that data was saved successfully.
+4 If errors (exceptions) happen during the saving process, the method should print
an error message. You should have at least two catch statements (one of them is
FileNotFoundException).
[+1] Make sure your code closes the output stream regardless of whether an error
happens or not.
[+2] Implement any required changes in your code (e.g. in other classes) so that this
method works.
Adda methodvoid load(String filename)
+4 The method loads the farm data from a file named fil ename.
+1 Once loaded, display a message that data was load successfully.
[+3] If errors (exceptions) happen during the loading process, the method should print
an error message. You should have at least three catch statements (one of them is
FileNotFoundException).
+4 If f ilename is not found: in addition to the error message from the above bullet,
initialize the game with default values, e.g. availableFood should be 1000,
animals array should have 100 spots and 4 animals: a chicken, a cow, and 2 llamas.
[+1] Make sure your code
 Here's the starter code for farmer class: package P3; import java.util.Arrays;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!