Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

. . 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 10g SQL

Authors: Joan Casteel, Lannes Morris Murphy

1st Edition

141883629X, 9781418836290

Students also viewed these Databases questions

Question

Which of the following is the first step when moving a note?

Answered: 1 week ago