Java Reference
In-Depth Information
13
// prompt for and input three floating-point values
14
System.out.print(
15
"Enter three floating-point values separated by spaces: "
);
16
double
number1 = input.nextDouble();
// read first double
17
double
number2 = input.nextDouble();
// read second double
18
double
number3 = input.nextDouble();
// read third double
19
20
// determine the maximum value
21
double
result = maximum(number1, number2, number3);
22
23
// display maximum value
24
System.out.println(
"Maximum is: "
+ result
);
25
}
26
27
// returns the maximum of its three double parameters
public static double
maximum(
double
x,
double
y,
double
z)
{
double
maximumValue = x;
// assume x is the largest to start
// determine whether y is greater than maximumValue
if
(y > maximumValue)
maximumValue = y;
// determine whether z is greater than maximumValue
if
(z > maximumValue)
maximumValue = z;
return
maximumValue;
}
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
}
// end class MaximumFinder
Enter three floating-point values separated by spaces:
9.35 2.74 5.1
Maximum is: 9.35
Enter three floating-point values separated by spaces:
5.8 12.45 8.32
Maximum is: 12.45
Enter three floating-point values separated by spaces:
6.46 4.12 10.54
Maximum is: 10.54
Fig. 6.3
|
Programmer-declared method
maximum
with three
double
parameters. (Part 2 of 2.)
The
public
and
static
Keywords
Method
maximum
's declaration begins with keyword
public
to indicate that the method is
“available to the public”—it can be called from methods of other classes. The keyword
static
enables the
main
method (another
static
method) to call
maximum
as shown in
line 21 without qualifying the method name with the class name
MaximumFinder
—
static
methods in the same class can call each other directly. Any other class that uses
maximum
must fully qualify the method name with the class name.