Fraction:

- Added place holders
	- Constructors
	- GCD, reduce and equal methods
This commit is contained in:
HeshamTB 2020-08-30 14:09:45 +03:00
parent c2cd906820
commit d2b0d3c453
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -1,2 +1,76 @@
public class Fraction { public class Fraction {
private int numerator;
private int denominator;
/**
* Contruct a fraction numerator and denominator
* <p>The fraction is reduced to it's minimum from if possible</p>
* @param numerator Numerator
* @param denominator Denominator
*/
public Fraction(int numerator, int denominator){
this.numerator = numerator;
this.denominator = denominator;
//reduce here
}
/**
* Default value of fraction is 1 (1/1)
*/
public Fraction(){
this.numerator = 1;
this.denominator = 1;
}
/**
* Reduces a fraction to it's minimum form
* @param oldFraction The fraction to be reduced.
* @return reduced fraction
*/
private Fraction reduce(Fraction oldFraction) {
int gcd = GCD(oldFraction.numerator, oldFraction.denominator);
return new Fraction(oldFraction.numerator/gcd, oldFraction.denominator/gcd);
}
/**
* Find the greatest common divisor of two numbers.
* @param a First value
* @param b Second value
* @return Greatest common divisor as Integer.
*/
private int GCD(int a, int b){
int gcd = 1;
for (int i = 1; i <= b && i <= a; i++){
if (b % i == 0 && a % i == 0) gcd = i; // i divides a and b
}
return gcd;
}
/**
* Check if the fraction provided is equal in value
* @param fraction The fraction to check
* @return True if the fractions are equal, otherwise false.
*/
public boolean equal (Fraction fraction){
Fraction reducedFraction = reduce(fraction);
if (this.numerator == reducedFraction.numerator &&
this.denominator == reducedFraction.denominator) return true;
return false;
}
public static Fraction add(Fraction fraction1, Fraction fraction2){
return null;
}
} }