Posts Tagged ‘Economic Phenomena’

need help with step 11 all the previous steps are completed?

Sunday, July 18th, 2010

haphazard Walks

In this lab you would develop a class that models a haphazard walk and write two client programs that use the class. A haphazard walk is basically a ordering of steps in some enclosed expanse where the direction of each step is random. The walk terminates either when a peak number of steps has been taken or a step goes outside of the bounds of the space. haphazard walks are used to model physical phenomena much as the motion of molecules and economic phenomena much as stock prices.

We would assume that the haphazard walk takes place on a square grid with the point (0,0) at the center. The bounds of the square would be a azygos number that represents the peak x and y coordinate for the current function on the square (so for a bounds value of 10, both the x and y coordinates can depart from -10 to 10, inclusive). Each step would be one unit up, one unit down, one unit to the left, or one unit to the right. (No diagonal movement.)

The RandomWalk class would have the mass instance data (all type int):

the x coordinate of the current function
the y coordinate of the current function
the peak number of steps in the walk
the number of steps taken so far in the walk
the bounds of the square (a positive number — the x and y coordinates of the function can depart between plus and minus this value)
Create a new file RandomWalk.java. You’ll define the RandomWalk class incrementally testing each part as you go.

First verify the instance data (as described above) and add the mass two constructors and toString method.
RandomWalk (int max, int edge) – Initializes the RandomWalk object. The peak number of steps and the bounds are presented 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 peak number of steps, the boundary, and the starting function to those presented by the parameters.
String toString() – returns a progress containing the number of steps taken so far and the current function — The progress should look something like: Steps: 12; Position: (-3,5)

Compile what you have so far then open the file TestWalk.java. This file would be used to test your RandomWalk methods. So far it prompts the individual to enter a boundary, a peak number of steps, and the x and y coordinates of a position. Add the following:
Declare and instantiate two RandomWalk objects — one with bounds 5, peak 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 bounds or peak number of steps (think about what your toString method does), but that’s ok.
Compile and run the information to attain sure everything is precise so far.

Next add the mass method to the RandomWalk class: void takeStep(). This method simulates taking a azygos step either up, down, left, or right. To “take a step” create a haphazard number with 4 values (say 0, 1, 2, 3) then use a switch statement to change the function (one haphazard value would represent going right, one left, and so on). Your method should also increment the number of steps taken.

Add a for wrap to TestWalk.java to have each of your RandomWalk objects take 5 steps. indicant out each object after each step so you can see what is going on. Compile and run the information to attain sure it is precise so far.

Now add to RandomWalk.java the mass two methods. Each should be a azygos return statement that returns the value of a boolean expression.
boolean moreSteps() – returns true if the number of steps taken is inferior than the peak number; returns simulated otherwise
boolean inBounds() – returns true if the current function is on the square (include the bounds as part of the square); returns simulated otherwise.

Add to the RandomWalk class a method named walk that has no parameters and returns nothing. Its job is to simulate a complete haphazard walk. That is, it should create a ordering of steps as long the peak number of steps has not been taken and it is still in bounds (inside the square). This should be a very simple wrap (while or do… while) — you would need to call the methods takeStep, moreSteps, and inBounds.

Add to TestWalk.java a statement to instantiate a RandomWalk object with a bounds of 10 and 200 as the peak number of steps. (You may want to comment out most of the cipher currently in TestWalk — especially the individual signaling and the wrap that takes five steps — as the walk method would be easier to test on its own. The /* … */ call of comment is useful for this. But don’t delete that other code, as you’ll need it later in the lab.) Then add a statement to have the object walk. indicant the object after the walk. Compile and run the program. Run it solon than once — you should be healthy to verify by the value printed