001: package JSci.maths;
002:
003: import java.lang.Comparable;
004:
005: import JSci.GlobalSettings;
006: import JSci.maths.groups.AbelianGroup;
007: import JSci.maths.fields.*;
008:
009: /**
010: * The MathInteger class encapsulates integer numbers.
011: * @see JSci.maths.fields.IntegerRing
012: * @version 1.0
013: * @author Mark Hale
014: */
015: public final class MathInteger extends Number implements Comparable,
016: Ring.Member {
017: private static final long serialVersionUID = 6893485894391864141L;
018:
019: private final int x;
020:
021: /**
022: * Constructs an integer number.
023: */
024: public MathInteger(final int num) {
025: x = num;
026: }
027:
028: /**
029: * Constructs the integer number represented by a string.
030: * @param s a string representing an integer number.
031: * @exception NumberFormatException if the string does not contain a parsable number.
032: */
033: public MathInteger(final String s) throws NumberFormatException {
034: x = Integer.parseInt(s);
035: }
036:
037: /**
038: * Compares two integer numbers for equality.
039: * @param obj an integer number.
040: */
041: public boolean equals(Object obj) {
042: if (obj instanceof MathInteger)
043: return x == ((MathInteger) obj).value();
044: else
045: return false;
046: }
047:
048: public int hashCode() {
049: return x;
050: }
051:
052: /**
053: * Compares two integer numbers.
054: * @param obj an integer number.
055: * @return a negative value if <code>this<obj</code>,
056: * zero if <code>this==obj</code>,
057: * and a positive value if <code>this>obj</code>.
058: */
059: public int compareTo(Object obj) throws IllegalArgumentException {
060: if (obj != null && (obj instanceof MathInteger)) {
061: int objValue = ((MathInteger) obj).x;
062: if (x == objValue)
063: return 0;
064: else
065: return x - objValue;
066: } else
067: throw new IllegalArgumentException("Invalid object: "
068: + obj.getClass());
069: }
070:
071: /**
072: * Returns a string representing the value of this integer number.
073: */
074: public String toString() {
075: return Integer.toString(x);
076: }
077:
078: /**
079: * Returns the integer value.
080: */
081: public int value() {
082: return x;
083: }
084:
085: public int intValue() {
086: return x;
087: }
088:
089: public long longValue() {
090: return x;
091: }
092:
093: public float floatValue() {
094: return x;
095: }
096:
097: public double doubleValue() {
098: return x;
099: }
100:
101: /**
102: * Returns true if this number is even.
103: */
104: public boolean isEven() {
105: return (x & 1) == 0;
106: }
107:
108: /**
109: * Returns true if this number is odd.
110: */
111: public boolean isOdd() {
112: return (x & 1) == 1;
113: }
114:
115: public Object getSet() {
116: return IntegerRing.getInstance();
117: }
118:
119: /**
120: * Returns the negative of this number.
121: */
122: public AbelianGroup.Member negate() {
123: return new MathInteger(-x);
124: }
125:
126: /**
127: * Returns the addition of this number and another.
128: */
129: public AbelianGroup.Member add(final AbelianGroup.Member n) {
130: if (n instanceof MathInteger)
131: return add((MathInteger) n);
132: else
133: throw new IllegalArgumentException(
134: "Member class not recognised by this method: "
135: + n.getClass());
136: }
137:
138: /**
139: * Returns the addition of this integer number and another.
140: */
141: public MathInteger add(final MathInteger n) {
142: return add(n.x);
143: }
144:
145: public MathInteger add(int y) {
146: return new MathInteger(x + y);
147: }
148:
149: /**
150: * Returns the subtraction of this number and another.
151: */
152: public AbelianGroup.Member subtract(final AbelianGroup.Member n) {
153: if (n instanceof MathInteger)
154: return subtract((MathInteger) n);
155: else
156: throw new IllegalArgumentException(
157: "Member class not recognised by this method: "
158: + n.getClass());
159: }
160:
161: /**
162: * Returns the subtraction of this integer number and another.
163: * @param n an integer number.
164: */
165: public MathInteger subtract(final MathInteger n) {
166: return subtract(n.x);
167: }
168:
169: public MathInteger subtract(int y) {
170: return new MathInteger(x - y);
171: }
172:
173: /**
174: * Returns the multiplication of this number and another.
175: */
176: public Ring.Member multiply(final Ring.Member n) {
177: if (n instanceof MathInteger)
178: return multiply((MathInteger) n);
179: else
180: throw new IllegalArgumentException(
181: "Member class not recognised by this method: "
182: + n.getClass());
183: }
184:
185: /**
186: * Returns the multiplication of this integer number and another.
187: * @param n an integer number.
188: */
189: public MathInteger multiply(final MathInteger n) {
190: return multiply(n.x);
191: }
192:
193: public MathInteger multiply(int y) {
194: return new MathInteger(x * y);
195: }
196:
197: /**
198: * Returns this integer number raised to the power of another.
199: * @param n an integer number.
200: */
201: public MathInteger pow(final MathInteger n) {
202: if (n.x == 0)
203: return IntegerRing.ONE;
204: else {
205: int ans = x;
206: for (int i = 1; i < n.x; i++)
207: ans *= x;
208: return new MathInteger(ans);
209: }
210: }
211: }
|