Question
Hi so I need help with this program. The RandomWalker.java compiles but the TestRandomWalker.java that was provided doesn't compile. Am I doing something wrong? RandomWalker.java
Hi so I need help with this program. The RandomWalker.java compiles but the TestRandomWalker.java that was provided doesn't compile. Am I doing something wrong?
RandomWalker.java
public class RandomWalker
{
//declare instance variables
private int x;
private int y;
private int steps;
//default constructor
public RandomWalker()
{
x=0;
y=0;
}
//Parameter constructor to set x and y values
public RandomWalker(int x,int y)
{
this.x=x;
this.y=y;
}
/**
*Instructs this random walker toupdate its coordinates by randomly
*making one of the 4 possible moves (up, down, left, or right).
*/
public void move()
{
//increment the steps by 1
steps++;
//generate a random value in a range of 0-1
double rand = Math.random();
if(rand < 0.25)
++x; // move to right
else if(rand < 0.5)
--y; // move to up
else if(rand < 0.75)
--x; // move to left
else if(rand < 1.0)
++y;// move to down
}
/**
*Random walker's current x-coordinate.
*@return random walkers x-coordinate
*/
public int getX()
{
return x;
}
/**
* Returns this random walker's current y-coordinate.
*@return random walker's y-coordinate
*/
public int getY()
{
return y;
}
/**
*Returns the number of steps this random walker has taken.
*@return the number of time move was called
*/
public int getSteps()
{
return steps;
}
}
----------------------------------------------------------
TestRandomWalker.java
import java.awt.*;
public class TestRandomWalker {
public static final int STEPS = 500;
public static void main(String[] args) {
RandomWalker walker = new RandomWalker();
DrawingPanel panel = new DrawingPanel(500, 500);
Graphics g = panel.getGraphics();
// advanced features -- center and zoom in the image
panel.getGraphics().translate(250, 250);
panel.getGraphics().scale(4, 4);
// make the walker walk, and draw its movement
int prevX = walker.getX();
int prevY = walker.getY();
for (int i = 1; i <= STEPS; i++) {
g.setColor(Color.BLACK);
g.drawLine(prevX, prevY, walker.getX(), walker.getY());
walker.move();
prevX = walker.getX();
prevY = walker.getY();
g.setColor(Color.RED);
g.drawLine(prevX, prevY, walker.getX(), walker.getY());
int steps = walker.getSteps();
if (steps % 10 == 0) {
System.out.println(steps + " steps");
}
panel.sleep(100);
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started