Interface Numeric

All Superinterfaces:
java.lang.Comparable

public interface Numeric
extends java.lang.Comparable

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

longValue

public long longValue()
Returns:
The ordinary Java long that this represents.
Throws:
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.).

equals

public boolean equals(java.lang.Object o)
Overrides:
equals in class java.lang.Object
Parameters:
o - The object to compare to.
Returns:
True If this has the same numeric value as o, otherwise false.
Throws:
java.lang.ClassCastException - If o cannot be cast to the same type as this.

compareTo

public int compareTo(java.lang.Object o)
Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - The object to compare to.
Returns:
-1, 0, or 1 according to whether this is less than, equal to, or greater than o.
Throws:
java.lang.ClassCastException - If o's type prevents it from being compared to this.

add

public Numeric add(Numeric val)
Returns:
The sum (this + val).
Throws:
java.lang.ClassCastException - if the types of this and val are incompatible.

subtract

public Numeric subtract(Numeric val)
Returns:
The difference (this - val).
Throws:
java.lang.ClassCastException - if the types of this and val are incompatible.

multiply

public Numeric multiply(Numeric val)
Returns:
The product (this * val).
Throws:
java.lang.ClassCastException - if the types of this and val are incompatible.

divideAndRemainder

public Numeric[] divideAndRemainder(Numeric val)
Returns:
Returns an array of two Numerics, containing the integer quotient (this / val, rounded down) and the remainder (this % val).
Throws:
java.lang.ClassCastException - if the types of this and val are incompatible.
java.lang.ArithmeticException - in case of division by zero.