Archive for November, 2007

Chapter 6 Methods 255 (such as g.drawLine( x1,

Friday, November 30th, 2007

Chapter 6 Methods 255 (such as g.drawLine( x1, y1, x2, y2 )), and a class name followed by a method name (such as Integer.parseInt(stringToConvert)). The last syntax is only for static methods of a class (discussed in detail in Chapter 8) . Common Programming Error 6.3 Omitting the return-value-type in a method definition is a syntax error. Common Programming Error 6.4 Forgetting to return a value from a method that should return a value is a syntax error. If a return value type other than void is specified, the method must contain a return statement. Common Programming Error 6.5 Returning a value from a method whose return type has been declared void is a syntax error. Common Programming Error 6.6 Declaring method parameters of the same type as float x, y instead of float x, float y is a syntax error, because types are required for each parameter in the parameter list. Common Programming Error 6.7 Placing a semicolon after the right parenthesis enclosing the parameter list of a method definition is a syntax error. Common Programming Error 6.8 Redefining a method parameter in the method s body is a syntax error. Common Programming Error 6.9 Passing to a method an argument that is not compatible with the corresponding parameter s type is a syntax error. Common Programming Error 6.10 Defining a method inside another method is a syntax error. Good Programming Practice 6.3 Avoid using the same names for instance variables and local variables. This helps readers of your program distinguish variables used in different parts of a class definition. Good Programming Practice 6.4 Choosing meaningful method names and meaningful parameter names makes programs more readable and helps avoid excessive use of comments. Software Engineering Observation 6.5 A method should usually be no longer than one page. Better yet, a method should usually be no longer than half a page. Regardless of how long a method is, it should perform one task well. Small methods promote software reusability. Software Engineering Observation 6.6 Programs should be written as collections of small methods. This makes programs easier to write, debug, maintain and modify. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hosting web - 254 Methods Chapter 6 The general format of

Thursday, November 29th, 2007

254 Methods Chapter 6 The general format of a method definition is return-value-type method-name( parameter-list ) { declarations and statements } The method-name is any valid identifier. The return-value-type is the data type of the result returned from the method to the caller. The return-value-type void indicates that a method does not return a value. Methods can return at most one value. The parameter-list is a comma-separated list in which the method declares each parameter s type and name. There must be one argument in the method call for each parameter in the method definition. Each argument also must be compatible with the type of the corresponding parameter in the method definition. For example, a parameter of type double can receive values of 7.35, 22 or 0.03546, but not “hello” (because a String cannot be assigned to a double variable). If a method does not receive any values, the parameter-list is empty (i.e., the name of the method is followed by an empty set of parentheses). Each parameter in the parameter list of a method must be declared with a data type; otherwise, a syntax error occurs. Following the first line of the method definition (also known as the method header), declarations and statements in braces form the method body. The method body is also referred to as a block. Variables can be declared in any block, and blocks can be nested. A method cannot be defined inside another method. There are three ways to return control to the statement that invoked a method. If the method does not return a result, control returns when the program flow reaches the method- ending right brace or when the statement return; is executed. If the method returns a result, the statement return expression; evaluates the expression, then returns the resulting value to the caller. When a return statement executes, control returns immediately to the statement that invoked the method. Note that the example in Fig. 6.3 actually contains two method definitions init (lines 13 41) and square(line 44 48). Remember that the applet container calls method init to initialize the applet. In this example, method init repeatedly invokes the square method to perform a calculation, then places the results in the JTextArea that is attached to the applet s content pane. When the applet appears on the screen, the results are displayed in the JTextArea. Notice the syntax used to invoke method square we use just the name of the method, followed by the arguments to the method in parentheses. Methods in a class definition are allowed to invoke other methods in the same class definition by using this syntax. (There is an exception to this rule, discussed in Chapter 8.) Methods in the same class definition are both the methods defined in that class and the inherited methods (the methods from the class that the current class extends JApplet in Fig. 6.3). We have now seen three ways to call a method: A method name by itself (as shown with square(x)in this example), a reference to an object followed by the dot (.) operator and the method name Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Chapter 6 Methods 253 43 // square method (Free web host)

Thursday, November 29th, 2007

Chapter 6 Methods 253 43 // square method definition 44 public int square( int y ) 45 { 46 return y * y; // return square of y 47 48 } // end method square 49 50 } // end class SquareIntegers Fig. 6.3 Using programmer-defined method square(part 2 of 2). Note that we declared references output, outputAreaand containerand variable result as local variables in init, because they are used only in init. Variables should be declared as instance variables only if they are required for use in more than one method of the class or if the program should save their values between calls to the class s methods. Also, note that method init calls method square directly without preceding the method name with a class name and a dot operator or a reference name and a dot operator. Each method in a class is able to call the class s other methods directly. However, there is an exception to this rule. A class s static methods can call only other static methods of the class directly. Chapter 8 discusses staticmethods in detail. The definition of method square (line 44) shows that square expects an integer parameter y; square uses this name to manipulate the value it receives. Keyword int preceding the name of the method indicates that square returns an integer result. The returnstatement in squarepasses the result of the calculation y*yback to the calling method. Note that the entire method definition appears between the braces of the class SquareInt. All methods must be defined inside a class definition. Good Programming Practice 6.2 Place a blank line between method definitions to separate the methods and enhance program readability. Common Programming Error 6.2 Defining a method outside the braces of a class definition is a syntax error. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

252 Methods Chapter 6 48). Method squarereceives the

Wednesday, November 28th, 2007

252 Methods Chapter 6 48). Method squarereceives the copy of the value of counterin the parameter y. Then, square calculates y*y (line 46). Method square uses a return statement to return (i.e., give back) the result of the calculation to the statement in init that invoked square. In method init, the return value is assigned to variable result. Lines 34 35 concatenate “The square of “, the value of counter, “is “, the value of result and a newline character to the end of output. This process repeats for each iteration of the forrepetition structure. Line 39 uses method setTextto set outputArea s text to the Stringoutput. 1 // Fig. 6.3: SquareIntegers.java 2 // A programmer-defined square method 3 4 // Java core packages 5 import java.awt.Container; 6 7 // Java extension packages 8 import javax.swing.*; 9 10 public class SquareIntegers extends JApplet { 11 12 // set up GUI and calculate squares of integers from 1 to 10 13 public void init() 14 { 15 // JTextArea to display results 16 JTextArea outputArea = new JTextArea(); 17 18 // get applet’s content pane (GUI component display area) 19 Container container = getContentPane(); 20 21 // attach outputArea to container 22 container.add( outputArea ); 23 24 int result; // store result of call to method square 25 String output = “”; // String containing results 26 27 // loop 10 times 28 for ( int counter = 1; counter <= 10; counter++ ) { 29 30 // calculate square of counter and store in result 31 result = square( counter ); 32 33 // append result to String output 34 output += “The square of ” + counter + 35 ” is ” + result + “n”; 36 37 } // end for structure 38 39 outputArea.setText( output ); // place results in JTextArea 40 41 } // end method init 42 Fig. 6.3 Using programmer-defined method square(part 1 of 2). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Chapter 6 Methods 251 Software Engineering Observation 6.4 (Hosting your own web site)

Tuesday, November 27th, 2007

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

250 Methods Chapter 6 Method Description Example abs( (Web design tools)

Monday, November 26th, 2007

250 Methods Chapter 6 Method Description Example abs( x ) absolute value of x abs( 23.7 )is 23.7 (this method also has versions for abs( 0.0 ) is 0.0 float, intand longvalues) abs( -23.7 )is 23.7 ceil( x ) rounds x to the smallest integer not less ceil( 9.2 )is 10.0 than x ceil( -9.8 )is -9.0 cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0 (x is in radians) exp( x ) exponential method ex exp( 1.0 ) is 2.71828 exp( 2.0 ) is 7.38906 floor( x ) rounds x to the largest integer not floor( 9.2 )is 9.0 greater than x floor( -9.8 )is -10.0 log( x ) natural logarithm of x (base e) log( 2.718282 )is 1.0 log( 7.389056 )is 2.0 max( x, y ) larger value of x and y max( 2.3, 12.7 )is 12.7 (this method also has versions for max( -2.3, -12.7 )is -2.3 float, intand longvalues) min( x, y ) smaller value of x and y min( 2.3, 12.7 )is 2.3 (this method also has versions for min( -2.3, -12.7 )is -12.7 float, intand longvalues) pow( x, y ) x raised to power y (xy) pow( 2.0,7.0 )is 128.0 pow( 9.0,.5 )is 3.0 sin( x ) trigonometric sine of x sin( 0.0 ) is 0.0 (x is in radians) sqrt( x ) square root of x sqrt( 900.0 )is 30.0 sqrt( 9.0 )is 3.0 tan( x ) trigonometric tangent of x tan( 0.0 ) is 0.0 (x is in radians) Fig. 6.2 Mathclass methods. There are several motivations for modularizing a program with methods. The divide- and-conquer approach makes program development more manageable. Another motivation is software reusability using existing methods as building blocks to create new programs. With good method naming and definition, you can create programs from standardized methods rather than by building customized code. For example, we did not have to define how to convert Strings to integers and floating-point numbers; Java already provides such methods for us in class Integer (parseInt) and class Double (parse- Double). A third motivation is to avoid repeating code in a program. Packaging code as a method allows a program to execute that code from several locations in a program simply by calling the method. Software Engineering Observation 6.3 To promote software reusability, each method should be limited to performing a single, well- defined task, and the name of the method should express that task effectively. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Chapter 6 Methods 249 6.3 Math Class Methods (Web host music)

Monday, November 26th, 2007

Chapter 6 Methods 249 6.3 Math Class Methods Mathclass methods allow the programmer to perform certain common mathematical calculations. We use various Math class methods here to introduce the concept of methods. Throughout the book, we discuss many other methods from the classes of the Java API. Methods are called by writing the name of the method, followed by a left parenthesis, followed by the argument (or a comma-separated list of arguments) of the method, followed by a right parenthesis. For example, a programmer desiring to calculate the square root of 900.0 might write Math.sqrt( 900.0 ) When this statement executes, it calls staticMath method sqrt to calculate the square root of the number contained in the parentheses (900.0). The number 900.0 is the argument of method sqrt. The preceding expression evaluates to 30.0. Method sqrt method takes an argument of type double and returns a result of type double. Note that all Math class methods are static; therefore, they are invoked by preceding the name of the method with the class name Math and a dot (.) operator. To output the value of the preceding method call in the command window, a programmer might write System.out.println( Math.sqrt( 900.0 ) ); In this statement, the value that sqrt returns becomes the argument to method println. Software Engineering Observation 6.2 It is not necessary to import class Math to use its methods. Math is part of the java.lang package, which is automatically imported by the compiler. Common Programming Error 6.1 Forgetting to invoke a Math class method by preceding the name of the method with the class name Math and a dot operator (.) results in a syntax error. Method arguments may be constants, variables or expressions. If c1=13.0, d=3.0 and f=4.0, then the statement System.out.println( Math.sqrt( c1 + d * f ) ); calculates and prints the square root of 13.0+3.0*4.0=25.0, namely 5.0. Some Math class methods are summarized in Fig. 6.2. In the figure, the variables x and y are of type double. The Math class also defines two commonly used mathematical constants: Math.PI and Math.E. The constant Math.PI (3.14159265358979323846) of class Math is the ratio of a circle s circumference to its diameter. The constant Math.E (2.7182818284590452354) is the base value for natural logarithms (calculated with static Math method log). 6.4 Methods Methods allow the programmer to modularize a program. Variables declared in method definitions are local variables only the method that defines them knows they exist. Most methods have a list of parameters that provide the means for communicating information between methods via method calls. A method s parameters are also local variables. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Best web design - 248 Methods Chapter 6 programmers need. The Java

Sunday, November 25th, 2007

248 Methods Chapter 6 programmers need. The Java API methods are provided as part of the Java 2 Software Development Kit (J2SDK). Good Programming Practice 6.1 Familiarize yourself with the rich collection of classes and methods in the Java API and with the rich collections of classes available in various class libraries. Software Engineering Observation 6.1 Avoid reinventing the wheel. When possible, use Java API classes and methods instead of writing new classes and methods. This reduces program development time and avoids intro ducing programming errors. Performance Tip 6.1 Do not try to rewrite existing Java API classes and methods to make them more efficient. You usually will not be able to increase the performance of these classes and methods. The programmer can write methods to define specific tasks that a program may use many times during its execution. These are sometimes referred to as programmer-defined methods. The actual statements defining the methods are written only once and are hidden from other methods. A method is invoked (i.e., made to perform its designated task) by a method call. The method call specifies the name of the method and provides information (as arguments) that the called method requires to perform its task. When the method call completes, the method either returns a result to the calling method (or caller) or simply returns control to the calling method. A common analogy for this program structure is the hierarchical form of management. A boss (the calling method, or caller) asks a worker (the called method) to perform a task and report back (i.e., return) the results after completing the task. The boss method does not know how the worker method performs its designated tasks. The worker may also call other worker methods, and the boss will be unaware of this occurrence. We will soon see how this hiding of implementation details promotes good software engineering. Figure 6.1 shows the bossmethod communicating with several worker methods in a hierarchical manner. Note that worker1 acts as a boss method to worker4 and worker5. Relationships among methods may also be different than the hierarchical structure shown in this figure. main worker1 worker2 worker3 worker4 worker5 Fig. 6.1Hierarchical boss-method/worker-method relationship. Fig. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Best web design - Chapter 6 Methods 247 Outline 6.1 Introduction 6.2

Saturday, November 24th, 2007

Chapter 6 Methods 247 Outline 6.1 Introduction 6.2 Program Modules in Java 6.3 Math Class Methods 6.4 Methods 6.5 Method Definitions 6.6 Argument Promotion 6.7 Java API Packages 6.8 Random-Number Generation 6.9 Example: A Game of Chance 6.10 Duration of Identifiers 6.11 6.12 Scope Rules Recursion 6.13 Example Using Recursion: The Fibonacci Series 6.14 Recursion vs. Iteration 6.15 Method Overloading 6.16 Methods of Class JApplet 6.17 (Optional Case Study) Thinking About Objects: Identifying Class Operations Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises 6.1 Introduction Most computer programs that solve real-world problems are much larger than the programs presented in the first few chapters of this text. Experience has shown that the best way to develop and maintain a large program is to construct it from small, simple pieces, or modules. This technique is called divide and conquer. This chapter describes many key features of the Java language that facilitate the design, implementation, operation and maintenance of large programs. 6.2 Program Modules in Java Modules in Java are called methods and classes. Java programs are written by combining new methods and classes the programmer writes with prepackaged methods and classes available in the Java API (also referred to as the Java class library) and in various other method and class libraries. In this chapter, we concentrate on methods; we begin to discuss classes in detail in Chapter 8. The Java API provides a rich collection of classes and methods for performing common mathematical calculations, string manipulations, character manipulations, input/ output operations, error checking and many other useful operations. This set of modules makes the programmer s job easier, because the modules provide many of the capabilities Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Unlimited web hosting - 6 Methods Objectives To understand how to

Saturday, November 24th, 2007

6 Methods Objectives To understand how to construct programs modularly from small pieces called methods. To introduce the common math methods available in the Java API. To be able to create new methods. To understand the mechanisms for passing information between methods. To introduce simulation techniques that use random- number generation. To understand how the visibility of identifiers is limited to specific regions of programs. To understand how to write and use methods that call themselves. Form ever follows function. Louis Henri Sullivan E pluribus unum. (One composed of many.) Virgil O! call back yesterday, bid time return. William Shakespeare, Richard II Call me Ishmael. Herman Melville, Moby Dick When you call me that, smile. Owen Wister Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01