KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djep > groupJep > groups > Integers


1 /* @author rich
2  * Created on 05-Mar-2004
3  *
4  * This code is covered by a Creative Commons
5  * Attribution, Non Commercial, Share Alike license
6  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
7  */

8 package org.lsmp.djep.groupJep.groups;
9 import java.math.*;
10
11 import org.lsmp.djep.groupJep.interfaces.*;
12
13 /**
14  * The group of integers, implemented as a BigInteger.
15  * @see java.math.BigInteger
16  * @author Rich Morris
17  * Created on 05-Mar-2004
18  */

19 public class Integers extends Group implements IntegralDomainI,HasDivI,
20     OrderedSetI,HasModI,HasPowerI {
21     
22     /**
23      * Operations on the reals (Implemented as BigInteger).
24      */

25     public Integers() {
26     }
27
28     public Number JavaDoc getZERO() {
29         return BigInteger.ZERO;
30     }
31
32     public Number JavaDoc getONE() {
33         return BigInteger.ONE;
34     }
35
36     public Number JavaDoc getInverse(Number JavaDoc num) {
37         BigInteger a = (BigInteger) num;
38         return a.negate();
39     }
40
41     public Number JavaDoc add(Number JavaDoc num1, Number JavaDoc num2) {
42         BigInteger a = (BigInteger) num1;
43         BigInteger b = (BigInteger) num2;
44         return a.add(b);
45     }
46
47     public Number JavaDoc sub(Number JavaDoc num1, Number JavaDoc num2) {
48         BigInteger a = (BigInteger) num1;
49         BigInteger b = (BigInteger) num2;
50         return a.subtract(b);
51     }
52
53     public Number JavaDoc mul(Number JavaDoc num1, Number JavaDoc num2) {
54         BigInteger a = (BigInteger) num1;
55         BigInteger b = (BigInteger) num2;
56         return a.multiply(b);
57     }
58
59     public Number JavaDoc div(Number JavaDoc num1, Number JavaDoc num2) {
60         BigInteger a = (BigInteger) num1;
61         BigInteger b = (BigInteger) num2;
62         return a.divide(b);
63     }
64     /* note -3 mod 2 is 1 rather than -1 as for % in java language specifications. */
65     public Number JavaDoc mod(Number JavaDoc num1, Number JavaDoc num2) {
66         BigInteger a = (BigInteger) num1;
67         BigInteger b = (BigInteger) num2;
68         return a.remainder(b);
69     }
70     
71     public Number JavaDoc pow(Number JavaDoc num1, Number JavaDoc num2) {
72         BigInteger a = (BigInteger) num1;
73         BigInteger b = (BigInteger) num2;
74         return a.pow(b.intValue());
75     }
76     public boolean equals(Number JavaDoc a,Number JavaDoc b) {
77         return ((BigInteger) a).compareTo((BigInteger) b) == 0;
78     }
79     
80     public int compare(Number JavaDoc a,Number JavaDoc b) {
81         return ((BigInteger) a).compareTo((BigInteger) b);
82     }
83
84     public Number JavaDoc valueOf(String JavaDoc str) {
85         return new BigInteger(str);
86     }
87     
88     public String JavaDoc toString() { return "Z: integers"; }
89 }
90
Popular Tags