Results 1 to 2 of 2

Thread: fraction class

  1. #1
    ericgomez7 is offline Member
    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Default fraction class

    /**
    * What is wrong with my multiplication and division methods?
    */

    public class Fraction
    {

    // instance variables (fields) of our Fraction object
    public int num;
    public int denom;


    // **** Constructors for Fraction Objects ****

    public Fraction(int n, int d)
    {
    num = n;
    denom = d;
    cleanup();
    }


    // **** Public Methods for Fraction Objects ****
    public String toString()
    {
    return num + "/" + denom;
    // add code to print out mixed fraction
    }

    public Fraction add(Fraction other)
    {
    int commonDenom = denom * other.denom;
    int numerator = num*other.denom + other.num * denom;
    return new Fraction(numerator,commonDenom);

    } // end of add method

    // **** Private Methods for Fraction Objects ****
    private void cleanup()
    {
    if (denom < 0)
    {
    num *= -1;
    denom *= -1;
    }

    // reduce the fraction by finding gcf
    int factor = gcf(Math.abs(num),Math.abs(denom));
    num = num/factor;
    denom = denom/factor;
    }

    // Returns the greatest common factor
    // of two positive integers
    private int gcf(int n, int d)
    {

    if (n % d == 0)
    return d;
    else if (d % n == 0)
    return n;
    else
    return gcf(n % d, d % n);
    } // end of gcf method


    public Fraction subtract(Fraction other) {
    int commonDenom2 = denom * other.denom;
    int numerator2 = num*other.denom - other.num * denom;
    return new Fraction(numerator2,commonDenom2);

    }


    public Fraction multiply(Fraction other) {
    int commonDenom3 = denom * other.denom;
    int numerator3 = num*other.denom * other.num * denom;
    return new Fraction(numerator3,commonDenom3);

    }


    public Fraction divide(Fraction other) {
    int commonDenom4 = denom * other.denom;
    int numerator4 = num*other.denom / other.num * denom;
    return new Fraction(numerator4,commonDenom4);
    }

    } // end of Fraction class

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: fraction class

    (a/b)*(c/d) == (a*c)/(b*d) and (a/b)/(c/d) == (a*d)/(b*c)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Fraction Calculator need help
    By askinne2 in forum New To Java
    Replies: 15
    Last Post: 07-10-2012, 02:33 AM
  2. creating fraction class for school get weird problem
    By misterbreadcrum in forum New To Java
    Replies: 7
    Last Post: 09-21-2011, 05:45 AM
  3. Fraction Problem
    By Jakora33 in forum New To Java
    Replies: 6
    Last Post: 09-16-2009, 03:51 PM
  4. Need help on Fraction
    By mitch25 in forum New To Java
    Replies: 2
    Last Post: 07-24-2009, 09:46 PM
  5. Big Fraction 1.00
    By JavaBean in forum Java Software
    Replies: 1
    Last Post: 03-26-2008, 04:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •