Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Walking Drunkard (or Random Walk) Problem focus on Encapsulation and putting Java Collections to work (80 pts) : Imagine an infinite grid of streets

The Walking Drunkard (or Random Walk) Problem focus on Encapsulation and putting Java Collections to work (80 pts):

Imagine an infinite grid of streets where locations/intersections are represented as integer pairs (x, y) or more precisely, (avenue, street) pairs. For example, you might be on the intersection of 8th Ave and 52nd Street. Now consider someone that has had 1 drink too many that person starts at a given intersection and then stumbles to the next intersection, but in a completely random, poorly-thought-out direction. So the drunkard might start at 7th Ave and 90th street - then randomly picks one of eight directions as a next step. Notice that diagonal steps represent a 2 linear-block move while the others represent a single linear block move:

Forward / North 7th Ave and 91st Street

Right / East 6th Ave and 90th Street.

Backward / South 7th Ave and 89th Street.

Left / West 8th Ave and 90th Street

Forward-Right / Northeast 6th Ave and 91st Street

Backward-Right / Southeast 6th Ave and 89nt Street

Backward-Left / Southwest 8th Ave and 89nt Street

Forward-Left / Northwest 8th Ave and 91st Street

See NYC grid below as an example, but our grid can be larger in scope and as you may experience, negative values for Streets / Avenues are allowed.

Lab 5 is all about modeling random movement and keeping key statistics on a block-by-block trip covered by drunken (or random) behavior ;-)

Very often as a professional software developer, youll be asked to modify existing or partially completed code, rather than start an application from scratch. Thats exactly what youre being asked to do here.

Youre being provided with four starter entities:

Direction enum defines Direction constants, and methods related to using Direction values.

Intersection Class simply models the street corner or the (avenue, street) coordinates.

DrunkWalker Class models a drunkard navigating intersection to intersection.

DrunkWalkTester Class is a test harness for the DrunkWalker Class.

Your job will be to submit all 4 Classes/enums with updates that make them far more functional than they were when you found them.

Heres what needs to be done:

Direction enum already encompasses 4 of the 8 (plus NONE) Direction-set values required. You need to:

Add the 4 additional Values

Make appropriate changes to the getNextRandom() method to consider the 4 new Direction values.

Intersection:Use Eclipse code generating features (in Editor pane right-click->source ) to generate:

a constructor that accepts avenue and street values (and of course assigns them to the Class data members).

getters/setters for the two private data members (avenue, street).

hashCode() and equals() methods

toString() method.

Populate the main() method to perform the following:

Create 2 instances of Intersection that represent two different grid/street corners.

Display the value of both instances using toString() either explicitly or implicitly.

Test the equality of the two instances you created using the equals() method display the results of the if/equals test proving that equals is working properly.

DrunkWalker models the drunkard navigating intersection to intersection. Your update tasks for this Class are follows:

Implement the hollowed out

DrunkWalker(int avenue, int street) constructor.

Complete the implementation of the following methods. In all cases, let the comments in each method be your guide:

public void step().

public String getLocation()

public void fastForward(int steps)

public int howFar()

private void takeAStep()

public void displayWalkDetails()

add the appropriate Collection data members to support the methods above

Implement a toString() method using the Eclipse->Source feature (do this last when you have the full structure of the Class worked out. You dont need to include all data members state in toString() you decide what you want to see displayed while developing and testing the application.

DrunkWalkTester is a test harness for the DrunkWalker Class.

You will leave the existing code in the runTest() method alone, but youll add to it as dictated by the comments that appear at the end of that method:

/**

* Expand the test above to include the following ...

* Create a 2nd instances of DrunkWalker - Harvey

* Have then race each - which instance (billy or harvey)

* manages to walk a greater distance with 200 steps?

*

* Also invoke the displayWalkDetails() on both instances.

*/

public class DrunkWalkTester {

private Scanner input;

public DrunkWalkTester() {

init();

}

private void init() {

input = new Scanner(System.in);

}

public void runTest(int steps ) {

System.out.print("Enter the starting avenue value: ");

int avenue = input.nextInt();

System.out.print("Enter the starting street value: ");

int street = input.nextInt();

//////////////////////// start test

// make the Drunkard with initial position

DrunkWalker billy = new DrunkWalker(avenue,street);

// have him move/step 200 time

billy.fastForward(steps);

// get his current location

String location = billy.getLocation();

// get distance from start

int distance = billy.howFar();

System.out.println("Billy's " + location);

System.out.println("That's " + distance + " blocks from start.");

billy.displayWalkDetails();

//////////////////// end test

/**

* Expand the test above to include the following ...

* Create a 2nd instances of DrunkWalker - Harvey

* Have then race each - which instance (billy or harvey)

* manages to walk a greater distance with 200 steps?

*

* Also invoke the displayWalkDetails() on both instances.

*/

}

/**

* @param args

*

*/

public static void main(String[] args) {

DrunkWalkTester tester = new DrunkWalkTester();

int steps = 2000;

if(args.length == 1) {

steps = Integer.parseInt(args[0]);

}

tester.runTest(steps);

System.exit(0);

}

}

public class Intersection {

private int avenue;

private int street;

public Intersection() {

}

// !!!!!!!!!!!!!!!!!

/**

* Constructor that takes ave and street values as parameters ...

*/

/**

* toString() method !!!!!!!!!!!!!!!!!

*/

/**

* Getters/Setters !!!!!!!!!!!!!!!!!!!

*/

/**

* hashCode() and equals() methods

*/

/**

* @param args

*/

public static void main(String[] args) {

// Implement a Testing main() !!!!!!!!!!!!!!!!

// create 2 instances , populate, display, compare ... does everything look sane??

}

}

public class DrunkWalker {

private Intersection startIntersection;

private Intersection currentIntersection;

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

// add other data members here including Collection instances that you will use to

// to track history and statistics ...

private DrunkWalker() {

init();

}

/**

*

* @param avenue

* @param street

*/

public DrunkWalker(int avenue, int street) {

init();

}

private void init() {

// What should be do here to initialize an instance??

}

/**

* step in a random direction

*/

public void step() {

takeAStep();

/** !!!!!!!!!!!!!!!!!!!!!!!!!!!

* Now, update the Collections that manage the following:

*

* 1) add this next step/intersection to a "history" List that will contain the sequence of all

* steps taken by this DrunkWalker instance

*

* 2) add this next step to a Intersection -> Counter Map ... The Map

* Collection can and should be of Generics "Type"

* key = Intersection

* value = Count Value

* Need to do something like this:

* if intersection is not saved in Map yet as a key yet, add a key->value pair of Intersection->1

* else if intersection value is there, the existing key->value pair need to be replaced as

* Intersection->existing_count+1

*

*/

}

private void takeAStep() {

Direction dir = Direction.NONE.getNextRandom();

/** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!

* now what do we do based on the random Direction created?

* How do we go about updating the "currentIntersection" instance to reflect the

* direction/step that was just selected?

*/

}

/** !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

* toString()

* @return

*/

/**

* generate string that contains current intersection/location info

*/

public String getLocation() {

// !!!!!!!!!!!!!!!!!

/**

return String.format("Current location: DrunkWalker [avenue=%d, street=%d]",

currentIntersection.getAvenue(), currentIntersection.getStreet() );

*/

return null;

}

/**

* Take N number of steps

* @param steps

*/

public void fastForward(int steps ) {

// Considering that we already have a step() method, how would we

// implement this method? Uhh, think reuse!

}

/**

* Display information about this current walker instance

*/

public void displayWalkDetails() {

/**

* This method needs to display the following information in a neat, readable format

* using calls to System.out.println() or System.out.printf()

*

* 1 - starting location

* 2 - current/ending location

* 3 - distance (value returned by howFar() )

* 4 - number of steps taken - which collection would be able to provide that information, and how?

* 5 - number of unique intersections visited -

* which collection would be able to provide that information, and how?

* 6 - Intersections visited more than once

*

*/

}

/**

* X Y Coordinate distance formula

* |x1 - x2| + |y1 - y2|

* @return

*/

public int howFar() {

/** |x1 - x2| + |y1 - y2|.

startAvenue = x1

avenue = x2

startStreet = y1

street = y2

return (Math.abs(startAvenue - avenue) ) + (Math.abs(startStreet - street));

*

*/

return 0;

}

public static void main(String[] args) {

// create Drunkard with initial position (ave,str)

DrunkWalker billy = new DrunkWalker(6,23);

for(int i = 1 ; i <= 3 ; ++i ) {

billy.step();

System.out.printf("billy's location after %d steps: %s ",

i, billy.getLocation() );

}

// get his current location

String location = billy.getLocation();

// get distance from start

int distance = billy.howFar();

System.out.println("Current location after fastForward(): " + location);

System.out.println("That's " + distance + " blocks from start.");

// have him move 25 random intersections

billy.fastForward(25);

billy.displayWalkDetails();

}

}

public enum Direction {

NONE, NORTH, EAST, SOUTH, WEST ;

// !!! Add 4 more Direction Values - NORTHEAST, NORTHWEST, SOUTHWEST, SOUTHEAST

// methods

public Direction getFavorite() {

return SOUTH; // It's getting cold! ...

}

public Direction getNextRandom() {

Randomizer randomizer = new Randomizer();

/******************************

* !!!!!

* WHAT CHANGES NEED TO BE MADE HERE SO THAT THE 4 NEW RANDOM DIRECTIONS ARE CONSIDERED

*/

int direction = randomizer.generateInt(1, 4);

// 1 = south, 2 = west, 3 = north, 4 = east

if(direction == 1) { // south

return SOUTH;

}

else if(direction == 2) { // west

return WEST;

}

else if(direction == 3) { // north

return NORTH;

}

else { // east

return EAST;

}

}

public static void main(String [] args) {

int c = 0;

while(c++ < 100) {

System.out.println(Direction.NONE.getNextRandom() );

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logics For Databases And Information Systems

Authors: Jan Chomicki ,Gunter Saake

1st Edition

1461375827, 978-1461375821

More Books

Students also viewed these Databases questions