- /**
- Edward Ho
- Nov 2, 2009
- */
- public class Fraction
- {
- private int num, den; // numerator and denominator
- public Fraction () // constructor
- {
- num = 0;
- den = 1; // default statement, denominator cannot be 0
- }
- public Fraction (int n, int d)
- {
- num = n;
- if (d != 0)
- den = d;
- else
- throw new IllegalArgumentException("Fraction cannot have 0 as denominator");
- if (num < 0 && den < 0)
- {
- num = -num;
- den = -den;
- }
- int x = gcf(num,den);
- num = num/x;
- den = den/x;
- }
- public Fraction (int n)
- {
- num = n;
- den = 1;
- }
- public Fraction (Fraction n)
- {
- num = n.num; // pulling num on n > putting into num
- den = n.den;
- if (num < 0 && den < 0)
- {
- num = -num;
- den = -den;
- }
- }
- private int gcf(int a, int b)
- {
- int d, e;
- e=a;
- d=b;
- if (a<0)
- e=-a;
- if (b<0)
- d=-b;
- if (a == 0 || b == 0)
- return 1;
- while (e!=d)
- {if (e>d)
- e=e-d;
- else
- d=d-e;
- }
- return d;
- }
- public Fraction reduce()
- {
- int x = gcf(num,den);
- num = num/x;
- den = den/x;
- if (num < 0 && den < 0)
- {
- num = -num;
- den = -den;
- }
- return new Fraction(num, den);
- }
- public Fraction mixed()
- {
- int x = gcf(num,den);
- int whole = 0;
- num = num/x;
- den = den/x;
- if (num >= den)
- {
- num = num - den;
- whole = whole + 1;
- }
- if (num < 0 && den < 0)
- {
- num = -num;
- den = -den;
- }
- return new Fraction(num, den);
- }
- public void fractionPrint()
- {
- System.out.println(num +"/"+ den);
- }
- public void mixedfractionPrint()
- {
- System.out.println(num +"/"+ den);
- //System.out.println(whole +"("+ num +"/"+ den +")"+);
- }
- public Fraction fractionAdd (Fraction a)
- {
- int newNom;
- int newDem;
- newNom = (num * a.den) + (a.num * den);
- newDem = den * a.den;
- if (newNom <0 && newDem <0)
- {
- newNom = -newNom;
- newDem = -newDem;
- }
- return new Fraction(newNom, newDem);
- }
- public Fraction fractionSubtract (Fraction a)
- {
- int newNom;
- int newDem;
- newNom = (num * a.den) - (a.num * den);
- newDem = den * a.den;
- if (newNom <0 && newDem <0)
- {
- newNom = -newNom;
- newDem = -newDem;
- }
- return new Fraction(newNom, newDem);
- }
- public Fraction fractionMultiply (Fraction a)
- {
- int newNom;
- int newDem;
- newNom = num * a.num;
- newDem = den * a.den;
- if (newNom <0 && newDem <0)
- {
- newNom = -newNom;
- newDem = -newDem;
- }
- return new Fraction(newNom, newDem);
- }
- public Fraction fractionDivide (Fraction a)
- {
- int newNom;
- int newDem;
- if (a.num!=0)
- {
- newNom = num * a.den;
- newDem = den * a.num;
- if (newNom <0 && newDem <0)
- {
- newNom = -newNom;
- newDem = -newDem;
- }
- }
- else
- throw new IllegalArgumentException("Fraction cannot have 0 as denominator");
- return new Fraction(newNom, newDem);
- }
- public static void main (String[] args)
- {
- Fraction x = new Fraction(-10,-8);
- Fraction y = new Fraction(-3,4);
- Fraction z = new Fraction(x.fractionAdd(y));
- Fraction g = new Fraction(x.fractionSubtract(y));
- Fraction t = new Fraction(x.fractionMultiply(y));
- Fraction s = new Fraction(x.fractionDivide(y));
- //x.fractionAdd(y);
- System.out.println("Fraction x");
- x.fractionPrint();
- System.out.println("Fraction x reduced");
- x = x.reduce();
- x.fractionPrint();
- System.out.println("Fraction y");
- y.fractionPrint();
- System.out.println("Fraction y reduced");
- y = y.reduce();
- y.fractionPrint();
- System.out.println("Fraction x + y");
- z.fractionPrint();
- System.out.println("Fraction x + y reduced");
- z = z.reduce();
- z.fractionPrint();
- System.out.println("Fraction x - y");
- g.fractionPrint();
- System.out.println("Fraction x - y reduced");
- g = g.reduce();
- g.fractionPrint();
- System.out.println("Fraction x * y");
- t.fractionPrint();
- System.out.println("Fraction x * y reduced");
- t = t.reduce();
- t.fractionPrint();
- System.out.println("Fraction x / y");
- s.fractionPrint();
- System.out.println("Fraction x / y reduced");
- s = s.reduce();
- s.fractionPrint();
- x = x.mixed();
- x.mixedfractionPrint();
- //y.fractionPrint();
- }
- }
- /**public Fraction gcf(num, den)
- {
- while (num != den)
- {
- if (num > den)
- num -= den;
- else
- den -= num;
- }
- return num;
- }
- public Fraction reduce(num, den)
- {
- nume = num / num;
- deni = deni / num;
- System.out.println(nume "/" deni);
- }
- }
- **/
- /**
- public class GCF {
- public static int gcf(int a, int b) {
- {
- while (a!=b)
- {
- if (a>b)
- a-=b;
- else
- b-=a;
- }
- return a;
- }
- }
- public static void main (String[] args)
- {
- System.out.print(gcf(200,100));
- }
- }
- **/