Question: Using the code for Animal.java, Dog.java and AnimalTester.java below. Redefine the updatePostion() method within Dog.java so that if the squirrel value is true, the dog
Using the code for Animal.java, Dog.java and AnimalTester.java below. Redefine the updatePostion() method within Dog.java so that if the squirrel value is true, the dog will chase the squirrel around randomly. Calculate this new position in updatePosition() to be the currentPosition plus a random number from (-runningSpeed) to (+runningSpeed). Implement a way to determine when the squirrel escapes and the dog resumes normal movement. Modify the AnimalTester.java file to test the Dog's updatePosition() method
********Animal.java******** import java.util.*; public class Animal{ public String name; public String species; public double runningSpeed; public double variationOfSpeed; public double currentPosition; public int currentTime; Random random = new Random(); public double roundSpeed; public double newPosition; public Animal(String name_, String species_, double runningSpeed_, double variationOfSpeed_, int currentTime_, double currentPosition_){ this.name = name_; this.species = species_; this.runningSpeed = runningSpeed_; this.variationOfSpeed = variationOfSpeed_; this.currentTime = 0; this.currentPosition = currentPosition_; } public Animal(String name_, String species_, double runningSpeed_, double variationOfSpeed_, int currentTime_){ this.name = name_; this.species = species_; this.runningSpeed = runningSpeed_; this.variationOfSpeed = variationOfSpeed_; this.currentTime = 0; } public Animal(String name_, String species_, double runningSpeed_){ this.name = name_; this.species = species_; this.runningSpeed = runningSpeed_; this.variationOfSpeed = 0.1*runningSpeed; this.currentTime = 0; } public String getName(){ return name; } public String getSpecies(){ return species; } public double getRunningSpeed(){ return runningSpeed; } public double getVariationOfSpeed(){ return variationOfSpeed; } public int getCurrentTime(){ return currentTime; } public double getCurrentPosition(){ return currentPosition; } public void setRunningSpeed(double runningSpeed_){ this.runningSpeed = runningSpeed_; } public void setVariationOfSpeed(double variationOfSpeed_){ this.variationOfSpeed = variationOfSpeed_; } public void setCurrentPosition(double currentPosition_){ this.currentPosition = currentPosition_; } public double updatePosition(){ this.roundSpeed = runningSpeed + variationOfSpeed*(random.nextBoolean() ? 1 : -1 ); this.currentPosition = roundSpeed + currentPosition; currentTime += 1; return currentPosition; } }
********Dog.java******** import java.util.*; public final class Dog extends Animal{ private int squirrelLocation; private double min; private double max; public boolean squirrel; public Random squirrelRange = new Random (); public Dog(String name_, String species_, double runningSpeed_, double variationOfSpeed_, int currentTime_, double currentPosition_, int squirrelLocation_, boolean squirrel_){ super(name_, species_, runningSpeed_, variationOfSpeed_, currentTime_, currentPosition_); this.squirrelLocation = squirrelLocation_; this.squirrel = squirrel_; } public boolean toggleSquirrel(){ this.squirrel = !this.squirrel; return squirrel; } public double updatePosition(){ this.roundSpeed = runningSpeed + variationOfSpeed*(random.nextBoolean() ? 1 : -1 ); this.currentPosition = roundSpeed + currentPosition; currentTime += 1; return currentPosition; } }
********AnimalTester.java******** import java.util.*; public class AnimalTester { public static void main(String[] args){ Dog dog = new Dog("John", "Rabbit", 12.0, 2.0, 0, 0.0, 0, false); System.out.println("Dog's Running speed - "+dog.getRunningSpeed()); System.out.println("Current Time - "+dog.getCurrentTime()); System.out.println("Dog's variation of Speed - "+dog.getVariationOfSpeed()); System.out.println("Dog's current position - "+dog.getCurrentPosition()); System.out.println("Squirrel? - "+dog.squirrel); dog.toggleSquirrel(); dog.updatePosition(); System.out.println(); System.out.println("Dog's Running speed - "+dog.getRunningSpeed()); System.out.println("Current Time - "+dog.getCurrentTime()); System.out.println("Dog's variation of Speed - "+dog.getVariationOfSpeed()); System.out.println("Dog's current position - "+dog.getCurrentPosition()); System.out.println("Squirrel? - "+dog.squirrel); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
