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