-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRealNumber.java
60 lines (51 loc) · 1.39 KB
/
RealNumber.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class RealNumber extends Number{
private double value;
public RealNumber(double v){
value = v;
}
public double getValue(){
return value;
}
public String toString(){
return ""+getValue();
}
//---------ONLY EDIT BELOW THIS LINE------------
/*
*Return true when the values are within 0.001% of eachother.
*Special case: if one is exactly zero, the other must be exactly zero.
*/
/*
*Return a new RealNumber that has the value of:
*the sum of this and the other
*/
public RealNumber add(RealNumber other){
//other can be ANY RealNumber, including a RationalNumber
//or other subclasses of RealNumber (that aren't written yet)
RealNumber a = new RealNumber(value + other.getValue());
return a;
}
/*
*Return a new RealNumber that has the value of:
*the product of this and the other
*/
public RealNumber multiply(RealNumber other){
RealNumber a = new RealNumber(value * other.getValue());
return a;
}
/*
*Return a new RealNumber that has the value of:
*this divided by the other
*/
public RealNumber divide(RealNumber other){
RealNumber a = new RealNumber(value / other.getValue());
return a;
}
/*
*Return a new RealNumber that has the value of:
*this minus the other
*/
public RealNumber subtract(RealNumber other){
RealNumber a = new RealNumber(value - other.getValue());
return a;
}
}