Beginner Java question - need help writing a Fraction class

Discussion in 'Java' started by Erik, Dec 5, 2010.

  1. Erik

    Erik Guest

    I am trying to write a method class in java that handles fractions. I
    have a driver that allows the user to enter the numerator and
    denominator each separately. Then my program asks for another
    fraction and attempts to add the two fractions together. In the
    driver
    program I commented out the code after adding the fractions because I
    figured once I got the fractions to add up I could figure out the
    rest. Currently when I run the program the only value I get for the
    sum of the two fractions is 0/1.

    The basic requirements are to write a driver and fraction class that
    performs addition, multiplication, prints the fraction, and prints as
    a double.

    *==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==
    *==*==*==*==*
    *==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==
    *==*==*==*==*

    Here is the code for my driver program:

    public static void main(String[] args)
    {
    Scanner stdIn = new Scanner(System.in);
    Fraction c, d, x; // Fraction objects
    System.out.println("Enter numerator; then denominator.");
    c = new Fraction(stdIn.nextInt(), stdIn.nextInt());
    c.print();
    System.out.println("Enter numerator; then denominator.");
    d = new Fraction(stdIn.nextInt(), stdIn.nextInt());
    d.print();
    x = new Fraction(); // create a fraction for number 0
    System.out.println("Sum:");
    x.add(c).add(d);
    x.print();
    /*
    x.printAsDouble();
    x = new Fraction(1, 1); // create a fraction for number 1
    System.out.println("Product:");
    x.multiply(c).multiply(d);
    x.print();
    x.printAsDouble();
    System.out.println("Enter numerator; then denominator.");
    x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
    x.printAsDouble();
    */
    } // end main

    *==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==
    *==*==*==*==*
    *==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==
    *==*==*==*==*

    Here is my class program (this is incomplete but my problem seems to
    lie in the "add" method):

    public class Fraction
    {
    private int numerator;
    private int denominator;
    //
    *************************************************************************
    // These are constructors that assign the fraction.
    public Fraction() // delete this one
    {
    numerator = 0;
    denominator = 1;
    }
    public Fraction(int whole) // delete this one
    {
    this.numerator = whole;
    this.denominator = 1;
    }
    public Fraction(int numerator, int denominator)
    {
    this.numerator = numerator;
    this.denominator = denominator;
    } // end constructor
    //
    *************************************************************************
    // This method finds the sum of 2 fractions.
    public Fraction add(Fraction otherFraction)
    {
    Fraction sum = new Fraction(1,1);
    int newdenom = this.denominator *
    otherFraction.denominator;
    sum.denominator = newdenom;
    sum.numerator = this.numerator *
    otherFraction.denominator +
    otherFraction.numerator * this.denominator;
    return new Fraction(sum.numerator,
    sum.denominator); // originally
    return sum;
    }
    //
    *************************************************************************
    // This method prints the fraction.
    public void print()
    {
    System.out.println(this.numerator + "/" +
    this.denominator);
    }
    } // end class Fraction

    *==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==
    *==*==*==*==*
    *==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==
    *==*==*==*==*

    Example output:
    Enter numerator; then denominator.
    5
    8
    5/8
    Enter numerator; then denominator.
    4
    10
    4/10
    Sum:
    82/80
    1.025
    Product:
    20/80
    0.25
    Enter numerator; then denominator.
    6
    0
    infinity

    -----------------
    Thanks!
     
    Erik, Dec 5, 2010
    #1
    1. Advertising

  2. Erik

    Eric Sosman Guest

    On 12/5/2010 6:06 AM, Erik wrote:
    > I am trying to write a method class in java that handles fractions.[...]


    Your question has been answered in comp.lang.java.help (follow-ups
    set). Please don't multi-post; if you simply must broadcast to multiple
    groups, cross-post instead.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 5, 2010
    #2
    1. Advertising

  3. On 10-12-05 07:06 AM, Erik wrote:
    > I am trying to write a method class in java that handles fractions. I
    > have a driver that allows the user to enter the numerator and
    > denominator each separately. Then my program asks for another
    > fraction and attempts to add the two fractions together. In the
    > driver
    > program I commented out the code after adding the fractions because I
    > figured once I got the fractions to add up I could figure out the
    > rest. Currently when I run the program the only value I get for the
    > sum of the two fractions is 0/1.
    >
    > The basic requirements are to write a driver and fraction class that
    > performs addition, multiplication, prints the fraction, and prints as
    > a double.


    As a side note I personally would not be doing I/O calls (such as using
    Scanner) inside a constructor. It works for you now, but in production
    users could make you weep in frustration. Better to get the inputs,
    validate if necessary, _then_ construct.

    For your "add" method, you're not modifying "this". Yet at the end you
    print out the value of it, rather than that of the final return.

    AHS
     
    Arved Sandstrom, Dec 5, 2010
    #3
  4. On 10-12-05 10:24 AM, Arved Sandstrom wrote:
    > On 10-12-05 07:06 AM, Erik wrote:
    >> I am trying to write a method class in java that handles fractions. I
    >> have a driver that allows the user to enter the numerator and
    >> denominator each separately. Then my program asks for another
    >> fraction and attempts to add the two fractions together. In the
    >> driver
    >> program I commented out the code after adding the fractions because I
    >> figured once I got the fractions to add up I could figure out the
    >> rest. Currently when I run the program the only value I get for the
    >> sum of the two fractions is 0/1.
    >>
    >> The basic requirements are to write a driver and fraction class that
    >> performs addition, multiplication, prints the fraction, and prints as
    >> a double.

    >
    > As a side note I personally would not be doing I/O calls (such as using
    > Scanner) inside a constructor. It works for you now, but in production
    > users could make you weep in frustration. Better to get the inputs,
    > validate if necessary, _then_ construct.
    >
    > For your "add" method, you're not modifying "this". Yet at the end you
    > print out the value of it, rather than that of the final return.
    >
    > AHS


    Correction: I meant that at the end you're printing out the value of the
    original object.

    I saw that after I posted answers were going to cljh, which I am not
    normally subscribed to. I did subscribe, and saw other replies there.
    They obviously have more energy than I do this morning. :) I second the
    suggestions, especially the one about reducing the fraction before you
    return it.

    AHS
     
    Arved Sandstrom, Dec 5, 2010
    #4
  5. Erik

    Arne Vajhøj Guest

    On 05-12-2010 06:06, Erik wrote:
    > I am trying to write a method class in java that handles fractions. I
    > have a driver that allows the user to enter the numerator and
    > denominator each separately. Then my program asks for another
    > fraction and attempts to add the two fractions together. In the
    > driver
    > program I commented out the code after adding the fractions because I
    > figured once I got the fractions to add up I could figure out the
    > rest. Currently when I run the program the only value I get for the
    > sum of the two fractions is 0/1.
    >
    > The basic requirements are to write a driver and fraction class that
    > performs addition, multiplication, prints the fraction, and prints as
    > a double.


    I assume the problem in your code has been resolved in cljh.

    If you want a complete Fraction class then look at
    Apache Common Math.

    Arne
     
    Arne Vajhøj, Dec 5, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jam
    Replies:
    2
    Views:
    776
  2. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    659
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  3. Peter

    String Fraction to Number

    Peter, May 6, 2004, in forum: Java
    Replies:
    6
    Views:
    1,163
    Mason Bryant
    May 6, 2004
  4. Replies:
    4
    Views:
    417
    Guillaume
    Nov 20, 2005
  5. Sam Holden

    Re: fraction reducing funtion

    Sam Holden, Aug 3, 2003, in forum: C++
    Replies:
    2
    Views:
    382
    Sam Holden
    Aug 4, 2003
Loading...

Share This Page