Java Reference
In-Depth Information
The
Comic
class is defined in lines 42-61. There are five instance variables—the
String
object's
title
,
issueNumber
, and
condition
, and the floating-point value's
basePrice
and
price
.
The constructor method of the class, located in lines 49-56, sets the value of four
instance variables to the arguments sent to the constructor.
The
setPrice(
Float
)
method in lines 58-60 sets the price of a comic book. The argu-
ment sent to the method is a
float
value. The price of a comic is calculated by multiply-
ing this
float
by the base price of the comic. Consequently, if a book is worth $1,000,
and its multiplier is 2.0, the topic is priced at $2,000.
Hash tables are a powerful data structure for manipulating large amounts of data. The
fact that hash tables are so widely supported in the Java class library via the
Object
class
should give you a clue as to their importance in Java programming.
The data structures that you learned about today are arguably the most essential utility
classes in the Java class library.
Hash tables, vectors, stacks, and the other structures in the
java.util
package are useful
regardless of the kind of programs that you want to develop. Almost every software pro-
gram handles data in some manner.
These data structures are well suited for use in code that applies generically to a wide
range of different classes of objects. A method written to manipulate vectors could be
written to function equally well on strings, string buffers, character arrays, or other
objects that represent text. A method in an accounting program could take objects that
represent integers, floating-point numbers, and other math classes, using each to calcu-
late a balance.
This flexibility comes at a price: When a data structure works with any kind of object,
there hasn't been a way for the Java compiler to warn you when the structure is being
misused.
For instance, the
ComicBooks
application uses a hash table named
quality
to associate
condition descriptions such as “mint” and “good” with price multipliers. Here's the state-
ment for “near mint”:
quality.put(“near mint”, 1.50F);