26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6 Methods 249<br />

6.3 Math Class Methods<br />

Math class methods allow the programmer <strong>to</strong> perform certain common mathematical calculations.<br />

We use various Math class methods here <strong>to</strong> introduce the concept of methods.<br />

Throughout the book, we discuss many other methods from the classes of the <strong>Java</strong> API.<br />

Methods are called by writing the name of the method, followed by a left parenthesis,<br />

followed by the argument (or a comma-separated list of arguments) of the method, followed<br />

by a right parenthesis. For example, a programmer desiring <strong>to</strong> calculate the square<br />

root of 900.0 might write<br />

Math.sqrt( 900.0 )<br />

When this statement executes, it calls static Math method sqrt <strong>to</strong> calculate the square<br />

root of the number contained in the parentheses (900.0). The number 900.0 is the argument<br />

of method sqrt. The preceding expression evaluates <strong>to</strong> 30.0. Method sqrt method<br />

takes an argument of type double and returns a result of type double. Note that all<br />

Math class methods are static; therefore, they are invoked by preceding the name of the<br />

method with the class name Math and a dot (.) opera<strong>to</strong>r. To output the value of the preceding<br />

method call in the command window, a programmer might write<br />

System.out.println( Math.sqrt( 900.0 ) );<br />

In this statement, the value that sqrt returns becomes the argument <strong>to</strong> method println.<br />

Software Engineering Observation 6.2<br />

It is not necessary <strong>to</strong> import class Math <strong>to</strong> use its methods. Math is part of the java.lang<br />

package, which is au<strong>to</strong>matically imported by the compiler. 6.2<br />

Common <strong>Program</strong>ming Error 6.1<br />

Forgetting <strong>to</strong> invoke a Math class method by preceding the name of the method with the<br />

class name Math and a dot opera<strong>to</strong>r (.) results in a syntax error. 6.1<br />

Method arguments may be constants, variables or expressions. If c1 = 13.0, d=3.0<br />

and f = 4.0, then the statement<br />

System.out.println( Math.sqrt( c1 + d * f ) );<br />

calculates and prints the square root of 13.0 + 3.0 * 4.0 = 25.0, namely 5.0.<br />

Some Math class methods are summarized in Fig. 6.2. In the figure, the variables x<br />

and y are of type double. The Math class also defines two commonly used mathematical<br />

constants: Math.PI and Math.E. The constant Math.PI (3.14159265358979323846)<br />

of class Math is the ratio of a circle’s circumference <strong>to</strong> its diameter. The constant Math.E<br />

(2.7182818284590452354) is the base value for natural logarithms (calculated with static<br />

Math method log).<br />

6.4 Methods<br />

Methods allow the programmer <strong>to</strong> modularize a program. Variables declared in method<br />

definitions are local variables—only the method that defines them knows they exist. Most<br />

methods have a list of parameters that provide the means for communicating information<br />

between methods via method calls. A method’s parameters are also local variables.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!