-
Notifications
You must be signed in to change notification settings - Fork 11
/
Decimal.java
40 lines (37 loc) · 1.04 KB
/
Decimal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package mae.tut.num;
/** Represents a decimal number, implemented as a float.
* <P>
* <P>
* @author Eyler
* @version 2.0
*/
public class Decimal extends Ordinal {
/** Value of this Number */
float num;
/** Use factory method {@link mae.tut.num.Factory#newDecimal(float)},
* rather than this constructor
*/
public Decimal(float x) { num = x; }
public Number add(Number n) {
if (n instanceof Ordinal) {
Ordinal r = (Ordinal)n;
return Factory.newDecimal(num + n.value());
} else {
return n.add(this);
}
}
public Number mult(Number n) {
if (n instanceof Ordinal) {
Ordinal r = (Ordinal)n;
return Factory.newDecimal(num * n.value());
} else {
return n.mult(this);
}
}
public Number inverse() {
return Factory.newDecimal(1/num);
}
public float value() { return num; }
/** String representation of this Number */
public String toString() { return num+""; }
}