|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
An interface for any class that supports various numeric operations.
There are many possible implementations of this interface. For example, java.math.BigInteger happens to implement it (and more), since I took care to borrow the method names from BigInteger. But in this assignment, you will give an alternative implementation that uses Russian dolls instead.
This interface is declared as extending Comparable, meaning that it supports the compareTo method. So Russian dolls will be instances of Comparable as well as Numeric and RussianDoll. This means that you could call the static method java.util.Arrays.sort in order to sort an array of Russian dolls.
The interface provides an (abstract) method longValue()
for converting instances of Numeric to ordinary Java longs. (We
use long rather than int because that's what BigInteger does.) Of
course we'd like a static method for converting in the other
direction, too. Static methods can't actually be declared in a
Java interface (there is no such thing as an abstract static
method, since static methods have no dynamic method lookup and
can't be overridden). Nonetheless, any non-abstract class Foo that
implements the Numeric interface really should define a
static method as follows:
/** Construct an instance of this class that represents a Java long. * @param val The long to represent. * @throws ClassCastException If the long cannot * be represented by this implementing class. For * example, RussianDoll cannot represent negative numbers. * / public static Foo valueOf(long val) { ... probably call a constructor for Foo ... }
Method Summary | |
Numeric |
add(Numeric val)
|
int |
compareTo(java.lang.Object o)
|
Numeric[] |
divideAndRemainder(Numeric val)
|
boolean |
equals(java.lang.Object o)
|
long |
longValue()
|
Numeric |
multiply(Numeric val)
|
Numeric |
subtract(Numeric val)
|
Method Detail |
public long longValue()
java.lang.ClassCastException
- If the number that this
represents is not expressible as a Java long (e.g.,
it is too big, too small, a fraction, etc.).public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
o
- The object to compare to.
java.lang.ClassCastException
- If o cannot be cast to the same type as this.public int compareTo(java.lang.Object o)
compareTo
in interface java.lang.Comparable
o
- The object to compare to.
java.lang.ClassCastException
- If o's type prevents it from being compared to this.public Numeric add(Numeric val)
java.lang.ClassCastException
- if the types of this and val are incompatible.public Numeric subtract(Numeric val)
java.lang.ClassCastException
- if the types of this and val are incompatible.public Numeric multiply(Numeric val)
java.lang.ClassCastException
- if the types of this and val are incompatible.public Numeric[] divideAndRemainder(Numeric val)
java.lang.ClassCastException
- if the types of this and val are incompatible.
java.lang.ArithmeticException
- in case of division by zero.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |