Page 190 - Introduction to Programming with Java: A Problem Solving Approach
P. 190

                156 Chapter 5 Using Pre-Built Methods
numeric value based on one or more other numeric values. For example, a square root function generates the square root of a given number. Likewise, the Math class’s sqrt method returns the square root of a given number. In addition to providing mathematical methods, the Math class also provides two mathematical constants—􏰁 (the ratio of a circle’s circumference to its diameter) and e (the base of natural logarithms).
Basic Math Methods
Let’s now look at some of the Math class’s methods. Throughout the book, when there’s a need to present a group of methods from the API library, we’ll introduce the methods by showing a list of method headings and associated brief descriptions. Headings for API methods are commonly referred to as API headings. Figure 5.2 contains API headings for some of the more popular methods in the Math class, with associated brief descriptions.
As you read through Figure 5.2, we hope that you’ll find most of the methods to be straightforward. But some items may need clarification. Note the static modifier at the left of all the Math methods. All the methods in the Math class are static. The static modifier means they are class methods and must be called by prefacing the method’s name with the name of the class in which they are defined. For example, here’s how you’d call the abs method:
Call Math methods by prefacing them with Math dot. int num = Math.abs(num);
   The above statement updates num’s value, so num gets the absolute value of its original value. For example, Apago PDF Enhancer
if num starts out with 􏰂15, it ends up with 15.
Note that the following statement does not work properly:
Math.abs(num);
It finds the absolute value of num, but it does not update the content stored inside num. Math methods return a value. They do not update a value. So if you want to update a value, you must use an assignment operator.
In Figure 5.2, note that there’s only one pow method—one with double parameters. There’s no pow method with int parameters. But that’s no big deal because you can pass an int value to the pow method. More generally, it’s legal to pass an integer value to a method that accepts a floating-point argument. It’s like assigning an integer value into a floating-point variable, discussed in Chapter 3. Let’s see how this works within a code fragment. There is an empirical rule called “Horton’s Law,” which says that the length of a river scales with the area drained by the river in accordance with this formula:
length ≈ 1.4 (area)0.6
Here’s how you might implement Horton’s Law in Java code:
  OK to pass an int (area), into pow, which accepts double arguments.
 int area = 10000;
// square miles drained
System.out.println("river length = " + 1.4 * Math.pow(area, 0.6));
Output:
 river length = 351.66410041134117















































































   188   189   190   191   192