Chapter 6 Methods 251 Software Engineering Observation 6.4 (Hosting your own web site)
Chapter 6 Methods 251 Software Engineering Observation 6.4 If you cannot choose a concise name that expresses a method s task, it is possible that your method is attempting to perform too many diverse tasks. It is usually best to break such a method into several smaller method definitions. 6.5 Method Definitions The programs presented up to this point each consisted of a class definition containing at least one method definition that called Java API methods to accomplish its tasks. We now consider how programmers write their own customized methods. Until we discuss more of the details of class definitions in Chapter 8, we use applets for all programs that contain two or more method definitions, for simplicity. Consider an applet that uses a method square (invoked from the applet s init method) to calculate the squares of the integers from 1 to 10 (Fig. 6.3). When the applet begins execution, the applet container calls the applet s init method. Line 16 declares JTextArea reference outputArea and initializes it with a new JTextArea. This JTextArea will display the program s results. This program is the first in which we display a GUI component on an applet. The on- screen display area for a JApplet has a content pane, to which the GUI components must be attached so they can be displayed at execution time. The content pane is an object of class Container from the java.awt package. This class was imported on line 5 for use in the applet. Line 19 declares Container reference container and assigns to it the result of a call to method getContentPane one of the many methods that our class SquareIntinherits from class JApplet. Method getContentPane returns a reference to the applet s content pane. The program uses that reference to attach GUI components, like a JTextArea, to the applet s user interface. Line 22 places the JTextArea GUI component object to which outputArea refers on the applet. When the applet executes, any GUI components attached to it are displayed. Container method add attaches a GUI component to a container. For the moment, we can attach only one GUI component to the applet s content pane, and that GUI component will occupy the applet s entire drawing area on the screen (as defined by the width and height of the applet, in pixels, in the applet s HTML document). Later, we will discuss how to attach many GUI components to an applet by changing the applet s layout. The layout controls how the applet positions GUI components in its area on the screen. Line 24 declares int variable result to store the result of each square calculation. Line 25 declares String reference output and initializes it with the empty string. This Stringwill contain the results of squaring the values from 1 to 10. Lines 28 37 define a for repetition structure. Each iteration of this loop calculates the square of the current value of control variable x, stores the value in result and concatenates the result to the end of output. The applet invokes (or calls) its square method on line 31 with the statement result = square( counter ); The () after square represent the method-call operator, which has high precedence. At this point, the program makes a copy of the value of counter (the argument to the method call) and transfers program control to the first line of method square(defined at lines 44 Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01