Java Reference
In-Depth Information
Price: $2650.0
Title: Cerebus
Issue: 1A
Condition: good
Price: $22.5
8
The
ComicBooks
application is implemented as two classes: an application class called
ComicBooks
and a helper class called
Comic
.
In the application, the hash table is created in lines 9-22.
First, the hash table is created in line 10.
Next, a
float
called
price1
is created with the value
3.00
. This value is added to the
hash table and associated with the key
“mint”
. (Remember that hash tables, like other
data structures, can only hold objects—the float value is automatically converted to a
Float
object through autoboxing.)
The process is repeated for each of the other comic book conditions from “near mint” to
“poor.”
After the hash table is set up, an array of
Comic
objects called
comix
is created to hold
each comic book currently for sale.
The
Comic
constructor is called with four arguments: the topic's title, issue number, con-
dition, and base price. The first three are strings, and the last is a
float
.
After a
Comic
has been created, its
setPrice(
float
)
method is called to set the topic's
price based on its condition. Here's an example, line 27:
comix[0].setPrice( (Float)quality.get(comix[0].condition) );
The hash table's
get(
String
)
method is called with the condition of the topic, a
String
that is one of the keys in the table. An
Object
is returned that represents the value asso-
ciated with that key. (In line 27, because
comix[0].condition
is equal to “very fine,”
get()
returns the floating-point value
3.00F
.)
Because
get()
returns an
Object
, it must be cast as a
Float
. The
Float
argument is
unboxed as a
float
value automatically through unboxing.
This process is repeated for two more topics.
In lines 33-38, information about each comic book in the
comix
array is displayed.