created 06/25/99

Chapter 59 Programming Exercises

Exercise 1 — Four Buttons

Modify the TwoButtons program so that it has four buttons: one for red, one for green, one for blue, and one for gray. Clicking on a button changes the frame to the corresponding color. The GUI will look like this after the green button has been clicked:

Four Buttons

A sensible way to do this is to copy the chapter's example program and modify it. For a real challenge, try to do it from scratch.

To improve the appearance of the GUI, change the color of the buttons to reflect their function. Use the setBackground( Color ) method of JButton (which it inherits from JComponent).

Click here to go back to the main menu.


Exercise 2 — Color Cycle

Write a program that has only one button in the frame. Clicking on the button cycles through the colors: red → green → blue → gray → red → green → blue → gray → red and so on, one color change per click. In addition to the setBackground( Color.color ) method we have been using, you will need the Color getBackground() method to get the current color.

Another way to do this (a somewhat cruder way) is to use an instance variable (of type Color) in the frame to hold the current color.

Click here to go back to the main menu.


Exercise 3 — Three Button Monte

Write a program implements a game:

There are three buttons in the frame. Two of the buttons cause the program to quit using System.exit(0); the remaining button changes the frame to green (a win!) The winning button is different each time the game is played.

The easy way to do this (although it seems unfair to the user) treats each button the same way. The actionPerformed() method does not check which button was clicked. When any button is clicked, the method picks a random integer from 0 to 2 and performs the "winning" action if the integer happens to be 0. Otherwise, it performs the "losing" action. To the user, it seems like there is a "winning" button and two "losing" buttons. But, in fact, it does not matter which button was clicked.

This is similar to some electronic gambling devices in casinos, where it appears to the user that there are "winning moves" and "losing moves" but in fact the machine actually ignores what the user has done and just declares a "win" every now and then, according to predetermined odds.

You will need the Random class:

Random randNum = new Random(); // create a Random number object

. . .

int someInt = randNum.nextInt(3);  // someInt gets a number from 0 to 2

You may recall seeing Random in Chapter 20.

Click here to go back to the main menu.


Exercise 4 — Winning Streak

Modify the program of Exercise 3 so that only one button exits the program. If the user clicks on any of the other two buttons, the frame just changes color. The user keeps clicking until the "loosing button" is clicked. The user's goal is to click as many times as possible.

Click here to go back to the main menu.


Exercise 5 — Combination Lock

Create a frame with ten buttons, labeled 0 through 9. To exit the program, the user must click on the correct three buttons in order, something like 7-3-5. If the wrong combination is used, the frame turns red.

Click here to go back to the main menu.


Exercise 6 — Expand and Shrink Buttons

Create a frame with two buttons, called Expand and Shrink. When the Expand button is clicked, the frame expands by 10 percent. When the Shrink button is clicked, the frame shrinks by 10 percent. Do this in the actionPerformed() method by using setSize(). Keep track of the current size of the frame with two instance variables of type int. When you increase them or decrease them by 10 percent, you will have to use integer arithmetic or use a type cast.

Click here to go back to the main menu.