Question: . . 4 . A FastCritter moves twice as fast as a regular critter. When asked to move by n steps, it actually moves by

..4. A FastCritter moves twice as fast as a regular critter. When asked to move by n steps, it actually moves by 2* n steps. Implement a FastCritter subclass of Critter whose move method behaves as described. FastCritter.java Critter.java 1 import java.util.ArrayList; 23/**4 A simulated critter. 5*/6 public class Critter 7{8 private int position; 9 private ArrayList history; 101112 Constructs a critter at position with blank history. 13*/14 public Critter()15{16 position =0; 17 history = new ArrayList(); 18}1920/**21 Gets the history of this critter. 22 @return the history 23*/24 public ArrayList getHistory ()25{26 return history; 272829/**30 Adds to the history of this critter. 31 @param newValue the desired state 32*/30 Adds to the history of this critter. 31 @param newValue the desired state 32*/33 public void addHistory (String event)3435 history.add(event); 36}3738/**39 Gets the position of this critter. 40 @return the position 41*/42 public int getPosition()43{44 return position; 45}4647/**48 Moves this critter. 49 @param steps the number of steps by which to move. 50*/51 public void move(int steps)52{53 position = position + steps; 54 addHistory("move to "+ position); 55}5657/**58 The action of this critter in one step of the simulation. 59*/60 public void act()61{62}63} FastCritterTester.java 1 public class FastCritterTester 2{3 public static void main(String[] args)4{5 Critter speedy = new FastCritter(); 6 speedy.move(10); 7 System.out.println(speedy.getHistory ()); 8 System.out.println("Expected: [move to 20]"); 9 speedy.move(-1); 10 System.out.println(speedy.getHistory()); 11 System.out.println("Expected: [move to 20, move to 18]"); 12}13}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!