DrunkardsWalk Submit Assignment Due Tuesday by 11:59pm Pos 100Submitting a file
ID: 3590095 • Letter: D
Question
DrunkardsWalk Submit Assignment Due Tuesday by 11:59pm Pos 100Submitting a file upload File Types java The Drunkard's Walk. A drunkard in a grid of streets randomly picks one of four directions and stumbles to the next intersection, then again randomly picks one of the four directions, and so on. You might think that on average the drunkard does not move very far because the choices cancel each other out, but that is actually not the case. Represent locations as integer pairs (x.y). Implement the drunkard's walk over 100 intersections, starting at (0,0), and print the ending location. PreviousExplanation / Answer
/* Please find the code Below */
import java.util.Random;
public class RandomWalkTester
{
public static void main(String[] args)
{
final int NORTH = 0;
final int EAST = 1;
final int SOUTH = 2;
final int WEST = 3;
Random gen = new Random();
RandomWalk drunkard = new RandomWalk();
int direction;
for (int i = 0; i < 100; i++)
{
direction = gen.nextInt(4);
if (direction == NORTH)
{
drunkard.moveNorth();
}
else if (direction == EAST)
{
drunkard.moveEast();
}
else if (direction == SOUTH)
{
drunkard.moveSouth();
}
else if (direction == WEST)
{
drunkard.moveWest();
}
else
{
System.out.println("Impossible!");
}
}
drunkard.getDrunk();
}
}
RandomWalk.java
class RandomWalk
{
private int x;
private int y;
RandomWalk ()
{
x = 0;
y = 0;
}
public void moveNorth()
{
y = y + 1;
}
public void moveSouth()
{
y = y - 1;
}
public void moveEast()
{
x = x + 1;
}
public void moveWest()
{
x = x - 1;
}
public void getDrunk()
{
System.out.println("Location: " + x + ", " + y);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.