2020-08-30 10:58:07 +02:00
|
|
|
public class Fraction {
|
2020-08-30 13:09:45 +02:00
|
|
|
|
|
|
|
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){
|
|
|
|
|
2020-08-30 16:05:07 +02:00
|
|
|
//This might get in recursive loop
|
|
|
|
if (isReducible(numerator, denominator)){
|
2020-08-30 17:14:49 +02:00
|
|
|
System.out.printf("reducing %d/%d\n", numerator, denominator);
|
|
|
|
Fraction f = reduce(numerator, denominator);
|
2020-08-30 16:05:07 +02:00
|
|
|
this.numerator = f.numerator;
|
|
|
|
this.denominator = f.denominator;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
this.numerator = numerator;
|
|
|
|
this.denominator = denominator;
|
|
|
|
}
|
2020-08-30 13:09:45 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default value of fraction is 1 (1/1)
|
|
|
|
*/
|
|
|
|
public Fraction(){
|
|
|
|
|
|
|
|
this.numerator = 1;
|
|
|
|
this.denominator = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reduces a fraction to it's minimum form
|
2020-08-30 17:14:49 +02:00
|
|
|
* @param numerator
|
|
|
|
* @param denominator
|
|
|
|
* @return
|
2020-08-30 13:09:45 +02:00
|
|
|
*/
|
2020-08-30 17:14:49 +02:00
|
|
|
private Fraction reduce(int numerator, int denominator) {
|
2020-08-30 13:09:45 +02:00
|
|
|
|
2020-08-30 17:14:49 +02:00
|
|
|
int gcd = GCD(numerator, denominator);
|
|
|
|
return new Fraction(numerator/gcd, denominator/gcd);
|
2020-08-30 13:09:45 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the greatest common divisor of two numbers.
|
|
|
|
* @param a First value
|
|
|
|
* @param b Second value
|
|
|
|
* @return Greatest common divisor as Integer.
|
|
|
|
*/
|
2020-08-30 16:05:07 +02:00
|
|
|
private static int GCD(int a, int b){
|
2020-08-30 13:09:45 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2020-08-30 14:25:08 +02:00
|
|
|
public boolean equal(Fraction fraction){
|
2020-08-30 13:09:45 +02:00
|
|
|
|
2020-08-30 17:14:49 +02:00
|
|
|
Fraction reducedFraction = reduce(fraction.numerator, fraction.denominator);
|
2020-08-30 13:09:45 +02:00
|
|
|
if (this.numerator == reducedFraction.numerator &&
|
|
|
|
this.denominator == reducedFraction.denominator) return true;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-30 14:30:56 +02:00
|
|
|
/**
|
|
|
|
* add two fractions
|
|
|
|
* @param fraction1
|
|
|
|
* @param fraction2
|
|
|
|
* @return added fractions reduced to min form
|
|
|
|
*/
|
2020-08-30 13:09:45 +02:00
|
|
|
public static Fraction add(Fraction fraction1, Fraction fraction2){
|
2020-08-30 14:25:08 +02:00
|
|
|
|
2020-08-30 16:05:07 +02:00
|
|
|
//new denominator
|
|
|
|
int mDenominatorResult = fraction1.denominator * fraction2.denominator;
|
|
|
|
int mNumerator1 = fraction1.numerator * fraction2.denominator;
|
|
|
|
int mNumerator2 = fraction2.numerator * fraction1.denominator;
|
|
|
|
//new numerator
|
|
|
|
int mNumeratorResult = mNumerator1 + mNumerator2;
|
2020-08-30 14:25:08 +02:00
|
|
|
|
2020-08-30 16:05:07 +02:00
|
|
|
//
|
|
|
|
return new Fraction(mNumeratorResult, mDenominatorResult);
|
2020-08-30 14:25:08 +02:00
|
|
|
}
|
|
|
|
|
2020-08-30 16:24:40 +02:00
|
|
|
/**
|
|
|
|
* subtract two fractions
|
|
|
|
* @param fraction1
|
|
|
|
* @param fraction2
|
|
|
|
* @return subtracted fractions reduced to min form
|
|
|
|
*/
|
|
|
|
public static Fraction subtract(Fraction fraction1, Fraction fraction2){
|
|
|
|
|
|
|
|
//new denominator
|
|
|
|
int mDenominatorResult = fraction1.denominator * fraction2.denominator;
|
|
|
|
int mNumerator1 = fraction1.numerator * fraction2.denominator;
|
|
|
|
int mNumerator2 = fraction2.numerator * fraction1.denominator;
|
|
|
|
//new num
|
|
|
|
int mNumeratorResult = mNumerator1 - mNumerator2;
|
|
|
|
|
|
|
|
return new Fraction(mNumeratorResult, mDenominatorResult);
|
|
|
|
}
|
|
|
|
|
2020-08-30 14:25:08 +02:00
|
|
|
/**
|
|
|
|
* Multiply two fractions and reduce
|
|
|
|
* @param fraction1
|
|
|
|
* @param fraction2
|
2020-08-30 16:24:40 +02:00
|
|
|
* @return fractions
|
2020-08-30 14:25:08 +02:00
|
|
|
*/
|
|
|
|
public static Fraction multiply(Fraction fraction1, Fraction fraction2) {
|
|
|
|
|
|
|
|
//The constructor already reduces.
|
|
|
|
return new Fraction(fraction1.numerator * fraction2.numerator,
|
|
|
|
fraction1.denominator * fraction2.denominator);
|
|
|
|
|
2020-08-30 13:09:45 +02:00
|
|
|
}
|
|
|
|
|
2020-08-30 16:24:40 +02:00
|
|
|
/**
|
|
|
|
* Divide two fractions and reduce
|
|
|
|
* @param fraction1
|
|
|
|
* @param fraction2
|
|
|
|
* @return fractions
|
|
|
|
*/
|
|
|
|
public static Fraction divide(Fraction fraction1, Fraction fraction2){
|
|
|
|
|
|
|
|
return new Fraction(fraction1.numerator * fraction2.denominator,
|
|
|
|
fraction1.denominator * fraction2.numerator);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-30 14:30:56 +02:00
|
|
|
/**
|
|
|
|
* string of the fraction in num/den form
|
2020-08-30 16:05:07 +02:00
|
|
|
* overrides global toString()
|
2020-08-30 14:30:56 +02:00
|
|
|
* @return fraction string
|
|
|
|
*/
|
2020-08-30 13:15:33 +02:00
|
|
|
public String toString(){
|
2020-08-30 14:25:08 +02:00
|
|
|
|
2020-08-30 13:15:33 +02:00
|
|
|
return String.format("%d/%d", this.numerator, this.denominator);
|
2020-08-30 14:25:08 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-30 14:30:56 +02:00
|
|
|
/**
|
|
|
|
* returns a string with the decimal value of the fraction
|
|
|
|
* @param digits precision
|
|
|
|
* @return decimal string
|
|
|
|
*/
|
2020-08-30 14:25:08 +02:00
|
|
|
public String decimal(int digits){
|
|
|
|
|
2020-08-30 16:33:26 +02:00
|
|
|
return String.format("%."+digits+"f", (double)this.numerator / this.denominator);
|
2020-08-30 14:25:08 +02:00
|
|
|
|
2020-08-30 13:15:33 +02:00
|
|
|
}
|
|
|
|
|
2020-08-30 16:05:07 +02:00
|
|
|
/**
|
|
|
|
* Check if fraction is in simplest terms.
|
|
|
|
* @param fraction Fraction object
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private boolean isReducible(Fraction fraction){
|
|
|
|
|
|
|
|
int gcd = GCD(fraction.numerator, fraction.denominator);
|
2020-08-30 17:14:49 +02:00
|
|
|
System.out.println("GCD: "+gcd);
|
2020-08-30 16:05:07 +02:00
|
|
|
if (gcd == 1) return false;
|
|
|
|
else return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if fraction is in simplest terms.
|
|
|
|
* @param numerator
|
|
|
|
* @param denominator
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private boolean isReducible(int numerator, int denominator){
|
|
|
|
|
|
|
|
int gcd = GCD(numerator, denominator);
|
|
|
|
if (gcd == 1) return false;
|
|
|
|
else return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-30 10:58:07 +02:00
|
|
|
}
|