5-Random Walks
5-Random Walks
In this lab you will develop a class that models a random walk and write two client programs that use the class. A random
walk is basically a sequence of steps in some enclosed space where the direction of each step is random. The walk terminates
either when a maximum number of steps has been taken or a step goes outside of the boundary of the space. Random walks
are used to model physical phenomena such as the motion of molecules and economic phenomena such as stock prices.
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):
Create a new file RandomWalk.java. You’ll define the RandomWalk class incrementally testing each part as you go.
1. First declare the instance data (as described above) and add the following two constructors and toString method.
RandomWalk (int max, int edge) - Initializes the RandomWalk object. The maximum number of steps and
the boundary are given by the parameters. The x and y coordinates and the number of steps taken should be
set to 0.
RandomWalk (int max, int edge, int startX, int startY) -- Initializes the maximum number of steps, the
boundary, and the starting position to those given by the parameters.
String toString() - returns a String containing the number of steps taken so far and the current position --
The string should look something like: Steps: 12; Position: (-3,5)
2. Compile what you have so far then open the file TestWalk.java. This file will be used to test your RandomWalk
methods. So far it prompts the user to enter a boundary, a maximum number of steps, and the x and y coordinates of
a position. Add the following:
Declare and instantiate two RandomWalk objects -- one with boundary 5, maximum steps 10, and centered
at the origin (use the two parameter constructor) and the other with the values entered by the user.
Print out each object. Note that you won’t get any information about the boundary or maximum number of
steps (think about what your toString method does), but that’s ok.
Compile and run the program to make sure everything is correct so far.
3. Next add the following method to the RandomWalk class: void takeStep(). This method simulates taking a single
step either up, down, left, or right. To “take a step” generate a random number 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.
4. 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. Compile and run the program to make sure it is correct so far.
5. Now add to RandomWalk.java the following two methods. Each should be a single return statement that returns the
value of a boolean expression.
boolean moreSteps() - returns true if the number of steps taken is less than the maximum number; returns
false otherwise
Compile and run your program to make sure it works. As before run it several times.
10. In your Collisions.java program the condition to determine if the points are at the same position is a bit cumbersome.
This is something that would be best put in a separate method. Add a static method to Collisions.java (after the main
method) with signature
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.
11. In using random walks to simulate behavior it is often of interest to know how far away from the origin the object
gets as it moves.
// ************************************************************
// TestWalk.java
//
// Program to test methods in the RandomWalk class.
// ************************************************************
import java.util.Scanner;