Chapter 4 Input/Output: Designing the User Interface

Ch 4 Lab: Three Stooges

Authors: Ralph Morelli & Bronzell Dinkins
Trinity College, Hartford, CT
For use with Chapter 4

For Instructors

This is a lab that is suitable for Chapter 4. It requires the creation of a simple applet interface involving three Button components. Its emphasizes the use of inheritance and polymorphism in creating a subclass of the java.applet.Applet class that implements the ActionListener interface.

Part I of this lab contains several exercises aimed at focusing attention on some of the issues that are important for Java applet programming. These are described in the In the Laboratory section of Chapter 4. Part I can be skipped (or deleted) if students are already familiar with that material from a previous lab.

Objectives

The objectives of this lab are:

Before Lab

Read and study Chapter 4 of Java, Java, Java, 3E and read this document. Bring your text book to lab.

Problem Statement

Modify the provided SimpleApplet program so that it contains three buttons labels "Moe," "Larry," and "Curly," or any three distinct names that you choose. Modify the actionPerformed() method so that every time the user clicks on one of the buttons, the labels on the buttons are rotated, with the first button getting the second button's label, the second getting the third button's label, and the third getting the first button's label. (Hint: You don't need an if-else statement for this problem.)

In-lab Directions

This lab has two parts. Part I involves creating an applet project using the provided SimpleApplet.java file and performing several exercises to note certain types of syntax errors. In Part II, you will modify SimpletApplet to solve the problem described above.

Part I

Step 1. Build the Lab4 project.

Step 2: Changing the applet's size.

Edit the length and width attributes in TrivialApplet.html to make the size of the applet larger. The other way to change the size of an applet is to call the setSize(width,height) method in the applet's init() method. (See Exercise 22, page 216, for more details on using this method.)

Step 3: Syntax Errors.

Make each of the following changes to SimpleApplet.java, one at a time, then recompile it and note the syntax error that results. Copy the error message and comment on the error in a text file, SimpleApplet.txt. You will up load this file at the end of lab.

Step 4: Tracing the Applet.

Place a System.out.println("In Method X") statement, where X is the name of the method, in each method of SimpleApplet. Then run the program and note the output that's generated in the Java console window. You should see that the init() method is called once when the applet first starts up, but the actionPerformed() method is called each time you click on the button.

Step 5: Semantic (runtime) Errors.

Make each of the following changes to SimpleApplet.java, one at a time, then rerun it and note the error that results. Move your Appletviewer window so that you can see what is being written to the standard System.out window. Make a note of the error in your text file, SimpleApplet.txt, and try to explain it. Ask the instructor or TA if you can't understand it.

Part II.

Modify the SimpleApplet class (page 176) so that it contains three buttons labels "Moe," "Larry," and "Curly," or any three distinct names that you choose. Modify the actionPerformed() method so that every time the user clicks on one of the buttons, the labels on the buttons are rotated, with the first button getting the second button's label, the second getting the third button's label, and the third getting the first button's label.

Note that you do not need to use an if-else statement to solve this problem. Basically you want to swap the labels on each of the three buttons. See page 202 for an example of a swapping algorithm that might be helpful in this case.

Optional

This section is required if you complete all other lab activites with more than 30 minutes remaining.
Add a Reset button to the applet. When pressed, the Reset button should return the labels to the original configuration ("Moe" "Larry" "Curly").

HINT: In order to determine which button was pressed you must use the getSource() method, which is explained, with examples, in Java, Java, Java, 3E section 4.4.7).

Documentation Requirements

It's important that you properly document your final program. There should be a comment block at the beginning of the program that gives the name of the file, the author of the program and a brief description of the program, including a description of the problem solved by the program.

There should be a comment block before each of the methods, the init() and actionPerformed() methods. These comments should describe what the method does and should provide details about the method's parameters and return values. Your code should use proper indentation and appropriate whitespace to make it easily readable. See Appendix A for an example of what a properly documented program should look like.

Handin

Have your work checked by the lab Instructor or TA before you leave the lab.

You're done. Great work!