Chapter 6: Object-Oriented Design (2)
Lab Exercises
Topics Lab Exercises
Overall class design Random Walks
GUI Layouts Telephone Keypad
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.
Compile and run the program to make sure everything is correct so far.
Compile and run your program to make sure it works. As before run it several times.
public static boolean samePosition (RandomWalk p1, RandomWalk p2)
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.
a. Add an instance variable maxDistance (type int) to the RandomWalk class. This should be set to 0 in each constructor.
b. Now the takeStep method needs to update this maximum when a step is taken. We'll add a support method to the class to do this. Add a private method named max that takes two integer parameters (say num1 and num2) and returns the largest of the two.
c. Add code to takeStep to update maxDistance. This can be done in a single statement using the max method -- the new value of maxDistance should be the maximum of 1) the old value of maxDistance, and 2) the current distance to the origin. Note that if the current point is (-3, 15) the distance to the origin is 15; if the current point is (-10, 7) the distance to the origin is 10. Remember that Math.abs returns the absolute value of a number.
d. Finally add an accessor method to return that distance so a client program can access it:
public int getMaxDistance()
e. Test the maximum by adding statements in TestWalk.java to get and print the maximum distance for each of the objects after the loop that had them take and print out 5 steps (this way you can see if the maximum is correct – each step is printed).
// ***********************************************************
// TestWalk.java
//
// Program to test methods in the RandomWalk class.
// ***********************************************************
import java.util.Scanner;
public class TestWalk
{
public static void main (String[] args)
{
int maxSteps; // maximum number of steps in a walk
int maxCoord; // the maximum x and y coordinate
int x, y; // starting x and y coordinates for a walk
Scanner scan = new Scanner(System.in);
System.out.println ("\nRandom Walk Test Program");
System.out.println ();
System.out.print ("Enter the boundary for the square: ");
maxCoord = scan.nextInt();
System.out.print ("Enter the maximum number of steps: ");
maxSteps = scan.nextInt();
System.out.print ("Enter the starting x and y coordinates with “ +
“a space between: ");
x = scan.nextInt();
y = scan.nextInt();
}
}
Telephone Keypad
Files Telephone.java and TelephonePanel.java contain the skeleton for a program to lay out a GUI that looks like telephone keypad with a title that says “Your Telephone!!”. Save these files to your directory. Telephone.java is complete, but TelephonePanel.java is not.
1. Using the comments as a guide, add code to TelephonePanel.java to create the GUI. Some things to consider:
a. TelephonePanel (the current object, which is a JPanel) should get a BorderLayout to make it easy to separate the title from the keypad. The title will go in the north area and the keypad will go in the center area. The other areas will be unused.
b. You can create a JLabel containing the title and add it directly to the north section of the TelephonePanel. However, to put the keypad in the center you will first need to create a new JPanel and add the keys (each a button) to it, then add it to the center of the TelephonePanel. This new panel should have a 4x3 GridLayout.
c. Your keypad should hold buttons containing 1 2 3, 4 5 6, 7 8 9, * 0 # in the four rows respectively. So you’ll create a total of 12 buttons.
2. Compile and run Telephone.java. You should get a small keypad and title. Grow the window (just drag the corner) and see how the GUI changes – everything grows proportionately.
3. Note that the title is not centered, but it would look nicer if it were. One way to do this is to create a new JPanel , add the title label to it, then add the new JPanel to the north area of the TelephonePanel (instead of adding the label directly). This works because the default layout for a JPanel is a centered FlowLayout, and the JPanel itself will expand to fill the whole north area. Modify your program in this way so that the title is centered.
//******************************************************
// Telephone.java
//
// Uses the TelephonePanel class to create a (functionless) GUI
// like a telephone keypad with a title.
// Illustrates use of BorderLayout and GridLayout.
//******************************************************
import javax.swing.*;
public class Telephone
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Telephone");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new TelephonePanel());
frame.pack();
frame.setVisible(true);
}
}
//******************************************************
// TelephonePanel.java
//
// Lays out a (functionless) GUI like a telephone keypad with a title.
// Illustrates use of BorderLayout and GridLayout.
//******************************************************
import java.awt.*;
import javax.swing.*;
public class TelephonePanel extends JPanel
{
public TelephonePanel()
{
//set BorderLayout for this panel
//create a JLabel with "Your Telephone" title
//add title label to north of this panel
//create panel to hold keypad and give it a 4x3 GridLayout
//add buttons representing keys to key panel
//add key panel to center of this panel
}
}