Question

In: Computer Science

The Drunkard’s (Random) Walk. Imagine you live on an infinite grid of streets where locations are...

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.”

Solutions

Expert Solution

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


Related Solutions

"The Drunkard’s Walk. A drunkard in a grid of streets randomly picks one of four directions...
"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 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." for java 1.7
Imagine that the local Chamber of Commerce where you live has learned that you are taking...
Imagine that the local Chamber of Commerce where you live has learned that you are taking this course. They have asked you to come and be a guest lecturer about health system reform; specifically they are very curious about the Patient Protection and Affordable Care Act (PPACA) of 2010. For this assignment, create a 9 – 12 slide PowerPoint solely related to healthcare reform. Be sure to focus on the PPACA. Use the notes section of the PowerPoint as needed.
Imagine a universe that has a permitivity of ε = 3εo. Within this infinite space, you...
Imagine a universe that has a permitivity of ε = 3εo. Within this infinite space, you find a spherical distribution of charge of radius a that has a volumetric charge density of ρv = ( a r ) Cm−3 . a. Determine the expressions for the electric field inside and outside this spherical region. b. Determine an equation that represents the energy required to put together this distribution of charge. c. If a = 2 m, what is this energy...
Find pictures of the five Platonic solids. Imagine you are an ant that likes to walk...
Find pictures of the five Platonic solids. Imagine you are an ant that likes to walk along the edges of solids. For which of these solids is it possible to walk along each edge exactly once, ending at your starting point? Give justification, and the name(s) of the solid(s) solids where such a walk is possible.
Where are the deer? Random samples of square-kilometer plots were taken in different ecological locations of...
Where are the deer? Random samples of square-kilometer plots were taken in different ecological locations of Mesa Verde National Park. The deer counts per square kilometer were recorded and are shown in the following table. Mountain Brush Sagebrush Grassland Pinon Juniper 31 20 8 32 56 3 22 17 6 27 19 5 Shall we reject or accept the claim that there is no difference in the mean number of deer per square kilometer in these different ecological locations? Use...
Where are the deer? Random samples of square-kilometer plots were taken in different ecological locations of...
Where are the deer? Random samples of square-kilometer plots were taken in different ecological locations of a national park. The deer counts per square kilometer were recorded and are shown in the following table. Mountain Brush Sagebrush Grassland Pinon Juniper 30 24 10 32 56 7 22 14 6 34 17 7 Shall we reject or accept the claim that there is no difference in the mean number of deer per square kilometer in these different ecological locations? Use a...
Imagine yourself in a wheelchair and take a walk around your neighborhood. (If you can, ask...
Imagine yourself in a wheelchair and take a walk around your neighborhood. (If you can, ask a friend to push you in a wheelchair.) 1. What did you notice? 2. Would you have any difficulties getting everywhere you want to go? 3. What changes would you like to make in your community to benefit someone who uses a wheelchair? Why? 4. Did you observe anything that would be a regulatory or legal concern? (Keep the Americans with Disabilities Act accommodations...
Problem 1 Imagine that you walk into some class on the Thursday morning after Mardi Gras...
Problem 1 Imagine that you walk into some class on the Thursday morning after Mardi Gras break and are surprised to discover that a ten-question, multiple choice quiz is being conducted. You have not studied for this quiz, so you decide to answer the questions randomly. Determine the following: a) If each question has four possible responses (a, b, c, or d), then how many possible ways can you respond to the quiz? b) How probable is it that you...
What can you say in general about the limiting behavior of a random walk on an...
What can you say in general about the limiting behavior of a random walk on an even-length cycle compared to that of the lazy random walk?
Python Please Simulating a random walk in two dimensions. For this part, you will use a...
Python Please Simulating a random walk in two dimensions. For this part, you will use a two-dimensional grid where the x axis goes from -50 to +50 and the y axis also goes from -50 to +50. The particle will start at the middle of the left edge (x, y) = (−50,0), and we’ll put the outlet/absorber in the middle of the right edge (x, y) = (+50,0). Again, the particle will diffuse around randomly between these points and when...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT