In: Computer Science
The Drunkard’s (Random) Walk. Imagine you live on an infinite grid of streets where locations are represented as integer pairs (avenue,street). Negative numbers are okay in this example. Now consider a drunkard that randomly picks one of four directions at an intersection and then stumbles to the next intersection doing the same and so on. Write a class Drunkard to simulate this behavior given the drunkard’s initial position. Your Drunkard class should have as instance variables the drunkard’s current avenue (x location) and current street (y location). Your class should have a method called step( ) that moves the drunkard to the next randomly chosen adjacent intersection. Your class should have another method called fastForward(int steps) that takes an integer as a parameter (call it steps) and moves the drunkard steps intersections from his current location. Your class should have a method getLocation( ) that returns a String indicating the drunkard’s current location. Finally your class should have a method called howFar( ) that reports the drunkards distance in blocks from where he started calculated using the Manhattan distance metric.
You will find a test class called DrunkardTester below. Your Drunkard class must work with the test class provided without modification. You must NOT alter the test class.
DrunkardTester.java :
/**
* A tester for the Drunkard class.
*/
import java.util.Scanner;
public class DrunkardTester {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the starting
avenue integer: ");
int avenue = input.nextInt();
System.out.println("Please enter the starting street
integer: ");
int street = input.nextInt();
// make the Drunkard with initial position
Drunkard ozzy = new Drunkard(avenue,street);
// have him move 100 intersections
ozzy.fastForward(100);
// get his current location
String location = ozzy.getLocation();
// get distance from start
int distance = ozzy.howFar();
System.out.println("Current location: " +
location);
System.out.println("That's " + distance + " blocks
from start.");
}
}
Additionally, there is a completely empty Drunkard.java file that you must fill out.
(Your code goes here...)
Note that the definition of Manhattan distance is:
“The distance between two points in a grid based on a strictly horizontal and/or vertical path (that is, along the grid lines), as opposed to the diagonal or “as the crow flies” distance. The Manhattan distance is the simple sum of the horizontal and vertical components, whereas the diagonal distance might be computed by applying the Pythagorean theorem.”


import java.util.Random;
public class Drunkard {
private int x;
private int y;
private Random r = new Random();
private int initX;
private int initY;
public Drunkard(int x, int y) {
this.initX = x;
this.initY = y;
this.x = x;
this.y = y;
}
public void step() {
Boolean xMoveUp = r.nextBoolean();
Boolean yMoveLeft = r.nextBoolean();
if (xMoveUp == true)
--x;
else
++x;
if (yMoveLeft == true)
--y;
else
++y;
}
public void fastForward(int steps) {
for (int i = 0; i < steps; ++i)
step();
}
public String getLocation() {
return String.format("%d Avenue, %d Street", x, y);
}
public int howFar(){
return ((Math.abs(initX) - x) + Math.abs(initY - y));
}
Save the above code as "Drunkard.java"

import java.util.Scanner;
public class DrunkardTest {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter the starting avenue integer: ");
int x = in.nextInt();
System.out.print("Please enter the starting street integer: ");
int y = in.nextInt();
Drunkard d = new Drunkard(x, y);
d.step();
d.fastForward(100);
System.out.println("Current location: " + d.getLocation());
System.out.println("That's " + d.howFar() + " blocks from start.");
}
}
Save the above code as "DrunkardTester.java"
PLEASE LIKE IT RAISE YOUR THUMBS UP
IF YOU ARE HAVING ANY DOUBT FEEL FREE TO ASK IN COMMENT
SECTION