Java Reference
In-Depth Information
1 /*
2
Chapter 3: The Body Mass Index Calculator
3
Programmer: J. Starks
4
Date:
October 20, 2007
5
Filename:
BodyMassApplet.java
6
Purpose:
This project calculates the body mass index based
7
on a person's height and weight.
8 */
9
10 import java.applet.*;
11 import java.awt.*;
12 import java.awt.event.*;
13
14 public class BodyMassApplet extends Applet implements ActionListener
15 {
16
//declare variables
17
Image logo; //declare an Image object
18
int inches, pounds;
19
double meters, kilograms, index;
20
FIGURE 3-37
As you learned in Chapter 2, programmers sometimes edit existing applica-
tion code to create applet code. When the required edits are extensive, however,
it sometimes is more convenient to start a new document in the coding window.
The following steps will start a new document in TextPad in which to code the
applet version of the Body Mass Index Calculator. The steps then enter code for
comments, import statements, the class header that extends the Applet class and
implements the ActionListener, and variable declarations for the applet.
To Enter Code to Implement the ActionListener
1. Click the New Document button on the Standard toolbar in the TextPad
coding window. If line numbers do not display, click Line Numbers on
the View menu.
2. With the Data Disk in drive A, click File on the menu bar and then click
Save As on the File menu.
3. When the Save As dialog box is displayed, type BodyMassApplet in the
File name box. If necessary, click Java (*.java) in the Save as type list. If
necessary, click the Save in box arrow, click 3 1 2 Floppy (A:) in the list,
and then double-click the Chapter03 folder.
4. Click the Save button in the Save As dialog box.
5. Enter the code, as shown in Figure 3-37.
The new code is entered (Figure 3-38). The class header displays the
new class name, extension of Applet class, and implementation of the
ActionListener. Lines 17 through 19 declare the variables for the program.
Line 20 is blank intentionally, in order to separate sections of code.
Search WWH ::




Custom Search