-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fraction.java
165 lines (146 loc) · 5.41 KB
/
Fraction.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*************************************************************
* Microsoft: DEV277x - Object Oriented Programming in Java
* Module 2 Project
* ***********************
* FRACTION CALCULATOR
* ***********************
* Author: Zaryab Muhammad Akram
* 2/2/2018
* NNNNNNNNNNNNN Fraction Class NNNNNNNNNNNNNNNN
*************************************************************/
import java.util.InputMismatchException;
public class Fraction {
//fields
private int numerator = 0;
private int denominator = 0;
//two parameter constructor
public Fraction(int numerator, int denominator) {
this.numerator = numerator;
if (denominator == 0) { /*throw an IllegalArgumentException if the denominator is zero*/
throw new IllegalArgumentException(Integer.toString(denominator));
} else if(denominator < 0) {
this.numerator *= -1;
this.denominator = (-1) * denominator;
} else {
this.denominator = denominator;
}
}
//one parameter constructor
public Fraction(int numerator) {
this(numerator, 1);
}
//zero parameter constructor
public Fraction() {
this(0, 1);
}
/*
* This method exposes the value of the numerator field to the user
*/
public int getNumerator(){
return this.numerator;
}
/*
* This method exposes the value of the denominator field to the user
*/
public int getDenominator(){
return this.denominator;
}
/*
* This method returns a String representation of the Fraction
*/
public String toString() {
return this.numerator + "/" + this.denominator;
}
/*
*This method gives the result of numerator / denominator
*/
public double toDouble(){
return (double)numerator /denominator; /*type-casting to double so that integer division does not occur*/
}
/*
*This method takes in two ints and returns the Greatest Common Divisor.
*/
private static int gcd(int num, int den){
if (den == 0) {
return num;
}
return gcd(den, num % den);
}
/*
*This method takes in two ints and returns the Lowest Common Multiple of the denominators.
*/
private static int lcm(int den1, int den2){
int numGCD = gcd(den1, den2);
return (den1 * den2)/ numGCD;
}
/*
*This method returns a new Fraction that is the sum of other and this fractions.
*/
public Fraction add(Fraction other){
int fracDenominator = lcm(this.denominator, other.denominator);
int fracNumerator = ((fracDenominator/ this.denominator) * this.numerator) + ((fracDenominator/ other.denominator) * other.numerator);
Fraction result = new Fraction(fracNumerator, fracDenominator);
return result;
}
/*
*This method returns a new Fraction that is the difference between the other and this fraction.
*/
public Fraction subtract(Fraction other){
int fracDenominator = lcm(this.denominator, other.denominator);
int fracNumerator = ((fracDenominator/ this.denominator) * this.numerator) - ((fracDenominator/ other.denominator) * other.numerator);
Fraction result = new Fraction(fracNumerator, fracDenominator);
return result;
}
/*
*This method returns a new Fraction that is the product of the other and this fraction.
*/
public Fraction multiply(Fraction other){
int fracNumerator = this.numerator * other.numerator;
int fracDenominator = this.denominator * other.denominator;
Fraction result = new Fraction(fracNumerator, fracDenominator);
return result;
}
/*
*This method returns a new Fraction that is the division of the other and this fraction.
*/
public Fraction divide(Fraction other){
if(other.denominator == 0){
/*throw an IllegalArgumentException if the user asks to divide by zero*/
throw new IllegalArgumentException(Integer.toString(denominator));
}
int fracNumerator = this.numerator * other.denominator;
int fracDenominator = this.denominator * other.numerator;
Fraction result = new Fraction(fracNumerator, fracDenominator);
return result;
}
/*
*This method converts the current fraction to the lowest terms
*/
public void toLowestTerms(){
int numGCD = gcd(this.numerator, this.denominator);
this.numerator /= numGCD;
this.denominator /= numGCD;
if(this.denominator < 0) {
this.numerator *= -1;
this.denominator *= -1;
}
}
/*
*This method checks whether the other and this equations are equal or not.
*/
public boolean equals(Object other){
if(other instanceof Fraction){
Fraction otherFrac = (Fraction)other;
otherFrac.toLowestTerms();
Fraction thisFrac = new Fraction(this.numerator, this.denominator);
thisFrac.toLowestTerms();
if ((thisFrac.numerator == otherFrac.numerator) && (thisFrac.denominator == otherFrac.denominator)) {
return true;
} else {
return false;
}
} else {
throw new InputMismatchException();
}
}
}