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 P;
import java.util.Arrays;
public class Farm
private double availableFood;
private Animal animals;
private final int MAXANIMALCOUNT ;
private int animalsCount ;
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;
animals new AnimalMAXANIMALCOUNT;
addnew Chicken;
addnew Cow;
addnew Llama;
addnew Llama;
public void makeNoise all animals make their sound Moo Cluck, etc
forAnimal animal: getAnimals
animal.sound;
public void feedAnimals restore energy of all animals and deduct amount eaten from availableFood
forAnimal animal : getAnimals
ifavailableFood Math.minanimalgetMealAmountanimal.getEnergy
availableFood animal.eat;
else
System.out.printlnNot 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 ie when the farm is not full
ifanimalsCount && availableFood
this.availableFood availableFood;
public Animal getAnimals
returns an array with only the animals existing in the farm ie doesn't return the full animals array if it has null values.
ifanimalsCount MAXANIMALCOUNT
Animal temp new AnimalanimalsCount;
System.arraycopyanimals temp, animalsCount;
return temp;
else
return animals;
In this part, you will implement a loadsave feature in your game. Start by downloading the startercode
that comes with this assignment then do as required below.
A Farm class:
Adda methodvoid exit String filename
The method saves the farm data to a file named filename. Farm data includes
at least availableFood and animals ie the array with all animal instances
Once saved, display a message that data was saved successfully.
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
Make sure your code closes the output stream regardless of whether an error
happens or not.
Implement any required changes in your code eg in other classes so that this
method works.
Adda methodvoid loadString filename
The method loads the farm data from a file named fil ename.
Once loaded, display a message that data was load successfully.
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
If ilename is not found: in addition to the error message from the above bullet,
initialize the game with default values, eg availableFood should be
animals array should have spots and animals: a chicken, a cow, and llamas.
Make sure your code
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
