DancingBug class
import info.gridworld.actor.Bug;
/**
* A <code>BoxBug</code> traces out a square "box"
of a given size. <br />
* The implementation of this class is testable on the AP CS A
and AB exams.
*/
// do steps 1 through 4 in the BoxBugRunner class first
// then come back here to continue
public class DancingBug extends Bug
{
// 5a) add another instance variable, an int
array
// called hotMoves (but don't
assign it anything)
// 5b) add another instance variable called
index
private int steps;
private int sideLength;
private int [] hotMoves;
private int index;
/**
* Constructs a box bug that traces a square of
a given side length
* @param length the side length
*/
// 6 instead of length, make the parameter an
int array
// called hotMoves
public DancingBug(int length)
{
// 7a assign this.hotMoves the
value of the hotMoves parameter
// this saves a reference
to the array given to the
// constructor in the
main method in DancingBugRunner
// 7b initialize index to 0
steps = 0;
sideLength = length;
}
/**
* Moves to the next location of the
square.
*/
public void act()
{
// 8 now we are going to turn the
bug
// the current number of
turns at the
// value in the hotMoves
array indicated by
// index.
//
// Use a while loop or a
for loop to do this
// the number of times the
loop repeats
// should be
hotMoves[index]
// in the body of the
loop, call the turn
// method once
// 9 add an if statement so that
if the bug can move
// it moves
// 10 add 1 to index, then
// add an if statement so
that if index is equal to
// the length of the
hotMoves array, set index back to 0
// (which starts us back
at the beginning of the array)
// remove all this code below since
it's for a BoxBug
if (steps < sideLength
&& canMove())
{
move();
steps++;
}
else
{
turn();
turn();
steps = 0;
}
// don't remove anything after
this!
}
}
Here is the DancingRunner class
import info.gridworld.actor.ActorWorld;
import info.gridworld.grid.Location;
import java.awt.Color;
/**
* This class runs a world that contains box bugs. <br
/>
* This class is not tested on the AP CS A and AB exams.
*/
// 0 Read the problem statement on the lab12
handout
// carefully so you know what we are doing below.
// 1 change Box to Dancing throughout both files
// (Use the tools>replace command in bluej)
public class DancingRunner
{
public static void main(String[] args)
{
// 2 declare and create an array called turns
// that has 6 numbers in it, ranging from 0 to
7
// (just type in any 6 numbers you like)
int[] turns = {3, 0, 4, 5, 1, 2
};
ActorWorld world = new
ActorWorld();
// 3 instead of passing 6 to the constructor,
// pass the turns array
DancingBug alice = new
DancingBug(turns);
alice.setColor(Color.ORANGE);
// 4 instead of passing 3 to the constructor,
// pass the turns array
DancingBug bob = new
DancingBug(turns);
world.add(new Location(7, 8),
alice);
world.add(new Location(5, 5),
bob);
world.show();
}
}
DancingBug class import info.gridworld.actor.Bug; /** * A BoxBug traces out a square "box" of a given size
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
DancingBug class import info.gridworld.actor.Bug; /** * A BoxBug traces out a square "box" of a given size
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!