1 package JSci.maths; 2 3 import java.lang.Comparable ; 4 5 import JSci.GlobalSettings; 6 import JSci.maths.groups.AbelianGroup; 7 import JSci.maths.fields.*; 8 9 15 public final class MathInteger extends Number implements Comparable , Ring.Member { 16 private static final long serialVersionUID = 6893485894391864141L; 17 18 private final int x; 19 22 public MathInteger(final int num) { 23 x=num; 24 } 25 30 public MathInteger(final String s) throws NumberFormatException { 31 x=Integer.parseInt(s); 32 } 33 37 public boolean equals(Object 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 53 public int compareTo(Object obj) throws IllegalArgumentException { 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 ("Invalid object: "+obj.getClass()); 62 } 63 66 public String toString() { 67 return Integer.toString(x); 68 } 69 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 90 public boolean isEven() { 91 return (x&1)==0; 92 } 93 96 public boolean isOdd() { 97 return (x&1)==1; 98 } 99 public Object getSet() { 100 return IntegerRing.getInstance(); 101 } 102 105 public AbelianGroup.Member negate() { 106 return new MathInteger(-x); 107 } 108 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 ("Member class not recognised by this method: "+n.getClass()); 116 } 117 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 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 ("Member class not recognised by this method: "+n.getClass()); 134 } 135 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 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 ("Member class not recognised by this method: "+n.getClass()); 153 } 154 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 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 |