CSC 330 Lab 5 solution

$24.99

Original Work ?
Category: You will Instantly receive a download link for .ZIP solution file upon Payment

Description

5/5 - (1 vote)

(2) 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 52ndStreet. 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, you’ll be asked to modify existing or partially completed code, rather than start an application from scratch. That’s exactly what you’re being asked to do here.

You’re 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.

Here’s 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 don’t need to include all data member’s 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 you’ll 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.

/

/

/

/

/

/

/

The Code that needed to be complete

/

/

///

/

/

/

/

/

/

//Dirextions/

//

/**

* Partially completed Direction ENUM

*/

package edu.cuny.csi.csc330.lab5;

import edu.cuny.csi.csc330.lib.Randomizer;

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() );

}

}

}

/

/

/

/

Drunk Walker

/

/

/

/

/

/

package edu.cuny.csi.csc330.lab5;

import java.math.*;

import java.util.*;

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” <Intersection, Integer

* 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\n”,

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();

}

}

/

/

//

/

//

Drunk Walker Tester

/

//

//

/

//

/

/

/

/

package edu.cuny.csi.csc330.lab5;

import java.util.*;

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);

}

}

/

/

/

/

Intersection

//

/**

* Class that represents a Street Corner

* Instances of this Class will be used to track the history and current location of DrunkWalker(s)

*/

package edu.cuny.csi.csc330.lab5;

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??

}

}

//

/

/

/

/

/

The output should look like that:

DrunkWalker:

billy’s location after 1 steps: Current location: DrunkWalker [avenue=7, street=23]

billy’s location after 2 steps: Current location: DrunkWalker [avenue=7, street=22]

billy’s location after 3 steps: Current location: DrunkWalker [avenue=6, street=21]

Current location after fastForward(): Current location: DrunkWalker [avenue=6, street=21]

That’s 2 blocks from start.

Start Time: Tue Oct 18 15:07:44 EDT 2016

End time – After FF: Tue Oct 18 15:07:45 EDT 2016

Starting Location: Intersection [avenue=10, street=42]

Current/Ending Location: Intersection [avenue=11, street=61]

Distance (blocks): 20

Number of steps taken: 100

Number of unique intersections visited: 71

Visited Intersection [avenue=8, street=42] 3 times!

Visited Intersection [avenue=8, street=63] 2 times!

Visited Intersection [avenue=9, street=41] 2 times!

Visited Intersection [avenue=9, street=42] 3 times!

Visited Intersection [avenue=9, street=43] 2 times!

Visited Intersection [avenue=9, street=63] 3 times!

Visited Intersection [avenue=10, street=41] 2 times!

Visited Intersection [avenue=10, street=42] 3 times!

Visited Intersection [avenue=10, street=43] 2 times!

Visited Intersection [avenue=10, street=44] 2 times!

Visited Intersection [avenue=10, street=49] 4 times!

Visited Intersection [avenue=10, street=54] 2 times!

Visited Intersection [avenue=10, street=55] 3 times!

Visited Intersection [avenue=10, street=62] 2 times!

Visited Intersection [avenue=10, street=64] 2 times!

Visited Intersection [avenue=11, street=49] 2 times!

Visited Intersection [avenue=11, street=50] 5 times!

Visited Intersection [avenue=11, street=62] 2 times!

Visited Intersection [avenue=12, street=47] 2 times!

DrunkWalkTester:

Enter the starting avenue value: 5

Enter the starting street value: 23

Billy’s Current location: DrunkWalker [avenue=-2, street=28]

That’s 12 blocks from start.

Starting Location: Intersection [avenue=5, street=23]

Current/Ending Location: Intersection [avenue=-2, street=28]

Distance (blocks): 12

Number of steps taken: 50

Number of unique intersections visited: 32

Visited Intersection [avenue=-3, street=26] 3 times!

Visited Intersection [avenue=-2, street=27] 2 times!

Visited Intersection [avenue=-1, street=25] 2 times!

Visited Intersection [avenue=-1, street=27] 3 times!

Visited Intersection [avenue=0, street=26] 2 times!

Visited Intersection [avenue=0, street=27] 3 times!

Visited Intersection [avenue=0, street=28] 2 times!

Visited Intersection [avenue=1, street=26] 2 times!

Visited Intersection [avenue=1, street=27] 2 times!

Visited Intersection [avenue=1, street=29] 2 times!

Visited Intersection [avenue=3, street=26] 3 times!

Visited Intersection [avenue=4, street=25] 2 times!

Visited Intersection [avenue=5, street=24] 2 times!

Visited Intersection [avenue=5, street=25] 2 times!