Java Reference
In-Depth Information
LISTING 5.2
continued
System.out.println ();
// Pay overtime at "time and a half"
if
(hours > STANDARD)
pay = STANDARD * RATE + (hours-STANDARD) * (RATE * 1.5);
else
pay = hours * RATE;
NumberFormat fmt = NumberFormat.getCurrencyInstance();
System.out.println ("Gross earnings: " + fmt.format(pay));
}
}
OUTPUT
Enter the number of hours worked:
46
Gross earnings: $404.25
This example makes use of an object called
roster
. Even without knowing
what
roster
represents, or from what class it was created, we can see that it
has at least three methods:
getSize
,
expand
, and
addName
. The condition of the
if
statement calls
getSize
and compares the result to the constant
FULL
. If the
condition is true, the
expand
method is invoked (apparently to expand the size of
the roster). If the roster is not yet full, the variable
name
is passed as a parameter
to the
addName
method.
The program in Listing 5.3 instantiates a
Coin
object, flips the coin by call-
ing the
flip
method, then uses an
if-else
statement to determine which of two
sentences gets printed based on the result.
LISTING 5.3
//********************************************************************
// CoinFlip.java Author: Lewis/Loftus
//
// Demonstrates the use of an if-else statement.
//********************************************************************
Search WWH ::
Custom Search