Java Reference
In-Depth Information
Modify the
BigRational
class so that 0/0 is legal and is interpreted as
“Indeterminate” by
toString
.
3.23
Write a program that reads a data file containing rational numbers,
one per line, stores the numbers in an
ArrayList
, removes any dupli-
cates, and then outputs the sum, arithmetic mean, and harmonic mean
of the remaining unique rational numbers.
3.24
Suppose you would like to print a two dimensional array in which all
numbers are between 0 and 999. The normal way of outputting each
number might leave the array misaligned. For instance:
54 4 12 366 512
756 192 18 27 4
14 18 99 300 18
Examine the documentation for the
format
method in the
String
class
and write a routine that outputs the two-dimensional array in a nicer
format, such as
54 4 12 366 512
756 192 18 27 4
14 18 99 300 18
3.25
Package
java.math
contains a class
BigDecimal
, used to represent an
arbitrary-precision decimal number. Read the documentation for
BigDecimal
and answer the following questions:
a.
3.26
Is
BigDecimal
an immutable class?
b.
If
bd1.equals(bd2)
is true, what is
bd1.compareTo(bd2)
?
c.
If
bd1.compareTo(bd2)
is 0, when is
bd1.equals(bd2)
false?
d.
If
bd1
represents 1.0 and
bd2
represents 5.0, by default what is
bd1.divide(bd2)
?
e.
If
bd1
represents 1.0 and
bd2
represents 3.0, by default what is
bd1.divide(bd2)
?
f.
What is
MathContext.DECIMAL128
?
g.
Modify the
BigRational
class to store a
MathContext
that can be
initialized from an additional
BigRational
constructor (or which
defaults to
MathContext.UNLIMITED
). Then add a
toBigDecimal
method to the
BigRational
class.
An
Account
class stores a current balance, and provides
getBalance
,
deposit
,
withdraw
, and
toString
methods in addition to at least one
constructor. Write and test an
Account
class. Make sure your withdraw
method throws an exception if appropriate.
3.27
Search WWH ::
Custom Search