A drunkard in a grid of streets randomly picks one of four directions and stumbl
ID: 3678478 • Letter: A
Question
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 four directions, and so on. You might think that on average the drunkard doesn't move very far because the choices cancel each other out, but that is actually not the case. Starting with the NB Grafix Thing2 project, add Java code to the paint method so that it will graphically display the drunkard's walk, using the following algorithm: initialize x and y integer variables to the center point of the canvas as long as x and y are still visible in the canvas: randomly pick one of 4 directions N, S, E, W randomly pick a small distance (e.g., between 5 and 10 pixels) draw a line from the current (x, y) to the next point (x, y) + distance set (x, y) to the next point (x, y) + distance Notes: the canvas dimensions are 800 times 572, so the center point is at (400, 286)Explanation / Answer
import java.util.Random;
/*drunkard tester class creation to print the person position direction*/
public class DrunkardTester {
public static void main(String[] args) {
//the below line used to generate random path
Random generator = new Random();
Drunkard drunkard = new Drunkard(0, 0);
int direction;
for (int i = 0; i < 100; i++) {
direction = Math.abs(generator.nextInt()) % 4;
if (direction == 0) { // N
drunkard.moveNorth();
} else if (direction == 1) { // E
drunkard.moveEast();
} else if (direction == 2) { // S
drunkard.moveSouth();
} else if (direction == 3) { // W
drunkard.moveWest();
} else {
System.out.println("Impossible!");
}
}
//report says the direction which is there in drunkard class
drunkard.report();import java.util.Random;
/*drunkard tester class creation to print the person position direction*/
public class DrunkardTester {
public static void main(String[] args) {
//the below line used to generate random path
Random generator = new Random();
Drunkard drunkard = new Drunkard(0, 0);
int direction;
for (int i = 0; i < 100; i++) {
direction = Math.abs(generator.nextInt()) % 4;
if (direction == 0) { // N
drunkard.moveNorth();
} else if (direction == 1) { // E
drunkard.moveEast();
} else if (direction == 2) { // S
drunkard.moveSouth();
} else if (direction == 3) { // W
drunkard.moveWest();
} else {
System.out.println("Impossible!");
}
}
//report says the direction which is there in drunkard class
drunkard.report();import java.util.Random;
/*drunkard tester class creation to print the person position direction*/
public class DrunkardTester {
public static void main(String[] args) {
//the below line used to generate random path
Random generator = new Random();
Drunkard drunkard = new Drunkard(0, 0);
int direction;
for (int i = 0; i < 100; i++) {
direction = Math.abs(generator.nextInt()) % 4;
if (direction == 0) { // N
drunkard.moveNorth();
} else if (direction == 1) { // E
drunkard.moveEast();
} else if (direction == 2) { // S
drunkard.moveSouth();
} else if (direction == 3) { // W
drunkard.moveWest();
} else {
System.out.println("Impossible!");
}
}
//report says the direction which is there in drunkard class
drunkard.report();
}
Another class drunkard which has paint method and directions:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Point2D;
import java.util.*;
class Drunkard {
int x, y;
double startX = 0;
double startY = 0;
Point2D.Double point = new Point2D.Double(startX, startY);
Drunkard(int x, int y) {
this.x = x;
this.y = y;
}
void moveNorth() {
this.y -= 1;
}
void moveSouth() {
this.y += 1;
}
void moveEast() {
this.x += 1;
}
void moveWest() {
this.x -= 1;
}
void report() {
System.out.println("Location: " + x + ", " + y);
}
public void Paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
Ellipse2D.Double drunk = new Ellipse2D.Double(startX ,startY , 30, 30);
g2.setColor(Color.black);
g2.draw(drunk);
g2.fill(drunk);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.