How to Print Double in Fraction Class

2k views Asked by At

This program is supposed to take in two numerators and two denominators and output them in fraction form while also adding and multiplying said fractions. It is also required that I have a driver and fraction class that performs addition, multiplication, prints the fraction, and prints as a double. This pertains to the Fraction class. I cannot figure out how to get a fraction to print as a double in my fraction class. I would like to know how to get the printAsDouble() to work with what I have. Also is there a way to use the output from Fraction add or Fraction multi and send it through printAsDouble()?

Here is my code:

public class Fraction

{

    int numer, denom;
    double end;

    public int getNumerator()
    {
        return this.numer;
    };

    public void setNumerator(int inNumer)
    {
        this.numer = inNumer;
    }

    public int getDenominator()
    {
        return this.denom;
    };

    public void setDenominator(int inDenom)
    {
        this.denom = inDenom;
    }

    public Fraction add(Fraction input)
    {
        Fraction output = new Fraction();
        output.setNumerator((this.getNumerator()*input.getDenominator())+(input.getNumerator()*this.getDenominator()));
        output.setDenominator(this.getDenominator()*input.getDenominator());
        return output;
    }

    public Fraction multi(Fraction input)
    {
        Fraction output = new Fraction();
        output.setNumerator(input.getNumerator()*this.getNumerator());
        output.setDenominator(input.getDenominator()*this.getDenominator());
        return output;
    };

    public int print(Fraction input)
    {
       System.out.println(this.getNumerator + "/" + this.getDenominator);
       System.out.println(input.getNumerator + "/" + input.getDenominator);
    };

    public double printAsDouble(numer denom)
    {
        double numer, denom;

        System.out.println();
        System.out.println();
    };
};

Any help or a point in the right direction would be appreciated!

2

There are 2 answers

5
Lucia Pasarin On

You probably just need this cast:

public void printAsDouble()
{
    double result = (double)numer / denom; // class attributes
    System.out.println(result);
}

or

public double getAsDouble()
{
    double result = (double)numer / denom; // class attributes
    return result;
}

otherwise double result = numer / denom; would be cast into an int.

double printAsDouble() looks actually semantically wrong to me.

2
Dawood ibn Kareem On

Your numerator and denominator are already stored in your object, so there's no point in passing them in to your print method. It would probably be worthwhile having a get method as well as a print method, in case you have other uses for this double. So I would do it something like this.

public double getValue() {
   return (double)numer / denom;
}

public void printAsDouble() {
    System.out.println(getValue());
}

The cast to double is necessary to prevent the rounding that happens automatically with integer division.