Greetings,
I'm working on a class project for fractions and I'm stuck and cannot figure out where to go from here. We have a mandatory driver file we aren't supposed to change. See below for example output and the output I am actually getting.

Sample 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

Driver file

import java.util.Scanner;

public class NathanialProg7
{
   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
}                                                                    // end public class NathanialProg7

My Fraction class file

public class Fraction
{
    private int numerator;
    private int denominator;
    private double solution;                                                             // numerator divided by denominator
//*************************************
    public Fraction(int n)                                                               // Fraction method #1
    {
       this(n,1);                                                                        // This statement used to call other constructor
    }
//*************************************
    public Fraction(int n, int d)                                                        // Fraction method #2
    {
        numerator = n;
        denominator = d;
        solution = (double) numerator / denominator;
    }
//*************************************
    public Fraction()                                                                    // Fraction method #3 added so the third fraction object "x" works
    {}
//*************************************
    public int getNumerator()                                                            // accessor method for numerator
    {
        return numerator;
    }
//*************************************
    public int getDenominator()                                                          // accessor method for denominator
    {
        return denominator;
    }
//*************************************
    public Fraction add(Fraction frac)
    {
       Fraction sum;                                                                     // declaring a new Fraction type which will be returned from this method
       int n = numerator * frac.getDenominator() + denominator * frac.getNumerator();    // this int n will be the numerator for the Fraction sum, creates a new numerator by algebraicly adding fractions
       int d = denominator * frac.getDenominator();                                      // this int d will be the denominator for the Fraction sum, creates a new denominator by algebraicly adding fractions
       sum = new Fraction(n, d);                                                         // initializing the sum by invoking the Fraction(int n, int d) using the n and d integers
       return sum;                                                                       // allows for continious adding of fractions
    }
  //*************************************
    public Fraction multiply(Fraction frac)
    {
       Fraction product;                                                                 // declaring a new Fraction type which will be returned from this method
       int n = frac.getNumerator() * numerator;                                          // this int n will be the numerator for the Fraction product, creates a new numerator by multiplying numerators
       int d = frac.getDenominator() * denominator;                                      // this int d will be the denominator for the Fraction product, creates a new denominator by multiplyig denominators
       product = new Fraction(n, d);                                                     // initializing the sum by invoking the Fraction(int n, int d) using n and d integers
       return product;                                                                   // allows for continious multipying of fractions
    }
//*************************************
    public void print()
    {
        System.out.println(numerator + "/" + denominator);                               // print line for numerator over denominator
    }
 //*************************************
    public void printAsDouble()
    {
        System.out.println(solution);                                                    // print line for printing fraction solution as a double
    }

}                                                                                        // End public class Fraction

My output:
Enter numerator; then denominator.
2
3
2/3
Enter numerator; then denominator.
7
8
7/8
Sum:
0/0
0.0
Product:
1/1
1.0
Enter numerator; then denominator.
6
0
Infinity

The add and multiply member functions return their results, so you need to store that result to be able to use it later. They do not modify the original object. Also, rather than using the void constructor, try using the 2-argument constructor. So starting on line 18:

System.out.println("Sum:");
x = new Fraction(c.getNumerator(), c.getDenominator()); // Initialize the sum with the value in c.
x = x.add(d);  // You actually need to assign the returned value for it to be remembered.
x.print();
x.printAsDouble();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.