Java Reference
In-Depth Information
}
/* Implement a toy stack */
protected
protected int
int pop () {
return
return stack [-- depth ];
}
}
Several of the public methods are required because I wanted it to be a subclass of Format ,
which is abstract. This accounts for some of the complexity, like having three different
format methods.
Note that the parseObject( ) method is also required, but we don't actually implement
parsing in this version. This is left as an exercise for the reader.
See Also
Java I/O (O'Reilly) includes an entire chapter on NumberFormat and develops the subclass
ExponentialNumberFormat .
The online source for this topic has ScaledNumberFormat , which prints numbers with a
maximum of four digits and a computerish scale factor (B for bytes, K for kilo-, M for mega-
, and so on).
Formatting with Correct Plurals
Problem
You're printing something like "We used " + n + " items" , but in English, “We used 1
items” is ungrammatical. You want “We used 1 item.”
Solution
Use a ChoiceFormat or a conditional statement.
Use Java's ternary operator ( cond ? trueval : falseval ) in a string concatenation. Both
zero and plurals get an “s” appended to the noun in English (“no books, one book, two
books”), so we test for n==1 :
Search WWH ::




Custom Search