Lab 10 In-Class: More Classes
Lab Objectives
- Gain more experience in the process of implementing and testing
a class incrementally.
- Gain more experience using a class in more than one client program.
|
In this lab you will develop a class that models a random walk and write
a few client programs that use the class. A random walk is basically a
sequence of steps of fixed length where the direction of each
step is random. Often the walk is in some enclosed space.
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 upper left hand corner (as in a graphics
window). By default each step will be a fixed number of units either
up, down, to the left, or to the right.
(No diagonal movement.) The class will need to keep track of
the current position in the walk and how many steps have been taken.
In addition to the usual methods such as a constructor and toString
the class will need methods to take a single step and to simulate
a complete walk (the sequence of steps).
The basic RandomWalk class will have the following instance data
(all type int - you must use the variable names specified):
- x - the x coordinate of the current position
- y - the y coordinate of the current position
- stepSize - the length of each step (number of units on the grid)
- maxSteps - the maximum number of steps in the walk
(the walk terminates when the number of steps exceeds this)
- numSteps - the number of steps taken so far in the walk
- gridSize - the width of the square grid (a positive integer
-- the x and y coordinates of the position can vary between 0 and this
value)
Create your lab10 subdirectory then open a new file RandomWalk.java
in Emacs.
You'll define the RandomWalk class incrementally testing each part
as you go. Be sure to include documentation as you develop the
class. You need header documentation for the class (including
a brief description of the class plus your name) and each
method MUST have header documentation that
describes what the method does including what parameters it takes
and what it returns. Put a border around the documentation so
each method is easy to locate.
- First declare the instance data (as described above) and add the
following constructor and toString method.
- A constructor RandomWalk that takes five parameters
in the following order - the
starting x, starting y, the length of each step, the
maximum number of steps in the walk, and the width of the
square grid - and uses them to initialize the Random Walk object.
- 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: (30,55)
- Make sure the class has no compilation errors
then open the file
TestWalk.java. This file will be used
to test your RandomWalk methods. So far it prompts
the user to enter the x and y coordinates of a starting position,
the length of each step, the
a maximum number of steps, and a width for the grid. Add the following:
- Declare and instantiate a RandomWalk object using
the values entered by the user.
- Print out the object (remember you just print the object without
explicitly calling toString). 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.
Run the program to make sure everything is correct so far.
- Now add getter methods for each instance variable. Recall that
each will just return the value of the instance variable. These
must be named getX, getY, getStepSize, getMaxSteps, getNumSteps,
and getGridSize.
- 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). Remember that the
position will change by the step size in the given direction.
Your method should
also increment the number of steps taken. Use either the Random class
or Math.random from the Math class to generate the random number.
- To test the takeStep method add a for loop to
TestWalk.java to have your RandomWalk object take 8 steps.
Print out the object after each step so you can see what is going on.
Run the program to make sure it is correct so far. The
walk is random so each time you run the program the positions will
change.
- Now add to RandomWalk.java the following two methods. Each should
be a single return statement (no if needed!)
that returns the value of a boolean expression.
- boolean moreSteps() - returns true if there are more steps
to be taken (that is, if the number of steps
taken is less than the maximum number); returns false otherwise
- boolean inBounds() - returns true if the current position
is on the square (include the boundary as part of the square); returns false
otherwise.
- Add to the RandomWalk class a method named walk that has no
parameters and returns nothing. Its job is to simulate a complete
random walk. That is, it should continue taking steps as long as
the maximum number of steps has not been taken and it is still
in bounds (in the square or on the boundary).
This should be a very simple loop
(while or do... while) --- you will need to call the methods takeStep,
moreSteps, and inBounds.
- Test the walk method by adding to TestWalk.java a statement
to have the object walk using the walk method (add this AFTER the
for loop that takes 8 steps).
Print the object after the
walk. Run the program more than once -- you should
be able to tell by the value printed whether the object went out of
bounds or whether it stopped because it reached the maximum number of
steps.
- Now you can see what a random walk looks like! The file
AnimateWalk.java contains a program
that uses your class
to create a RandomWalk object and draws each step as it is taken.
The code is not complete, however. The randomWalk instance variable
must be initialized. Open the java file in Emacs
and add code to initialize (instantiate)
the randomWalk object in the AnimateWalk
constructor method. Make sure that the initial x and y coordinates of
the walk are set to the middle of the grid and that the randomWalk
is initialized to the same size as the frame.
Run the program several times to make sure that it works. If you notice
that the steps are all in one direction or if there is a direction
missing that means a problem in the takeStep method.
- There is a constant for the number of steps at the top of the AnimateWalk
class. Change the value of that constant to 5000 rather than 500 and
see what the walk looks like. You will probably notice that the walk
stops because of going out of bounds (it probably didn't do that with
500).
- Now write a client program
to simulate a drunk staggering randomly on some sort of
platform (imagine a square dock in the middle of a lake). Name the
class DrunkenWalk. The goal
of the program is to simulate the walk many times
(because of randomness each walk is different) and count the number of
times the drunk falls off the platform (goes out of bounds) and the
number of times it stays on. Your
program should do the following:
- Read in the width of the platform,
the maximum number of steps, and
the number of drunks to simulate.
- Then have a loop (a for loop would be a good idea) that on each
iteration does the following:
- instantiates a new RandomWalk object to represent a drunk
(start the drunk in the middle of the platform and use 1 for a
step size),
- has the object walk, then
- determines whether or not the drunk
fell off the platform (and updates a counter).
- After the
loop print out the number of times the drunk fell off and the number
of times it didn't.
To see the
"randomness" you should run your program several times with at least
1000 for the number of drunks to simulate. Try input of 10 for
the width and 200 for the number of steps first (sometimes the drunk
falls off, sometimes not); try 10 for the width and 500 for the steps
(you should see different behavior); try 50 for the width and 200
for the steps (again different behavior).
- Finally you will write a second client program
that simulates two particles moving in space. Its goal
is to determine the number of times the two particles collide (occupy
exactly the same position after the same number of steps -- the steps
could be thought of as simulating time). This means your program
needs a way to determine whether or not the particles (the RandomWalk
objects) are in the same position. For this we will add
a method to the RandomWalk class. So, do the following:
- First, add the following method to the RandomWalk class.
- public static boolean samePosition (RandomWalk p1, RandomWalk p2) -
returns true if RandomWalk objects p1 and p2 have the same x coordinates
and the same y coordinates.
- In this random walk program it does not matter if the particles fall off of the
grid, only if they collide. Therefore, it is not necessary to create a random walk object
with a specific grid size or a maximum number of steps. In order to create a
RandomWalk without a grid size and maximum number of steps, you
should overload the constructor method by defining a second constructor method
that does not have parameters for the grid size or the maximum number of steps.
The class will still have the
gridSize and maxSteps instance variables (in fact taking them out
would cause compile errors
in other places in your code) but the value of the gridSize and maxSteps
variables are inconsiquential if the walk method is not used.
Make sure there are no syntax errors before proceding.
- Now write a client program to simulate two particles moving in space.
The two particles will start at a fixed offset horizontally from each other (for example,
if the offset is 9, then one particle will start at point (-9, 0) and
the other at (9, 0)). Use 100,000 for the maximum number of steps.
The file Collisions.java
contains the skeleton of the program for you to complete.
Note that the program has a constant for the number of steps and
also has code to read in the offset for the particles to start.
You need to instantiate two RandomWalk objects using the constructor you just
defined to simulate each particle.
Use the appropriate constants and variables and use 1 for the step size.
Your program should contain a loop that
has each particle take a step (not a complete walk!)
as long as the particles have not exceeded the number of steps.
The loop does not need to check if the particles go out of bounds.
The program should determine how often the particles have collided by calling the
static method samePosition you just
added to the RandomWalk class to determine if the particles are in
the same position after taking each step (i.e. have "collided").
After the loop, print the number of collisions (appropriately labeled).
Compile and run your program to make sure it works. As before run
it several times.
- Be sure that all your files have appropriate
documentation (including your name in the header documentation for the class)
and are properly formatted (CTRL-x CTRL-p followed by CTRL-ALT-\).
Submission
From your lab10 directory, tar your code using the command:
tar czf username-username.tgz *
Where username is the name you use to log into the lab machines.
The file name should include the username of both partners separated
by a dash.
To submit your code, copy the tar file containing your code
to the directory:
~bouchard/cpsc120/lab10