Fraction:

- Fixed StackOverFlow ex
	- Fraction test
	- output
This commit is contained in:
HeshamTB 2020-08-30 18:14:49 +03:00
parent 526717242a
commit 0ac8436afa
Signed by: Hesham
GPG Key ID: 74876157D199B09E
3 changed files with 69 additions and 11 deletions

View File

@ -0,0 +1,26 @@
/usr/lib/jvm/java-11-openjdk/bin/java -javaagent:/usr/share/idea/lib/idea_rt.jar=43413:/usr/share/idea/bin -Dfile.encoding=UTF-8 -classpath /home/hesham/Dev/EE364-lab/lab-0/out/production/lab-0 FractionTest
reducing 2/4
1/2
1/2
0.50
0.50
1/2 1/2 are equal? true
multiple: 1/4
reducing 4/4
add: 1/1
subtract: 0/4
reducing 2/2
divide: 1/1
------------------------------------
6/7
1/5
0.86
0.20
6/7 1/5 are equal? false
multiple: 6/35
add: 37/35
subtract: 23/35
divide: 30/7
Process finished with exit code 0

View File

@ -13,7 +13,8 @@ public class Fraction {
//This might get in recursive loop //This might get in recursive loop
if (isReducible(numerator, denominator)){ if (isReducible(numerator, denominator)){
Fraction f = reduce(new Fraction(numerator, denominator)); System.out.printf("reducing %d/%d\n", numerator, denominator);
Fraction f = reduce(numerator, denominator);
this.numerator = f.numerator; this.numerator = f.numerator;
this.denominator = f.denominator; this.denominator = f.denominator;
} }
@ -36,17 +37,14 @@ public class Fraction {
/** /**
* Reduces a fraction to it's minimum form * Reduces a fraction to it's minimum form
* @param oldFraction The fraction to be reduced. * @param numerator
* @return reduced fraction * @param denominator
* @return
*/ */
private Fraction reduce(Fraction oldFraction) { private Fraction reduce(int numerator, int denominator) {
if (isReducible(oldFraction)){ int gcd = GCD(numerator, denominator);
int gcd = GCD(oldFraction.numerator, oldFraction.denominator); return new Fraction(numerator/gcd, denominator/gcd);
return new Fraction(oldFraction.numerator/gcd,
oldFraction.denominator/gcd);
}
else return oldFraction;
} }
@ -72,7 +70,7 @@ public class Fraction {
*/ */
public boolean equal(Fraction fraction){ public boolean equal(Fraction fraction){
Fraction reducedFraction = reduce(fraction); Fraction reducedFraction = reduce(fraction.numerator, fraction.denominator);
if (this.numerator == reducedFraction.numerator && if (this.numerator == reducedFraction.numerator &&
this.denominator == reducedFraction.denominator) return true; this.denominator == reducedFraction.denominator) return true;
return false; return false;
@ -173,6 +171,7 @@ public class Fraction {
private boolean isReducible(Fraction fraction){ private boolean isReducible(Fraction fraction){
int gcd = GCD(fraction.numerator, fraction.denominator); int gcd = GCD(fraction.numerator, fraction.denominator);
System.out.println("GCD: "+gcd);
if (gcd == 1) return false; if (gcd == 1) return false;
else return true; else return true;

View File

@ -0,0 +1,33 @@
import java.io.PrintStream;
public class FractionTest {
public static void main(String args[]){
PrintStream out = System.out;
Fraction f1 = new Fraction(1, 2);
Fraction f2 = new Fraction(2, 4);
out.println(f1.toString());
out.println(f2.toString());
out.println(f1.decimal(2));
out.println(f2.decimal(2));
out.println(f1.toString()+" "+f2.toString()+" are equal? "+ f1.equal(f2));
out.println("multiple: "+ Fraction.multiply(f1, f2));
out.println("add: "+ Fraction.add(f1, f2));
out.println("subtract: "+ Fraction.subtract(f1, f2));
out.println("divide: "+ Fraction.divide(f1, f2));
out.println("------------------------------------");
Fraction f3 = new Fraction(6,7);
Fraction f4 = new Fraction(1,5);
out.println(f3.toString());
out.println(f4.toString());
out.println(f3.decimal(2));
out.println(f4.decimal(2));
out.println(f3.toString()+" "+f4.toString()+" are equal? "+ f3.equal(f4));
out.println("multiple: "+ Fraction.multiply(f3, f4));
out.println("add: "+ Fraction.add(f3, f4));
out.println("subtract: "+ Fraction.subtract(f3, f4));
out.println("divide: "+ Fraction.divide(f3, f4));
}
}