/* CS 211 Accel. Stream H/W #1 */

/**
 * Encapsulates a mutable <code>int</code>.   A <code>MyInteger</code>
 * object stores a single integer value, and that integer value may
 * change during the lifetime of the object.
 *
 * @author   CS211 Staff
 * @version  0.9, 09/09/2000
 */

public class MyInteger {
    /**
     * The stored integer.
     */
    private int value;

    /**
     * Constructs a new <code>MyInteger</code> object that initially stores
     * the integer value <code>i</code>.
     *
     * @param  i  the integer value to initially store in the new object
     */
    public MyInteger(int i) {
        value = i;
    }

    /**
     * Retrieves the stored integer.
     *
     * @returns  the integer value stored in this object
     */
    public int getValue() {
        return value;
    }

    /**
     * Returns <code>true</code> if <code>this</code> is less than
     * <code>i</code>.
     *
     * @param   i  the <code>MyInteger</code> to compare against
     * @return  <code>true</code> if <code>this</code> is less than
     *          <code>i</code>; <code>false</code> otherwise
     */
    public boolean lessThan(MyInteger i) {
        return (value < i.getValue());
    }

    /**
     *  Displays the stored integer's value on the terminal.
     */
    public void showValue() {
        System.out.println(this);
    }

    /**
     * Returns <code>true</code> if <code>this</code> divides <code>i</code>,
     * without leaving any remainder.
     * <p>
     * The <code>divides</code> method's behavior is undefined when
     * <code>this</code> is zero.
     *
     * @param   i  the <code>MyInteger</code> to divide into
     * @return  <code>true</code> if <code>i</code> is divisible by
     *          <code>this</code>; <code>false</code> otherwise
     */
    public boolean divides(MyInteger i) {
        return ((i.getValue() % value) == 0);
    }

    /**
     * Replaces the value of <code>this</code> with <code>this + i</code>.
     *
     * @param  i  the <code>MyInteger</code> which is to be added to
     *            <code>this</code>
     */
    public void plus(MyInteger i) {
        value += i.getValue();
    }

    /**
     * Returns a new object holding the square of the value of
     * <code>this</code>.
     * 
     * @return  a new <code>MyInteger</code> that initially stores
     *          the square of this object's integer value.
     */
    public MyInteger squareOf() {
        int newval = value*value;
        MyInteger sqr = new MyInteger(newval);
        return sqr;
    }

    /*
     *
     * [in general, one should always make sure that the following
     * four methods, declared in Object, have sane implementations...]
     *
     */

    /**
     * Retrieves the decimal representation of the stored integer.
     *
     * @return  a <code>String</code> containing the signed decimal that
     *          corresponds to the stored integer
     */
    public String toString() {
        return String.valueOf(value);
    }

    /**
     * Returns <code>true</code> if (1) <code>o</code> is a
     * <code>MyInteger</code> and (2) <code>o</code> and
     * <code>this</code> store the same value.
     *
     * @return  <code>true</code> if <code>o</code> is a <code>MyInteger</code>
     *          that stores the same integer value as <code>this</code>
     *          does; <code>false</code> otherwise
     */
    public boolean equals(Object o) {
        return (o instanceof MyInteger)
            && (value == ((MyInteger) o).value);
    }

    /**
     * Returns a hash code value for this <code>MyInteger</code>.
     *
     * @return  the integer value stored in this object
     */
    public int hashCode() {
        return value;
    }

    /**
     * Returns a copy of this <code>MyInteger</code>.
     *
     * @return  a new <code>MyInteger</code> that takes <code>this</code>'s
     *          integer value as its initial stored value
     */
    public Object clone() {
        return new MyInteger(value);
    }
}
