Reputation: 91
I have a tester class I'm all set on, I'm just unsure on how to make this work with my blueprint. Here's what I'm trying to do:
"You exist on an infinite grid of avenues and streets where positions are represented as a coupling of integers (avenue,street). You can use negatives. 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 way of being given the drunkard's starting point. 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 input (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."
What do you recommend? Any help will do, thanks.
Here is what I have for my blueprint so far.
java.util.Random;
class Drunkard
{
private int avenue;
private int street;
Drunkard(int avenue, int street) {
this.avenue = avenue;
this.street = street;
}
Random rand = new Random();
void moveUp(){
this.street -= 1;
}
void moveDown(){
this.street += 1;
}
void moveRight(){
this.avenue += 1;
}
void moveLeft(){
this.avenue -= 1;
}
void getLocation(){
int avenue = parseInt("avenue");
int street = parseInt("street");
System.out.println("Location: " + avenue + ", " + street);
}
void fastForward(int steps)
{
for (int i=0; i < steps.length; i++}{
}
}
void step(){
}
void howFar(){
int distance = Math.abs(x1-x0) + Math.abs(y1-y0);
}
}
And I need to it correspond with this tester 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();
// instantiate
Drunkard jeff = new Drunkard(avenue,street);
//move 100 intersections
jeff.fastForward(100);
//find current location
String location = jeff.getLocation();
// get distance from start
int distance = jeff.howFar();
System.out.println("Current location: " + location);
System.out.println("This is " + distance + " blocks from your origin.");
}
}
Upvotes: 0
Views: 3389
Reputation: 513
So, to keep track of starting intersection of drunkard,
You have to keep the instance variables as something like following
private int startAvenue;
private int startStreet;
And modify the constructor to store the startAvenue and StartStreet values.
Drunkard(int avenue, int street) {
this.startAvenue = avenue;
this.startStreet = street;
}
Now the drunkard instance has its start location always.
Now we can focus on step method,
void step(){
int nextMove = rand.nextInt(3);
if(nextMove == 0)
moveUp();
else if(nextMove == 1)
moveDown();
else if(nextMove == 2)
moveLeft();
else
moveRight();
}
In fastForward(), simply invoke step() method inside the for loop
void fastForward(int steps)
{
for (int i=0; i < steps.length; i++}{
step();
}
}
In howFar() method,
int howFar(){
int distance = Math.abs(startAvenue-avenue) + Math.abs(startStreet-street);
}
Upvotes: 1