Java Reference
In-Depth Information
(numDimes != 1 ? "s, " : ","));
numPennies -= umDimes*Coin.DIME.denomValue();
int
umNickels
=
Coin.NICKEL.toDenomination(numPennies);
System.out.println(numNickels+"
"+Coin.NICKEL.toString()+
(numNickels != 1 ? "s, " : ",
and"));
numPennies -= umNickels*Coin.NICKEL.denomValue();
System.out.println(numPennies+"
"+Coin.PENNY.toString()+
(numPennies != 1 ? "s" : ""));
}
System.out.println();
System.out.println("Denomination values:");
for (int i = 0; i < Coin.values().length; i++)
Sys-
tem.out.println(Coin.values()[i].denomValue());
}
}
Listing 3-63 describes an application that converts its solitary “pennies” command-
line argument to an equivalent amount expressed in quarters, dimes, nickels, and pen-
nies. In addition to calling a Coin constant's denomValue() and
toDenomValue() methods, the application calls toString() to output a string
representation of the coin.
Another called enum method is values() . This method returns an array of all
Coin constantsthataredeclaredinthe Coin enum( value() 'sreturntype,inthisex-
ample,is Coin[] ).Thisarrayisusefulwhenyouneedtoiterateovertheseconstants.
For example, Listing 3-63 calls this method to output each coin's denomination.
When you run this application with 119 as its command-line argument ( java
Coins 119 ), it generates the following output:
119 pennies is equivalent to:
4 QUARTERs,
1 DIME,
1 ICKEL, and
Search WWH ::




Custom Search