-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRationalNumber.java
135 lines (126 loc) · 3.58 KB
/
RationalNumber.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
public class RationalNumber extends Number
{
private int numerator, denominator;
/**Initialize the RationalNumber with the provided values
* if the denominator is 0, make the fraction 0/1 instead
* If the denominator is negative, negate both numerator and denominator
*@param nume the numerator
*@param deno the denominator
*/
public RationalNumber(int nume, int deno){
//super(0.0);//this value is ignored!
if (deno == 0){
numerator = 0;
denominator = 1;
}
else if (deno < 0){
numerator = nume * -1;
denominator = deno * -1;
reduce();
}
else{
numerator = nume;
denominator = deno;
reduce();
}
}
public double getValue(){
return (double)numerator/denominator;
}
/**
*@return the numerator
*/
public int getNumerator(){
return numerator;
}
/**
*@return the denominator
*/
public int getDenominator(){
return denominator;
}
/**
*@return a new RationalNumber that has the same numerator
*and denominator as this RationalNumber but reversed.
*/
public RationalNumber reciprocal(){
RationalNumber a = new RationalNumber(denominator,numerator);
return a;
}
/**
*@return true when the RationalNumbers have the same numerators and denominators, false otherwise.
*/
public boolean equals(RationalNumber other){
if (numerator == other.getNumerator() && denominator == other.getDenominator()){
return true;
}
else{
return false;
}
}
/**
*@return the value expressed as "3/4" or "8/3"
*/
public String toString(){
if (denominator == 1 && numerator != 0){
return numerator+"";
}
if (numerator == 0){
return "0";
}
return (numerator + "/" + denominator);
}
/**Calculate the GCD of two integers.
*@param a the first integers
*@param b the second integer
*@return the value of the GCD
*/
private static int gcd(int a, int b){
int largest = 1;
for (int i = 1; i <= Math.min(a,b);i++){
if (a % i == 0 && b % i == 0){
largest = i;
}
}
return largest;
}
/**
*Divide the numerator and denominator by the GCD
*This must be used to maintain that all RationalNumbers are
*reduced after construction.
*/
private void reduce(){
int GCD1 = gcd(Math.abs(numerator),Math.abs(denominator));
numerator = numerator / GCD1;
denominator = denominator / GCD1;
}
/******************Operations Return a new RationalNumber!!!!****************/
/**
*Return a new RationalNumber that is the product of this and the other
*/
public RationalNumber multiply(RationalNumber other){
RationalNumber e = new RationalNumber(other.getNumerator() * numerator,other.getDenominator() * denominator) ;
return e;
}
/**
*Return a new RationalNumber that is the this divided by the other
*/
public RationalNumber divide(RationalNumber other){
RationalNumber a = new RationalNumber(other.getDenominator() * numerator,other.getNumerator() * denominator);
return a;
}
/**
*Return a new RationalNumber that is the sum of this and the other
*/
public RationalNumber add(RationalNumber other){
RationalNumber b = new RationalNumber(other.getNumerator() * denominator + numerator * other.getDenominator(),other.getDenominator() * denominator);
return b;
}
/**
*Return a new RationalNumber that this minus the other
*/
public RationalNumber subtract(RationalNumber other){
RationalNumber c = new RationalNumber(other.getDenominator() * numerator - denominator * other.getNumerator(),other.getDenominator() * denominator);
return c;
}
}