Object Oriented Design (2)

Object Oriented Design (2) - Chapter 6: Object-Oriented...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 6: Object-Oriented Design (2) Lab Exercises Topics Lab Exercises Overall class design Random Walks GUI Layouts Telephone Keypad Chapter 6: Object-Oriented Design 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
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): the x coordinate of the current position the y coordinate of the current position the maximum number of steps in the walk the number of steps taken so far in the walk the boundary of the square (a positive integer -- the x and y coordinates of the position can vary 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. 1. First declare the instance data (as described above) and add the following two constructors and toString method. o 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. o 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. o 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: o 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. o
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/31/2010 for the course CPSC 1301 taught by Professor Khan,s during the Spring '08 term at Columbus State University.

Page1 / 7

Object Oriented Design (2) - Chapter 6: Object-Oriented...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online