KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.lsmp.djep.groupJep.values.*;
13
14 /**
15  * The Field of rational numbers.
16  *
17  * @author Rich Morris
18  * Created on 05-Mar-2004
19  */

20 public class Rationals extends Group implements FieldI,OrderedSetI,HasPowerI {
21
22     private Rational ZERO = new Rational(BigInteger.valueOf(0));
23     private Rational ONE = new Rational(BigInteger.valueOf(1));
24
25     public Rationals() {}
26
27     public Number JavaDoc getZERO() {
28         return ZERO;
29     }
30
31     public Number JavaDoc getONE() {
32         return ONE;
33     }
34
35     public Number JavaDoc getInverse(Number JavaDoc num) {
36         return ((Rational) num).negate();
37     }
38
39     public Number JavaDoc getMulInverse(Number JavaDoc num) {
40         return ((Rational) num).inverse();
41     }
42
43     public Number JavaDoc add(Number JavaDoc a, Number JavaDoc b) {
44         return ((Rational) a).add((Rational) b);
45     }
46
47     public Number JavaDoc sub(Number JavaDoc a, Number JavaDoc b) {
48         return ((Rational) a).sub((Rational) b);
49     }
50
51     public Number JavaDoc mul(Number JavaDoc a, Number JavaDoc b) {
52         return ((Rational) a).mul((Rational) b);
53     }
54
55     public Number JavaDoc div(Number JavaDoc a, Number JavaDoc b) {
56         return ((Rational) a).div((Rational) b);
57     }
58
59     public Number JavaDoc pow(Number JavaDoc a, Number JavaDoc b) {
60         return ((Rational) a).pow((Rational) b);
61     }
62
63     public boolean equals(Number JavaDoc a,Number JavaDoc b)
64     {
65         return ((Rational) a).compareTo((Rational) b) == 0;
66     }
67     
68     public int compare(Number JavaDoc a,Number JavaDoc b)
69     {
70         return ((Rational) a).compareTo((Rational) b);
71     }
72     
73     public Number JavaDoc valueOf(String JavaDoc s) {
74         return Rational.valueOf(s);
75     }
76     
77     public String JavaDoc toString() { return "Q"; }
78 }
79
Popular Tags