-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFraction.java
118 lines (96 loc) · 2.81 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
/**
* Class representing a fraction
*
* @version 08/28/2018
* @author Luis Francisco Flores Romero - A01328937
* @author Salvador Orozco Villalever - A07104218
*/
public class Fraction{
// The numerator of the fraction
private int numerator;
// The denominator of the fraction
private int denominator;
/**
* Constructor that takes as a parameter two ints
*
* @param numerator the numerator of the fraction
* @param denominator the denominator of the fraction
*/
public Fraction(int numerator, int denominator){
assert denominator != 0: "The denominator of the fraction is zero";
// To reduce the fraction to its lowest terms, compute the
// greatest common divisor between the numerator and the
// denominator. Then divide each term by their gcd.
int gcd = Mathematics.computeGCD(numerator, denominator);
setNumerator(numerator/gcd);
setDenominator(denominator/gcd);
}
/**
* Copy-constructor
*
* @param f the fraction to copy
*/
public Fraction(Fraction f){
setNumerator(f.getNumerator());
setDenominator(f.getDenominator());
}
/**
* Setter for the numerator
*
* @param numerator the numerator of the fraction
*/
public void setNumerator(int numerator){
this.numerator = numerator;
}
/**
* Getter for the numerator
*
* @return numerator the numerator of the fraction
*/
public int getNumerator(){
return numerator;
}
/**
* Setter for the denominator
*
* @param denominator the denominator of the fraction
*/
public void setDenominator(int denominator){
this.denominator = denominator;
}
/**
* Getter for the denominator
*
* @return denominator the denominator of the fraction
*/
public int getDenominator(){
return denominator;
}
/**
* Method to print the fraction as a string
*
* @return str a string representing the fraction
*/
public String toString(){
// If the fraction is greater or equal to 1
// just return the numerator
if(this.getDenominator() == 1)
return Integer.toString(this.getNumerator());
String str = this.getNumerator() + "/" + denominator;
return str;
}
/**
* Method to add two fractions
*
* @param f2 the fraction to add to this fraction
* @return a new fraction corresponding to the sum
* of the two fractions
*/
public Fraction add(Fraction f2){
int num1 = this.getNumerator();
int den1 = this.getDenominator();
int num2 = f2.getNumerator();
int den2 = f2.getDenominator();
return new Fraction(num1*den2 + num2*den1, den1*den2);
}
}