Beginner Java question - need help writing a Fraction class

E

Erik

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
 
E

Eric Sosman

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.
 
A

Arved Sandstrom

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
 
A

Arved Sandstrom

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
 
A

Arne Vajhøj

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top