Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement a load/save feature in your game. Start by downloading the starter_code that comes with this assignment then do as required below. (in java) public

Implement a load/save feature in your game. Start by downloading the starter_code that comes with this assignment then do as required below. (in java)

image text in transcribed

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 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 Animal[] temp = getAnimals(); Arrays.sort(temp); System.arraycopy(temp, 0, animals, 0, animalsCount); }else Arrays.sort(animals); } public boolean addClone(Animal anim) throws CloneNotSupportedException { //this method creates a clone of an animal and adds it to the list of animals in the farm return add( (Animal) anim.clone()); } public boolean add(Animal anim){ //add an animal object to animals, return true if added successfully and false otherwise if(animalsCount =0 && availableFood

public abstract class Animal implements Cloneable, Comparable{ 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()) { 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 abstract void sound();

//compareTo, clone public Object clone() throws CloneNotSupportedException{ return super.clone(); } public int compareTo(Animal otherAnimal){ if(this.energy otherAnimal.energy) return 1; else return 0; /* Another solution (50% of the mark only): return (int)(this.energy - otherAnimal.energy); //code above is ok but gets only 50% of the mark - since casting will lose precision (e.g. if difference is 0 && energy 0); } public double getMealAmount() { return mealAmount; } public void setMealAmount(double mealAmount) { if(mealAmount>0 && mealAmount Farm class: Add a method void exit(String filename) . [4] The method saves the farm data to a file named Eilename. Farm data includes at least availableFood and animals (ie the array with all animal instances) . [+1) Once saved, display a message that data was saved successfully (44) of 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 PileNotFoundException). . [+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 (eg in other classes) so that this method works. Add a method void load(String filename) . (+4) The method loads the farm data from a file named Filename. . [+1) 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) (-4) if filename is not found: in addition to the error message from the above bullet, initialize the game with default values, eg, 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 closes the output stream regardless of whether an error happens or not. (+2) The constructor should take one argument for the filename of the farm data, string filename, and loads this file upon creating a new Farm instance. If the file doesn't exist, an error message should be displayed and the game should be initialized with default values (similar to the above bullet). Update the FarmTest class with the code given below. Farm mylarmnex Farm (stat.dat") myfarm.printSummary()) for Animal a: wyrarm.gerinimal() a.setEnergy (Math.random()*100) System.out.println(" Available food before feeding: " + myfarm.getAvailablerood) + " ") System.out.println(" Initial list of animals: -=- -=-"); myFarm.print animali System.out.println(" Adding a clone of the second animal - myFarm.addClone (myEarm.getAnimals ([1]) myfarm.printAnimals () myfarm feedAnimals System.out.println(" Available food after feeding: - myFarm.getAvailable Food() + " "); System.out.println(" After SORTING: - myFarm animSort(); myFarm.print animali Syatem.out.println(" Farm summary:n myFarm.printSummary(); myFarm.exit("stat.dat") Farm class: Add a method void exit(String filename) . [4] The method saves the farm data to a file named Eilename. Farm data includes at least availableFood and animals (ie the array with all animal instances) . [+1) Once saved, display a message that data was saved successfully (44) of 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 PileNotFoundException). . [+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 (eg in other classes) so that this method works. Add a method void load(String filename) . (+4) The method loads the farm data from a file named Filename. . [+1) 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) (-4) if filename is not found: in addition to the error message from the above bullet, initialize the game with default values, eg, 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 closes the output stream regardless of whether an error happens or not. (+2) The constructor should take one argument for the filename of the farm data, string filename, and loads this file upon creating a new Farm instance. If the file doesn't exist, an error message should be displayed and the game should be initialized with default values (similar to the above bullet). Update the FarmTest class with the code given below. Farm mylarmnex Farm (stat.dat") myfarm.printSummary()) for Animal a: wyrarm.gerinimal() a.setEnergy (Math.random()*100) System.out.println(" Available food before feeding: " + myfarm.getAvailablerood) + " ") System.out.println(" Initial list of animals: -=- -=-"); myFarm.print animali System.out.println(" Adding a clone of the second animal - myFarm.addClone (myEarm.getAnimals ([1]) myfarm.printAnimals () myfarm feedAnimals System.out.println(" Available food after feeding: - myFarm.getAvailable Food() + " "); System.out.println(" After SORTING: - myFarm animSort(); myFarm.print animali Syatem.out.println(" Farm summary:n myFarm.printSummary(); myFarm.exit("stat.dat")

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

9. Describe the characteristics of power.

Answered: 1 week ago