You are given a class Critter which represents an animal. Critter class will be
ID: 3808847 • Letter: Y
Question
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
CritterRunner.java
CritterTester.java
Explanation / Answer
package critter;
/*
* am uploading the additional java files you require,feel free to comment
* for any modifications or doubts,ALL THE BEST
*/
public class ImpatientCritter extends Critter {
boolean alternate=false;
public ImpatientCritter(double theWeight) {
super(theWeight);
// TODO Auto-generated constructor stub
}
@Override
public void move(int steps)
{
super.move(2*steps);
if(!alternate){
super.addHistory("move to "+super.getPosition());
alternate =!false;
}else{
super.addHistory("move to "+super.getPosition());
alternate=!true;
}
}
}
//**********************************************************************
public class LethargicCritter extends Critter{
boolean alternate=false;
public LethargicCritter(double theWeight) {
super(theWeight);
// TODO Auto-generated constructor stub
}
@Override
public void move(int steps)
{
if(!alternate){
super.addHistory("eat");
alternate =!false;
}else{
super.addHistory("sleep");
alternate=!true;
}
}
}
//**********************************************************************************
public class MulishCritter extends Critter {
int i=3;
public MulishCritter(double theWeight) {
super(theWeight);
// TODO Auto-generated constructor stub
}
@Override
public void move(int steps)
{
if(i%3==0&&i/3!=0)
super.move(steps);
i++;
}
}
//*********************************************8888
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.