Class MyInteger

java.lang.Object
  |
  +--MyInteger

class MyInteger
extends java.lang.Object
implements MyIntegralNumber

Encapsulates a mutable int. A MyInteger object stores a single integer value, and that integer value may change during the lifetime of the object.


Constructor Summary
MyInteger(int i)
          Constructs a new MyInteger object that initially stores the integer value i.
 
Method Summary
 java.lang.Object clone()
          Returns a new MyNumber object that initially equals this.
 MyNumber createCompatible(int i)
          Returns a new MyNumber object that initially holds the value i.
 boolean divides(MyIntegralNumber n)
          Returns true if n is evenly divisible by this.
 boolean equals(java.lang.Object o)
          Tests whether o and this are equal.
 boolean lessThan(MyNumber n)
          Returns true if this is smaller than n.
 void minus()
          Replaces this with -this.
 void plus(MyNumber n)
          Replaces this with this + n.
 MyNumber squareOf()
          Returns a new object holding the square of the value of this.
 void times(MyNumber n)
          Replaces this with this * n.
 java.lang.String toString()
          Retrieves the decimal representation of the stored integer.
 
Methods inherited from class java.lang.Object
, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

MyInteger

public MyInteger(int i)
Constructs a new MyInteger object that initially stores the integer value i.
Parameters:
i - the integer value to initially store in the new object
Method Detail

clone

public java.lang.Object clone()
Description copied from interface: MyNumber
Returns a new MyNumber object that initially equals this.

[The returned object is guaranteed to be an instance of this's class.]

Overrides:
clone in class java.lang.Object
Tags copied from interface: MyNumber
Returns:
a new MyNumber (of the same class as this) that takes this's integer value as its initial stored value

createCompatible

public MyNumber createCompatible(int i)
Description copied from interface: MyNumber
Returns a new MyNumber object that initially holds the value i.

[The returned object is guaranteed to be an instance of this's class.]

Example:

   // construct n...
   MyNumber n   = new MyInteger(3);

   // increment n...
   MyNumber one = n.createCompatible(1);  // ...one is a MyInteger.
   n.plus(one);
[The behavior of the createCompatible method is undefined if i is too large (or too small) to be handled by this's variety of number.]
Tags copied from interface: MyNumber
Returns:
a new MyNumber (of the same class as this) that takes i as its initial stored value

lessThan

public boolean lessThan(MyNumber n)
Description copied from interface: MyNumber
Returns true if this is smaller than n.

[The behavior of the lessThan method is undefined if n is not compatible with this.]

Tags copied from interface: MyNumber
Parameters:
n - the number to compare against this
Returns:
true if this is less than n; false otherwise

equals

public boolean equals(java.lang.Object o)
Description copied from interface: MyNumber
Tests whether o and this are equal.
Overrides:
equals in class java.lang.Object
Tags copied from interface: MyNumber
Parameters:
o - the object to compare against this
Returns:
true if (1) o is a MyNumber that is compatible with this and (2) o and this currently store the same integer value; false otherwise

minus

public void minus()
Description copied from interface: MyNumber
Replaces this with -this.

plus

public void plus(MyNumber n)
Description copied from interface: MyNumber
Replaces this with this + n.

[The behavior of the plus method is undefined if n is not compatible with this.]

Tags copied from interface: MyNumber
Parameters:
n - the number to be added to this

squareOf

public MyNumber squareOf()
Description copied from interface: MyNumber
Returns a new object holding the square of the value of this.

[The returned object is guaranteed to be an instance of this's class.]

Tags copied from interface: MyNumber
Returns:
a new MyNumber object (of the same class as this) that initially stores the square of this object's integer value.

times

public void times(MyNumber n)
Description copied from interface: MyNumber
Replaces this with this * n.

[The behavior of the times method is undefined if n is not compatible with this.]

Tags copied from interface: MyNumber
Parameters:
n - the number by which this is to be scaled

divides

public boolean divides(MyIntegralNumber n)
Description copied from interface: MyIntegralNumber
Returns true if n is evenly divisible by this.

[The divides method's behavior is undefined when this is zero.]

Specified by:
divides in interface MyIntegralNumber
Tags copied from interface: MyIntegralNumber
Parameters:
n - the integral number to divide into
Returns:
true if n is divisible by this; false otherwise

toString

public java.lang.String toString()
Description copied from interface: MyNumber
Retrieves the decimal representation of the stored integer.
Overrides:
toString in class java.lang.Object
Tags copied from interface: MyNumber
Returns:
a String that represents the stored integer, in decimal