forked from mlitman/FractionTest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFraction.java
More file actions
133 lines (121 loc) · 2.95 KB
/
Fraction.java
File metadata and controls
133 lines (121 loc) · 2.95 KB
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
/*
* f1 = Fraction(1,2)
* class Fraction:
* __init__(self, numerator, denominator):
* self.numerator = numerator
* self.denominator = denominator
*/
public class Fraction
{
//fields - variables associated with a class
long numerator;
long denominator;
//constructor
public Fraction(long numerator, long denominator)
{
this.numerator = numerator;
this.denominator = denominator;
}
public Fraction(String f)
{
//f is in the format of "numerator/denominator" so
//somehow (using only what we have learned in class)
//divide the string and set the numerator and denominator
String numString = "";
int pos = 0;
while(f.charAt(pos) != '/')
{
numString = numString + f.charAt(pos);
pos = pos + 1;
}
pos = pos + 1; //skips past the /
String denomString = "";
while(pos < f.length())
{
denomString = denomString + f.charAt(pos);
pos = pos + 1;
}
this.numerator = this.stringToInt(numString);
this.denominator = this.stringToInt(denomString);
}
static Fraction buildFraction(long numerator, long denominator)
{
return new Fraction(numerator, denominator);
}
//methods
public Fraction add(Fraction f)
{
//adds f to "this" Fraction and returns
//a new Fraction object that is the sum
//of the two
long denom = this.denominator * f.denominator;
long num = (this.denominator * f.numerator) +
(f.denominator * this.numerator);
Fraction answer = new Fraction(num, denom);
return answer;
}
public void reduceEuclidean()
{
long a = this.numerator;
long b = this.denominator;
long t;
while(b != 0)
{
t = b;
b = a % b;
a = t;
}
//a is now our GCD of numerator and denominator
this.numerator = this.numerator / a;
this.denominator = this.denominator / a;
}
public void reduce()
{
long smallest = this.numerator;
if(this.denominator < smallest)
{
smallest = this.denominator;
}
//smallest is the smaller of the 2 numbers
//Find the GCD (Greatest Common Divisor)
while(smallest >= 1)
{
if(this.numerator % smallest == 0 &&
this.denominator % smallest == 0)
{
//we have found the GCD, it is smallest
this.numerator = this.numerator / smallest;
this.denominator = this.denominator / smallest;
return; //I have found and applied the GCD, so kill the method
}
else
{
smallest = smallest - 1;
}
}
}
private int stringToInt(String dec)
{
int sum = 0;
int place = 1;
String map = "0123456789";
for(int i = dec.length()-1; i >= 0; i = i - 1)
{
sum = sum + (place * map.indexOf(dec.charAt(i)));
place = place * 10;
}
return sum;
}
//this overrides the default toString method we inherited
//from the Object class and allows us to do our own thing
//historic function - returns a value
public String toString()
{
return this.numerator + "/" + this.denominator;
}
//historic procedure - does NOT return a value
public void display()
{
System.out.println(this.numerator + "/" + this.denominator);
}
}