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.
Step 1. Build the Lab4 project.
- Close all applications that may be running on your computer. Start the Metrowerks Codewarrior IDE. Create a new project named lab4, configure it as a Java applet and save it on your desktop. If you can't remember how to do this, consult Lab 0: Metrowerks CodeWarrior.
- Use the Internet Explorer browser to download SimpleApplet.java from the server and place it in your lab4 folder. Using Codewarrior's project menu, add this source file to your project and remove TrivialApplet.java.
- Edit TrivialApplet.html, making "SimpleApplet.class" the value of the code attribute. You should also replace the other occurrences of "TrivialApplet" in this HTML file, although the only essiential change is to the code attribute. Do NOT change the name of the HTML file (so that you do not need to change the name of the Java Target).
- Run the applet to make sure the project is set up correctly.
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.
- Comment out the first import statement.
- Delete the semicolon after the first statement in the init() method.
- Change the spelling of Button to button in one of the declaration statements.
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.
- Comment out the line beginning
in the init() method. This is a very common mistake: declaring a variable but failing to create an instance of the object.toggle = new...- Comment out the statement in init() that adds the button to the applet.
- Modify the first statement at the beginning of the init() method to:
Run the program and click the button. This will cause a scope error. Fix the error.Button toggle = new Button("Local Button");
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.
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).
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.
Have your work checked by the lab Instructor or TA before you leave the lab.