We will assume that the random walk takes place on a square grid with the point (0,0) at the center. The boundary of the square will be a single integer that represents the maximum x and y coordinate for the current position on the square (so for a boundary value of 10, both the x and y coordinates can vary from -10 to 10, inclusive). Each step will be one unit up, one unit down, one unit to the left, or one unit to the right. (No diagonal movement.)
The RandomWalk class will have the following instance data (all type int):
Compile and run the program to make sure everything is correct so far.
This method simulates taking a single step either up, down, left, or right. To "take a step" generate a random number (see note below) with 4 values (say 0, 1, 2, 3) then use a switch statement to change the position (one random value will represent going right, one left, and so on). Your method should also increment the number of steps taken. Add a for loop to TestWalk.java to have each of your RandomWalk objects take 5 steps. Print out each object after each step so you can see what is going on.
Note on generating random numbers:We have been using the Random class to generate random numbers, but in some ways it's more convenient to use the random() method of the Math class. Math.random() returns a random double value between 0 (inclusive) and 1 (exclusive). Just scale it (multiply by the range you want) and cast to an int and you're ready to go -- nothing to import, no Random object to instantiate. Try it!
Compile and run your program to make sure it works. As before run it several times.
public static boolean samePosition (RandomWalk p1, RandomWalk p2)The method should return true if p1 and p2 are at the same position and return false otherwise. Modify your main method so it calls samePosition rather than directly testing to see if the objects are at the same position. Test the program.
public int getMaxDistance()