KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > MathInteger


1 package JSci.maths;
2
3 import java.lang.Comparable JavaDoc;
4
5 import JSci.GlobalSettings;
6 import JSci.maths.groups.AbelianGroup;
7 import JSci.maths.fields.*;
8
9 /**
10 * The MathInteger class encapsulates integer numbers.
11 * @see JSci.maths.fields.IntegerRing
12 * @version 1.0
13 * @author Mark Hale
14 */

15 public final class MathInteger extends Number JavaDoc implements Comparable JavaDoc, Ring.Member {
16         private static final long serialVersionUID = 6893485894391864141L;
17
18         private final int x;
19         /**
20         * Constructs an integer number.
21         */

22         public MathInteger(final int num) {
23                 x=num;
24         }
25         /**
26         * Constructs the integer number represented by a string.
27         * @param s a string representing an integer number.
28         * @exception NumberFormatException if the string does not contain a parsable number.
29         */

30         public MathInteger(final String JavaDoc s) throws NumberFormatException JavaDoc {
31                 x=Integer.parseInt(s);
32         }
33         /**
34         * Compares two integer numbers for equality.
35         * @param obj an integer number.
36         */

37         public boolean equals(Object JavaDoc obj) {
38                 if(obj instanceof MathInteger)
39                         return x == ((MathInteger)obj).value();
40                 else
41                         return false;
42         }
43     public int hashCode() {
44         return x;
45     }
46         /**
47         * Compares two integer numbers.
48         * @param obj an integer number.
49         * @return a negative value if <code>this&lt;obj</code>,
50         * zero if <code>this==obj</code>,
51         * and a positive value if <code>this&gt;obj</code>.
52         */

53         public int compareTo(Object JavaDoc obj) throws IllegalArgumentException JavaDoc {
54                 if(obj!=null && (obj instanceof MathInteger)) {
55             int objValue = ((MathInteger)obj).x;
56                         if(x == objValue)
57                                 return 0;
58                         else
59                                 return x-objValue;
60                 } else
61                         throw new IllegalArgumentException JavaDoc("Invalid object: "+obj.getClass());
62         }
63         /**
64         * Returns a string representing the value of this integer number.
65         */

66         public String JavaDoc toString() {
67                 return Integer.toString(x);
68         }
69         /**
70         * Returns the integer value.
71         */

72         public int value() {
73                 return x;
74         }
75         public int intValue() {
76                 return x;
77         }
78         public long longValue() {
79                 return x;
80         }
81         public float floatValue() {
82                 return x;
83         }
84         public double doubleValue() {
85                 return x;
86         }
87         /**
88         * Returns true if this number is even.
89         */

90         public boolean isEven() {
91                 return (x&1)==0;
92         }
93         /**
94         * Returns true if this number is odd.
95         */

96         public boolean isOdd() {
97                 return (x&1)==1;
98         }
99     public Object JavaDoc getSet() {
100         return IntegerRing.getInstance();
101     }
102         /**
103         * Returns the negative of this number.
104         */

105         public AbelianGroup.Member negate() {
106                 return new MathInteger(-x);
107         }
108         /**
109         * Returns the addition of this number and another.
110         */

111         public AbelianGroup.Member add(final AbelianGroup.Member n) {
112                 if(n instanceof MathInteger)
113                         return add((MathInteger)n);
114                 else
115                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method: "+n.getClass());
116         }
117         /**
118         * Returns the addition of this integer number and another.
119         */

120         public MathInteger add(final MathInteger n) {
121                 return add(n.x);
122         }
123     public MathInteger add(int y) {
124         return new MathInteger(x+y);
125     }
126         /**
127         * Returns the subtraction of this number and another.
128         */

129         public AbelianGroup.Member subtract(final AbelianGroup.Member n) {
130                 if(n instanceof MathInteger)
131                         return subtract((MathInteger)n);
132                 else
133                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method: "+n.getClass());
134         }
135         /**
136         * Returns the subtraction of this integer number and another.
137         * @param n an integer number.
138         */

139         public MathInteger subtract(final MathInteger n) {
140                 return subtract(n.x);
141         }
142     public MathInteger subtract(int y) {
143         return new MathInteger(x-y);
144     }
145         /**
146         * Returns the multiplication of this number and another.
147         */

148         public Ring.Member multiply(final Ring.Member n) {
149                 if(n instanceof MathInteger)
150                         return multiply((MathInteger)n);
151                 else
152                         throw new IllegalArgumentException JavaDoc("Member class not recognised by this method: "+n.getClass());
153         }
154         /**
155         * Returns the multiplication of this integer number and another.
156         * @param n an integer number.
157         */

158         public MathInteger multiply(final MathInteger n) {
159                 return multiply(n.x);
160         }
161     public MathInteger multiply(int y) {
162         return new MathInteger(x*y);
163     }
164         /**
165         * Returns this integer number raised to the power of another.
166         * @param n an integer number.
167         */

168         public MathInteger pow(final MathInteger n) {
169                 if(n.x==0)
170                         return IntegerRing.ONE;
171                 else {
172                         int ans=x;
173                         for(int i=1;i<n.x;i++)
174                                 ans*=x;
175                         return new MathInteger(ans);
176                 }
177         }
178 }
179
Popular Tags