Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Mar 28th, 2012  |  syntax: None  |  size: 6.05 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2. Edward Ho
  3. Nov 2, 2009
  4.  */
  5.  
  6. public class Fraction
  7. {
  8.     private int num, den; // numerator and denominator
  9.  
  10.     public Fraction () // constructor
  11.     {
  12.       num = 0;
  13.       den = 1; // default statement, denominator cannot be 0
  14.     }
  15.  
  16.   public Fraction (int n, int d)
  17.   {
  18.     num = n;
  19.  
  20.     if (d != 0)
  21.       den = d;
  22.      
  23.  
  24.  
  25.     else
  26.       throw new IllegalArgumentException("Fraction cannot have 0 as denominator");
  27.     if (num < 0 && den < 0)
  28.      {
  29.         num = -num;
  30.         den = -den;
  31.      }
  32.      int x = gcf(num,den);
  33.        num = num/x;
  34.        den = den/x;
  35.  
  36.   }
  37.  
  38.   public Fraction (int n)
  39.   {
  40.     num = n;
  41.     den = 1;
  42.   }
  43.  
  44.  
  45.   public Fraction (Fraction n)
  46.   {
  47.     num = n.num; // pulling num on n > putting into num
  48.     den = n.den;
  49.    
  50.      if (num < 0 && den < 0)
  51.      {
  52.         num = -num;
  53.         den = -den;
  54.      }
  55.   }
  56.  
  57.  
  58.  
  59.      private int gcf(int a, int b)
  60.      {
  61.  
  62.  
  63.  
  64.        int d, e;
  65.        e=a;
  66.        d=b;
  67.  
  68.       if (a<0)
  69.          e=-a;
  70.       if (b<0)
  71.          d=-b;
  72.       if (a == 0 || b == 0)
  73.         return 1;
  74.                
  75.  
  76.  
  77.        while (e!=d)
  78.        {if (e>d)
  79.          e=e-d;
  80.        else
  81.          d=d-e;
  82.        }
  83.        return d;
  84.      }
  85.  
  86.      public Fraction reduce()
  87.      {
  88.        int x = gcf(num,den);
  89.        num = num/x;
  90.        den = den/x;
  91.         if (num < 0 && den < 0)
  92.         {
  93.                 num = -num;
  94.                 den = -den;
  95.         }
  96.        return new Fraction(num, den);
  97.      }
  98.      
  99.      public Fraction mixed()
  100.      {
  101.        int x = gcf(num,den);
  102.        int whole = 0;
  103.        
  104.        num = num/x;
  105.        den = den/x;
  106.        
  107.        
  108.        if (num >= den)
  109.        {
  110.         num = num - den;
  111.         whole = whole + 1;
  112.        }
  113.        
  114.         if (num < 0 && den < 0)
  115.         {
  116.                 num = -num;
  117.                 den = -den;
  118.         }
  119.        return new Fraction(num, den);
  120.      }
  121.  
  122.  
  123.  
  124.      public void fractionPrint()
  125.      {
  126.        System.out.println(num +"/"+ den);
  127.      }
  128.      
  129.      public void mixedfractionPrint()
  130.      {
  131.        System.out.println(num +"/"+ den);
  132.        //System.out.println(whole +"("+ num +"/"+ den +")"+);
  133.      }
  134.  
  135.  
  136.      public Fraction fractionAdd (Fraction a)
  137.      {
  138.  
  139.                 int newNom;
  140.                 int newDem;
  141.                newNom = (num * a.den) + (a.num * den);
  142.                newDem = den * a.den;
  143.                 if (newNom <0 && newDem <0)
  144.                 {
  145.                                 newNom = -newNom;
  146.                                 newDem = -newDem;
  147.                 }
  148.     return new Fraction(newNom, newDem);
  149.  
  150.      }
  151.  
  152.      public Fraction fractionSubtract (Fraction a)
  153.      {
  154.  
  155.           int newNom;
  156.           int newDem;
  157.                newNom = (num * a.den) - (a.num * den);
  158.                newDem = den * a.den;
  159.                 if (newNom <0 && newDem <0)
  160.                 {
  161.                                 newNom = -newNom;
  162.                                 newDem = -newDem;
  163.                 }
  164.     return new Fraction(newNom, newDem);
  165.  
  166.      }
  167.  
  168.      public Fraction fractionMultiply (Fraction a)
  169.      {
  170.  
  171.  
  172.           int newNom;
  173.           int newDem;
  174.                newNom = num * a.num;
  175.                newDem = den * a.den;
  176.                 if (newNom <0 && newDem <0)
  177.                 {
  178.                                 newNom = -newNom;
  179.                                 newDem = -newDem;
  180.                 }
  181.     return new Fraction(newNom, newDem);
  182.  
  183.      }
  184.  
  185.  
  186.      public Fraction fractionDivide (Fraction a)
  187.      {
  188.  
  189.  
  190.           int newNom;
  191.           int newDem;
  192.           if (a.num!=0)
  193.           {
  194.                    newNom = num * a.den;
  195.                    newDem = den * a.num;
  196.                         if (newNom <0 && newDem <0)
  197.                         {
  198.                                 newNom = -newNom;
  199.                                 newDem = -newDem;
  200.                         }
  201.           }
  202.           else
  203.                    throw new IllegalArgumentException("Fraction cannot have 0 as denominator");
  204.         return new Fraction(newNom, newDem);
  205.  
  206.      }
  207.  
  208.  
  209.  
  210.  
  211.  
  212.      public static void main (String[] args)
  213.      {
  214.        Fraction x = new Fraction(-10,-8);
  215.        Fraction y = new Fraction(-3,4);
  216.        Fraction z = new Fraction(x.fractionAdd(y));
  217.        Fraction g = new Fraction(x.fractionSubtract(y));
  218.        Fraction t = new Fraction(x.fractionMultiply(y));
  219.        Fraction s = new Fraction(x.fractionDivide(y));
  220.        //x.fractionAdd(y);
  221.        System.out.println("Fraction x");
  222.        x.fractionPrint();
  223.        System.out.println("Fraction x reduced");
  224.        x = x.reduce();
  225.        x.fractionPrint();
  226.        System.out.println("Fraction y");
  227.        y.fractionPrint();
  228.        System.out.println("Fraction y reduced");
  229.        y = y.reduce();
  230.        y.fractionPrint();
  231.        System.out.println("Fraction x + y");
  232.        z.fractionPrint();
  233.        System.out.println("Fraction x + y reduced");
  234.        z = z.reduce();
  235.        z.fractionPrint();
  236.        System.out.println("Fraction x - y");
  237.        g.fractionPrint();
  238.        System.out.println("Fraction x - y reduced");
  239.        g = g.reduce();
  240.        g.fractionPrint();
  241.        System.out.println("Fraction x * y");
  242.        t.fractionPrint();
  243.        System.out.println("Fraction x * y reduced");
  244.        t = t.reduce();
  245.        t.fractionPrint();
  246.        System.out.println("Fraction x / y");
  247.        s.fractionPrint();
  248.        System.out.println("Fraction x / y reduced");
  249.        s = s.reduce();
  250.        s.fractionPrint();
  251.        
  252.        x = x.mixed();
  253.        x.mixedfractionPrint();
  254.        
  255.        //y.fractionPrint();
  256.      }
  257.  
  258.  
  259.  
  260.  
  261.  
  262. }
  263.  
  264.  
  265.  
  266.   /**public Fraction gcf(num, den)
  267.   {
  268.       while (num != den)
  269.       {
  270.         if (num > den)
  271.           num -= den;
  272.         else
  273.           den -= num;
  274.       }
  275.       return num;
  276.     }
  277.  
  278.     public Fraction reduce(num, den)
  279.     {
  280.       nume = num / num;
  281.       deni = deni / num;
  282.       System.out.println(nume "/" deni);
  283.     }
  284.  
  285. }
  286.  **/
  287.  
  288.  /**
  289.  
  290.  
  291. public class GCF {
  292.  
  293.     public static int gcf(int a, int b) {
  294.     {
  295.       while (a!=b)
  296.       {
  297.         if (a>b)
  298.           a-=b;
  299.         else
  300.           b-=a;
  301.       }
  302.       return a;
  303.     }
  304.  
  305.     }
  306.  
  307.   public static void main (String[] args)
  308.     {
  309.  
  310.     System.out.print(gcf(200,100));
  311.   }
  312. }
  313. **/