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

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

Leave a Reply