Question

JAVA You are given a class Critter which represents an animal. Critter class will be the...

JAVA

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

OverAchievingCritter: A OverAchievingCritter always does more than asked. If you tell an OverAchievingCritter 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.

UnpredictableCritter: A UnpredictableCritter is very stubborn. It only moves when it feels like it. You will override the move method. If you call the UnpredictableCritter's move method, sometimes it will move and sometimes it will not. The decision is totally random. In the conctructor construct a Random object with a seed 543212345. You can use Random's nextBoolean method or nextInt with a parameter of 1 to help you determine if the Critter should move.

Note: There are two testers for this problem.

I have most of the code done and only need help with the UnpredictableCritter. I probably have the random.nextBoolean() wrong.

OverAchievingCritter.java

public class OverAchievingCritter extends Critter {

public OverAchievingCritter(double theWeight) {

super(theWeight);

}

@Override public void move(int steps)

{

super.move(steps * 2);

}

}

LethargicCritter.java

public class LethargicCritter extends Critter {

private int activity;

public LethargicCritter(double theWeight) {

super(theWeight);

}

@Override public void move(int steps) {

if(activity == 0) {

addHistory("eat");

activity = 1;

} else {

addHistory("sleep");

activity = 0;

}

}

}

UnpredictableCritter.java

import java.util.Random;

public class UnpredictableCritter extends Critter {

public UnpredictableCritter(double theWeight) {

super(theWeight);

}

@Override public void move(int steps) {

Random random = new Random(543212345);

if(random.nextBoolean()) {

super.move(steps);

}

}

}

http://www.codecheck.it/files/19080222193b3bnz0p3ezmmwh5lrj69a4tv

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any problem with the code feel free to comment.

Program

import java.util.Random;

public class UnpredictableCritter extends Critter {
  
   private Random random;//create a random instance variable

   public UnpredictableCritter(double theWeight) {

       super(theWeight);
       random = new Random(543212345);//initialize in the constructor

   }

   @Override
   public void move(int steps) {
       if (random.nextBoolean()) {

           super.move(steps);

       }

   }

}

Output

Add a comment
Know the answer?
Add Answer to:
JAVA You are given a class Critter which represents an animal. Critter class will be the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need help with this Java question in 1 hour please JAVA Using the files from the...

    Need help with this Java question in 1 hour please JAVA Using the files from the in-class assignment add two additional animals of your choice. For each animal add two additional methods appropriate to your particular animal. As the majority of the code for this program exists you will not need an algorithm. Use the 3 java classes below. Thank you //Animal.java public class Animal {    public Animal() {    System.out.println("A new animal has been created!");    }   ...

  • Add an animal (similar to cat) to the AnimalInheritance Example. class Animal { public Animal() {...

    Add an animal (similar to cat) to the AnimalInheritance Example. class Animal { public Animal() { System.out.println("A new animal has been created!"); } public void sleep() { System.out.println("An animal sleeps..."); } public void eat() { System.out.println("An animal eats..."); } } public class Cat extends Animal { public Cat() { super(); System.out.println("A new cat has been created!"); } public void sleep() { System.out.println("A cat sleeps..."); } public void purr() { System.out.println("A cat purrs..."); } public void eat() { System.out.println("An cat eats...");...

  • The current code I have is the following: package uml; public class uml {        public...

    The current code I have is the following: package uml; public class uml {        public static void main(String[] args) {              // TODO Auto-generated method stub        } } class Account { private String accountID; public Account(String accountID) { this.accountID = accountID; } public String getAccountID() { return accountID; } public void setAccountID(String accountID) { this.accountID = accountID; } @Override public String toString() { return "Account [accountID=" + accountID + "]"; } } class SuppliesAccount extends Account { private...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • please this is Java and i need help Question 5 public class Food { public void...

    please this is Java and i need help Question 5 public class Food { public void Foodmethod_1 (int i) { public void Foodmethod_2 (int i) { } public static void Foodmethod_3(int i) { public class Bankudade extends Food { public static void Foodmethod_1 (int i) { public void Foodmethod_2 (int i) { public void Foodmethod_3 (int i) { > 7 a) Determine which method in the subclass overrides a method in the super class? (6 marks) EV b) Determine which...

  • (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */...

    (JAVA NetBeans) Write programs in java Example 10.21-24 //Animal.java /** Animal class * Anderson. Franceschi */ import java.awt.Graphics; public abstract class Animal { private int x; // x position private int y; // y position private String ID; // animal ID /** default constructor * Sets ID to empty String */ public Animal( ) { ID = ""; } /** Constructor * @param rID Animal ID * @param rX x position * @param rY y position */ public Animal( String...

  • In Java Create a testing class that does the following to the given codes below: To...

    In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...

  • *Using C++* You will create a program that uses a Critter class to move around a...

    *Using C++* You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • with C++ You will create a program that uses a Critter class to move around a...

    with C++ You will create a program that uses a Critter class to move around a Grid, which is also a class. The Critter and the Grid classes will be in separate files. The Critter class will have a data member to count the number of moves made. It will also need data members to hold the current x and y coordinates. It will have a member function that randomly moves it one space in one of 4 directions. You...

  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT