-
Notifications
You must be signed in to change notification settings - Fork 0
/
RationalNumber.java
98 lines (97 loc) · 2.61 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
package lab3;
public class RationalNumber extends SpecialNumber
{
int num;
int den;
/**
* The constructor accepts 2 inputs of type int
*/
public RationalNumber(int n, int d) throws Lab3Exception
{
if (d == 0)
throw new Lab3Exception("Denominator cannot be zero");
num = n;
den = d;
}
/**
* The add function multiplies the numerators by the other denominator
* and adds them and multiplies the denominators together to get a new
* rational number that works in the general case regardless of differing
* denominator
*/
public RationalNumber add(SpecialNumber other) throws Lab3Exception
{
if (!(other instanceof RationalNumber))
throw new Lab3Exception("Cannot add an incompatible type");
RationalNumber other2 = (RationalNumber)other;
int n = (num*other2.den)+other2.num*den;
int d = (den*other2.den);
RationalNumber x = new RationalNumber(n,d);
return x;
}
/**
* Divides the rational number by an integer by multiplying the denominator
* by that integer
*/
public SpecialNumber divideByInt(int i) throws Lab3Exception
{
if (i == 0)
throw new Lab3Exception("Cannot divide by zero");
int d = den * i;
RationalNumber x = new RationalNumber(num,d);
return x;
}
/**
* Returns the RationalNumber as a hashcode by reducing the
* rational number into simplest form, then multiplying the numerator by the denominator,
* thus producing a unique hashcode for each rational number.
*/
public int hashCode()
{
int reduced_num = num;
int reduced_den = den;
int i = 2;
while (i <= Math.min(reduced_num,reduced_den))
{
if (reduced_num % i == 0 && reduced_den % i == 0)
{
reduced_num = reduced_num / i;
reduced_den = reduced_den / i;
}
if (reduced_num % i != 0 || reduced_den % i != 0)
i++;
}
int result = reduced_num * reduced_den;
return result;
}
/**
* This comparison function directly divides the numerator
* and denominator of each rational number and compares the
* end result to see if they are equal or which one is great than
* the other
*/
@Override
public int compareTo(Object x)
{
RationalNumber y = (RationalNumber)x;
if (num/den == y.num/y.den)
return 0;
if (num/den > y.num/y.den)
return 1;
return -1;
}
/**
* Uses the comparison function to find whether 2 rational numbers
* are equal to each other
*/
@Override
public boolean equals(Object x)
{
if (!(x instanceof RationalNumber))
return false;
RationalNumber y = (RationalNumber)x;
if (compareTo(y) == 0)
return true;
return false;
}
}