CS 201 - Computer Science I - Fall 2008
Lab 10 - loops


Loyola College > Department of Computer Science > Dr. James Glenn > CS 201 > Labs > Lab 10

Due

Wednesday, November 17th at 11:59pm. Labs submitted one day late will be assessed a 20% penalty. Labs will not be accepted more than one day late.

Objectives

Introduction

A Random walk is a series of steps in random directions from a starting point. Random walks can be used to simulate motion of particles in fluids, wanderings of animals, eye movements, escaping prisoners of war, and many other things.

For this lab we will simulate random walks starting from (0, 0) where each step is 0.05 units long. Each walk will end when it is first more than 0.4 units from the starting point.

Assignment

Complete the code in the init method in the RandomWalkApplet class.

Part 1

Draw a circle in the center of the applet that has a diameter of 80% the width of the applet. Note that the Graphics object you will use for drawing has been set up so that (0, 0) is in the center of the screen and (-200, -200) (or, more generally, (-DEFAULT_SIZE / 2, -DEFAULT_SIZE / 2) is in the upper left corner.

Part 2

Draw the result of one random walk. For one walk, you should The loop should terminate when the current point is more than 0.4 (RADIUS) units from the starting point.

The positions can be represented by Vector objects. Vector has methods that will produce a random vector of a given length, add two vectors together, and determine a vector's length (the distance from the end to (0, 0)). See the documentation for more details.

When drawing the lines, you will have to convert from position values that whose coordinates range from -0.5 to 0.5 to pixel coordinates that range from -200 to 200. You can do so by multiplying by 400 (DEFAULT_SIZE).

For debugging, you may wish to use System.out.println statements to display the current position during the walk and the number of steps after the walk. Beware that if you write an ininite loop you will not see anything on the screen.

Part 3

Add a loop that repeats all of the steps from Part 2 50 times.

Part 4 (Extra Credit)

Keep track of the shortest walk, the longest walk, and the average length of a walk. You can keep track of these values with three accumulators: one for length of the shortest walk so far, one for the length of the longest walk so far, and one for the total number of steps over all the walks so far. Initialize the accumulators before the loop you added in part 3. After each walk is complete, update the values of the accumulators as necessary.

You can also keep track of the color of the shortest walk and the color of the longest walk. Initialize those to null where you initialized the accumulators. After each walk, if you see that the last walk was the shortest (or longest) walk, then update the color of the shortest walk so far (or the color of the longest walk so far). Use drawString to display these values in the lower left corner of the applet.

Files

The Java archive contains the following two files:

Exercises

How would you test to make sure there is no bias in the random steps generated? Think about how you would expect the exit points to be distributed if the walks were uniformly random.

Suppose we wanted to highlight the shortest walk and the longest walk to make them easier to see. We could not highlight them as we draw the walk, because as we are drawing a walk we don't know if it is going to end up the longest or shortest walk. What would you need to know in order to draw the highlighted paths at the end of init? Do we have a type that can store all of that information?

Submissions

Submit the your source code for the RandomWalkApplet class as an attachment to e-mail sent to your instructor. Submit your answer to the exercises as an attachment or in the body of the message.