Java Reference
In-Depth Information
To continue testing the applet, you can run the program again and enter
different sales amounts and choose different option button codes. Valid data
input will cause the applet to display a calculated commission in a message at the
bottom of the applet window; invalid data input will cause the applet to display
an error message and then clear the text box and option buttons.
You also can run the applet using a browser by typing the complete path of
the host document, a:\Chapter04\CommissionApplet.html, in the Address or
Location text box of the browser.
Once you have determined that the program runs correctly, you should print
a copy of the CommissionApplet source code and the HTML host document
code for documentation purposes. You can print a copy of the program code
from the TextPad coding window by selecting the appropriate document in the
Selector window and then using the Print command on the File menu. A print-
out of the program code will be sent to the default printer. You can print a copy
of the applet interface using the Applet menu in the Applet Viewer window.
The final step in the project is to quit TextPad.
To Quit TextPad
1. Click the Close button in the TextPad title bar.
Chapter Summary
This chapter presented a series of steps and a discussion of a Java application to
calculate commission based on a dollar sales amount. You learned how to use a
selection structure, also called an if…else structure, to branch to a certain sec-
tion of code and a repetition structure to repeat a certain section of code. You
learned how to create a stand-alone Commission application, including the try
and catch statements to handle possible exceptions, in addition to learning ways
to test for validity, reasonableness, and accurate input. You learned how to create
a user-defined method, called the getSales() method, which is called from the
main() method and obtains a sales amount after employing an if structure to
test for valid entries. You also learned how to code the getCode() method to test
for a valid sales code, along with a while statement to allow users to reenter data
should they enter inaccurate data. You learned about using a switch statement in
the getComm() method to calculate commission based on passed parameters.
Finally, you learned to code the output() method to display formatted numeric
values and exit a program using the finish() method. At each step of the process,
you learned how to code a program stub and then test a small section of code
before moving on to the next component.
In the last part of the chapter, you learned to convert the application into an
applet with a CheckboxGroup that displays three Checkboxes as option buttons
in the user interface. You learned how to use the paint() method to draw a graphic
and set foreground and background colors of the applet window and compo-
nents. You learned how to add components to the applet interface, along with how
to code an ItemListener event to listen for the user's click of the option button and
to perform the corresponding event code. In both the application and the applet,
you learned to use code to perform data validation and display error messages.
Search WWH ::




Custom Search