Previous       Next

### Workshop: Handling Parameters in an Applet

The next project you'll undertake has little practical value, except perhaps as a taunting device. The ShowWeight applet takes a person's weight and displays it under several different units. The applet takes two parameters: a weight in pounds, and the name of the person who weighs that amount. The weight is used to figure out the person's weight in ounces, kilograms, and metric tons, all of which are displayed. Create a new file with your word processor and give it the name ShowWeight.java. Enter Listing 17.3 into the file. Then save and compile the file.

##### Listing 17.3. The Full Text of ShowWeight.java
``` 1: import java.awt.*;
2:
3: public class ShowWeight extends javax.swing.JApplet {
4: float lbs = (float)0;
5: float ozs;
6: float kgs;
7: float metricTons;
8: String name = "somebody";
9:
10: public void init() {
11: String lbsValue = getParameter("weight");
12: if (lbsValue != null) {
13: Float lbsTemp = Float.valueOf(lbsValue);
14: lbs = lbsTemp.floatValue();
15: }
16: String personValue = getParameter("person");
17: if (personValue != null) {
18: name = personValue;
19: }
20: ozs = (float)(lbs * 16);
21: kgs = (float)(lbs / 2.204623);
22: metricTons = (float)(lbs / 2204.623);
23: }
24:
25: public void paint(Graphics screen) {
26: Graphics2D screen2D = (Graphics2D) screen;
27: screen2D.drawString("Studying the weight of " + name, 5, 30);
28: screen2D.drawString("In pounds: " + lbs, 55, 50);
29: screen2D.drawString("In ounces: " + ozs, 55, 70);
30: screen2D.drawString("In kilograms: " + kgs, 55, 90);
31: screen2D.drawString("In metric tons: " + metricTons, 55, 110);
32: }
```

The init() method is where the two parameters are loaded into the applet. Because they come from the web page as strings, they must be converted into the form you need: a floating-point number for the lbs variable, and a string for name. Converting a string to a floating-point number requires two steps: converting the string to a Float object and then converting that object to a variable of the type float.

## By the Way

As you learned with strings, objects and variables are treated differently in Java programs, and there are different things you can do with them. The reason there is a Float object and a float variable type is so you can use a floating-point number as either an object or a variable. The Float object class also has useful methods, such as valueOf() and floatValue(), that you can use to convert floating-point numbers into different types of variables.

Lines 20–22 are used to convert the lbs variable into different units of measure. Each of these statements has (float) in front of the conversion equation. This is used to cast the result of the equation into a floating-point number. The paint() method of the applet uses the drawString() method of the Graphics2D class to display a line of text onscreen. The paint() method has three arguments: the text to display, and the x and y positions where the text should be shown. Before you can test the ShowWeight applet, you need to create a web page that contains the applet. Open up a new file on your word processor and name it ShowWeight.html. Enter Listing 17.4 and save it when you're done.

##### Listing 17.4. The Full Text of ShowWeight.html
```1: <applet code="ShowWeight.class" >
2: <param value="Konishiki">
3: <param value=605>
4: </applet>
```

Use a web browser equipped with the Java Plug-in to see the ShowWeight applet. This demonstration uses Konishiki as its example because the American-born sumo wrestling champion weighs in at more than 605 pounds, making him the largest of these immodest, bikini-wearing behemoths. You can substitute anyone whose weight is either exemplary or well-known. Screenshot shows an example of output from the applet. As you can see, Konishiki's workout regimen doesn't include a lot of fat-free SnackWell's Devil's Food Cakes.

##### Screenshot The ShowWeight applet loaded with Mozilla Firefox.

To make the applet display a different name along with a different value for the weight parameter, all you have to change is the ShowWeight.html file. The applet itself will continue to work correctly.

 Previous       Next