Initializing an Instance Variable with Method init

Our next applet (Fig. 20.10) computes the sum of two values input by the user and displays the result by drawing a String inside a rectangle on the applet. The sum is stored in an instance variable of class AdditionApplet, so it can be used in both method init and method paint. The HTML document to load this applet into the appletviewer is shown in Fig. 20.11.

Figure 20.10. Adding double values.

(This item is displayed on pages 970 - 971 in the print version)

 1 // Fig. 20.10:
 2 // Adding two floating-point numbers.
 3 import java.awt.Graphics; // program uses class Graphics
 4 import javax.swing.JApplet; // program uses class JApplet
 5 import javax.swing.JOptionPane; // program uses class JOptionPane
 7 public class AdditionApplet extends JApplet
 8 {
 9 private double sum; // sum of values entered by user
11 // initialize applet by obtaining values from user
12 public void init ()
13 {
14 String firstNumber; // first string entered by user
15 String secondNumber; // second string entered by user
17 double number1; // first number to add
18 double number2; // second number to add
20 // obtain first number from user
21 firstNumber = JOptionPane.showInputDialog(
22 "Enter first floating-point value" );
24 // obtain second number from user
25 secondNumber = JOptionPane.showInputDialog(
26 "Enter second floating-point value" );
28 // convert numbers from type String to type double
29 number1 = Double.parseDouble( firstNumber );
30 number2 = Double.parseDouble( secondNumber );
32 sum = number1 + number2; // add numbers
33 } // end method init
35 // draw results in a rectangle on applet's background
36 public void paint( Graphics g )
37 {
38 super.paint( g ); // call superclass version of method paint
40 // draw rectangle starting from (15, 10) that is 270
41 // pixels wide and 20 pixels tall
42 g.drawRect( 15, 10, 270, 20 );
44 // draw results as a String at (25, 25)
45 g.drawString( "The sum is " + sum, 25, 25 );
46 } // end method paint
47 } // end class AdditionApplet

Figure 20.11. AdditionApplet.html loads class AdditionApplet of Fig. 20.10 into an applet container.

(This item is displayed on page 971 in the print version)

2 "AdditionApplet.class" width = "300" height = "65"> 3 4

The applet requests that the user enter two floating-point numbers. Line 9 (Fig. 20.10) declares instance variable sum of type double. The applet contains two methodsinit (lines 1233) and paint (lines 3646). When an applet container loads this applet, the container creates an instance of class AdditionApplet and calls its init methodthis occurs only once during an applet's execution. Method init normally initializes the applet's fields (if they need to be initialized to values other than their defaults) and performs other tasks that should occur only once when the applet begins execution. The first line of init always appears as shown in line 12, which indicates that init is a public method that receives no arguments and returns no information when it completes.

Lines 1430 declare variables to store the values entered by the user, obtain the user input and convert the Strings entered by the user to double values by using Double method parseDouble.

The assignment statement at line 32 sums the values stored in variables number1 and number2, and assigns the result to instance variable sum. At this point, the applet's init method returns program control to the applet container, which then calls the applet's start method. We did not declare start in this applet, so the one inherited from class JApplet is called here. You will see typical uses of method start in Chapters 21 and 23.

Next, the applet container calls the applet's paint method. In this example, method paint draws a rectangle (line 42) in which the result of the addition will appear. Line 45 calls the Graphics object's drawString method to display the results. The statement concatenates the value of instance variable sum to the String "The sum is" and displays the concatenated String.

Software Engineering Observation 20.1

The only statements that should be placed in an applet's init method are those that should execute only once when the applet is initialized.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look


Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams


Searching and Sorting

Data Structures



Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2



Accessing Databases with JDBC


JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover

Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615 © 2008-2020.
If you may any questions please contact us: