Chapter 5 - Using Pre-Built Methods
Chapter 5 - Using Pre-Built Methods
API Headings
To use an API class, you don't need to know the
internals of the class; you just need to know how to
"interface" with it.
To interface with a class, you need to know how to
use the methods within the class. For example, to
perform input, you need to know how to use the
Scanner class's methods - next, nextLine,
nextInt, nextDouble, etc.
To use a method, you need to know what type of
argument(s) to pass to it and what type of value it
returns.
The standard way to show that information is to show
the method's source code heading.
6
API Headings
For example, here's the source code heading for the Scanner
class's nextInt method:
public int nextInt()
Math Class
Source code headings for API methods are commonly
referred to as API headings.
Here are the API headings for some of the more
popular methods in the Math class:
public static int abs(int num)
Returns the absolute value of num.
public static double abs(double num)
Returns the absolute value of num.
public static int max(int x, int y)
Returns the larger value of x and y.
public static double max(double x, double y)
Returns the larger value of x and y.
8
Math Class
Math class API headings (continued):
public static int min(int x, int y)
Returns the smaller value of x and y.
public static double min(double x, double y)
Returns the smaller value of x and y.
public static double pow(double num, double power)
Returns num raised to the specified power.
Math Class
Note the static modifier at the left of all the Math methods. All
the methods in the Math class are static methods (also called
class methods), which means they are called by prefacing the
method's name with the name of the class in which they are
defined. For example:
int position1 = 15, position2 = 18;
int distanceApart = Math.abs(position1 - position2);
Math Class
It is legal to pass an integer value to a method that accepts a
floating-point argument. Note the following example. Horton’s
Law says that the length of a river is related to the area drained
by the river in accordance with this formula:
length ≈ 1.4 (area)0.6 OK to pass an
Here's how to implement Horton's Law in Java code: int (area),
into pow,
int area = 10000; // square miles drained which accepts
double riverLength = 1.4 * Math.pow(area, 0.6); double
arguments.
A common use of computers is to model real-world activities that
rely on random events.
That's because computers are good at generating random
numbers and being able to repeat the random events many,
many times.
11
Math Class
The Math class contains a named constant, PI.
Pi, written as in math books, is the ratio of a circle's
perimeter to its diameter.
It contains this double value: 3.14159265358979323846
It's a constant, which means its value is fixed. If you
attempt to assign a value to it, you'll get a compilation error.
Just like Math's methods are class methods and they are
accessed using the Math class name, Math's PI is a class
variable and it is accessed using the Math class name. In
other words, if you need pi, specify Math.PI.
Complete this code fragment:
double radius = 3.0;
double volumeOfSphere =
12
Lottery Example
import java.util.Scanner;
Lottery Example
if (input.equals("give me a hint")) // a back door
{
System.out.println("try: " + winningNumber);
}
else if (!input.equals("q"))
{ Compare input
if ( with the winning
{ number.
System.out.println("YOU WIN!");
input = "q"; // force winners to quit
}
else
{
System.out.println(
"Sorry, good guess, but not quite right.");
}
} // end else if
} while (!input.equals("q"));
System.out.println("Thanks for playing. Come again!");
} // end main
} // end Lottery class
18
Here are API headers and brief descriptions for four alternative
forms of the indexOf method:
public int indexOf(int ch)
Returns the position of the first occurrence of the given ch character
within the calling-object string. Returns -1 if ch is not found.
public int indexOf(int ch, int fromIndex)
Returns the position of the first occurrence of the given ch character
within the calling-object string, starting the search at the fromIndex
position. Returns -1 if ch is not found.
public int indexOf(String str)
Returns the position of the first occurrence of the given str string within
the calling-object string. Returns -1 if str is not found.
public int indexOf(String str, int fromIndex)
Returns the position of the first occurrence of the given str string within
the calling-object string, starting the search at the fromIndex position.
Returns -1 if str is not found.
22
Conversion Character
Conversion Character
Width:
Specifies the minimum number of characters that are to be printed.
If the data item contains more than the specified number of
characters, then all of the characters are printed. If the data item
contains fewer than the specified number of characters, then spaces
are added.
By default, output values are right aligned, so when spaces are
added, they go on the left side.
30
7 characters
31
Flags
BudgetReport Example
System.out.printf(HEADING_FMT_STR,
"Account", "Actual", "Budget", "Remaining");
System.out.printf(HEADING_FMT_STR,
"-------", "------", "------", "---------");
33
BudgetReport Example
System.out.printf(
"\nTotal remaining: $%(,.2f\n", remaining1 + remaining2);
} // end main
} // end class BudgetReport