Question
JAVA 5 Files seprate 1.ImpatientCritter.java 2.LethargicCritter.java 3.MulishCritter.java 4.Critter.java(Provided) 5.CritterRunner.java(Provided) You are given a class Critter which represents an animal. Critter class will be the superclass
JAVA
5 Files seprate
1.ImpatientCritter.java
2.LethargicCritter.java
3.MulishCritter.java
4.Critter.java(Provided)
5.CritterRunner.java(Provided)
You are given a class Critter which represents an animal. Critter class will be the superclass of a set of subclasses classes. A Critter has a weight and a position on a number line. The constructor initializes the position to 0 and sets the weight from the parameter. Critter has an ArrayList of Strings to keep a log of its activities. It has other methods which you can view here
You are to implement the following subclasses of Critter
ImpatientCritter: A ImpatientCritter is always in a hurry If you tell an ImpatientCritter to move 5, it will actually move 10 steps. You will override the move method.
LethargicCritter: A LethargicCritter only has two activities: eat and sleep. When asked to move, it will either eat or sleep. A LethargicCritter is created hungry so the first time its move method is called, the LethargicCritter should eat. (and add the word "eat" to the history. The next time the move method is called, the LethargicCritter will sleep (and add the word "sleep" to the history). It will continue to alternate activities in this manner.
MulishCritter: A MulishCritter is very stubborn. It only moves every third time you tell it it move. You will override the move method. If you call the MulishCritter's move method 4 times, it will move the requested amount, not move, not move, move the requested amount. (The % operator is handy here)
Provide Javadoc. You can use @Override for overridden methods
Use the following files:
Critter.java
import java.util.ArrayList; /** * A simulated critter. */ public class Critter { private int position; private double weight; private ArrayList history; /** Constructs a critter at position 0 with blank history. */ public Critter(double theWeight) { position = 0; weight = theWeight; history = new ArrayList(); } /** Gets the history of this critter. @return the history */ public ArrayList getHistory() { return history; } /** Adds to the history of this critter. @param event the event to add to the history */ public void addHistory(String event) { history.add(event); } /** Gets the position of this critter. @return the position */ public int getPosition() { return position; } /** Moves this critter. @param steps the number of steps by which to move. */ public void move(int steps) { position = position + steps; addHistory("move to " + position); } public double getWeight() { return weight; } }
CritterRunner.java
/** * Tests Critter subclasses. * */ public class CritterRunner { public static void main(String[] args) { Critter impatient = new ImpatientCritter(15.7); Critter lazy = new LethargicCritter(5.2); Critter mule = new MulishCritter(8.2); Critter[] zoo = {impatient, lazy, mule}; //all critters inherit Critter's methods for (Critter c : zoo) { System.out.println("Weight: " + c.getWeight()); c.move(8); c.move(-3); System.out.println(c.getHistory()); System.out.println("Position: " + c.getPosition()); } } }
CritterTester.java
/** * Tests Critter subclasses. * */ public class CritterTester { public static void main(String[] args) { Critter impatient = new ImpatientCritter(15.7); impatient.move(10); impatient.move(-3); System.out.println(impatient.getHistory()); System.out.println("Expected: [move to 20, move to 14]"); System.out.println(impatient.getPosition()); System.out.println("Expected: 14"); //LethargicCritter Critter lazy = new LethargicCritter(5.2); lazy.move(2); lazy.move(4); lazy.move(-1); lazy.move(3); lazy.move(5); System.out.println(lazy.getHistory()); System.out.println("Expected: [eat, sleep, eat, sleep, eat]"); System.out.println(lazy.getPosition()); System.out.println("Expected: 0"); //MulishCritter Critter mule = new MulishCritter(8.2); mule.move(2); mule.move(4); mule.move(-1); mule.move(3); mule.move(5); System.out.println(mule.getHistory()); System.out.println("Expected: [move to 2, move to 5]"); System.out.println(mule.getPosition()); System.out.println("Expected: 5"); } }Class Critter java lang. Object L Critter public class Critter extends java lang Object A simulated critter. Constructor Summary Critter (double theWeight) Constructs a critter at position 0 with blank history Method Summary vold addHistory(jara lang. String event) Adds to the history of this critter. Gets the history of this critter. Gets the position of this critter. jaa util. ArrayList
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started