Beginner Java question - need help writing a Fraction class #2

  • Follow


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!
0
Reply ethomsen86 (3) 12/5/2010 11:06:09 AM

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
esosman@ieee-dot-org.invalid
0
Reply Eric 12/5/2010 2:15:33 PM


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
0
Reply Arved 12/5/2010 2:24:33 PM

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
0
Reply Arved 12/5/2010 2:37:06 PM

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
0
Reply ISO 12/5/2010 10:55:32 PM

4 Replies
453 Views

(page loaded in 0.301 seconds)

Similiar Articles:













7/23/2012 7:05:38 AM


Reply: