0

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!

Pysicist
  • 1
  • 2
  • Looks like a duplicate of http://stackoverflow.com/questions/3144610/java-integer-division-how-do-you-produce-a-double – Lucia Pasarin Nov 26 '14 at 19:46
  • in `numer` a class? Your code wont compile btw, and why not just do `System.out.println(numer);` or `System.out.println(deamon);` ? – A4L Nov 26 '14 at 19:47
  • 1
    As a side note: you are probably not supposed to have parameters in your print methods. You're just supposed to print the fraction represented by the current object. The driver (main) is probably supposed to create two separate Fraction objects and print them each using its own print methods. – RealSkeptic Nov 26 '14 at 19:54

2 Answers2

2

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.

Lucia Pasarin
  • 2,268
  • 1
  • 21
  • 37
  • 1
    Did you intend for this method to be `static` - because it's not using the `numer` and `denom` that are stored in the object? Or did you intend to use those values? Having parameters with the same names as the class's instance variables is VERY confusing, especially in a non-static method. – Dawood ibn Kareem Nov 26 '14 at 20:00
  • Thanks for the help I was definitely over thinking this. – Pysicist Nov 26 '14 at 20:02
  • @DavidWallace I actually just copied the method from the description of the question. Thanks for noticing! – Lucia Pasarin Nov 26 '14 at 20:03
  • Right. I have my Eclipse configured to give a compile warning when I duplicate names like this. I recommend doing the same, because it's nearly always a mistake. – Dawood ibn Kareem Nov 26 '14 at 20:04
  • 1
    Great, your answer looks much better after the edit. +1, Lucia. – Dawood ibn Kareem Nov 26 '14 at 20:06
0

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.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • Is there a way to take the Fraction from Fraction add and put its output through the printAsDouble()? – Pysicist Nov 26 '14 at 20:14
  • Yeah, if `frac1` and `frac2` are two variables of type `Fraction`, you can write something like `Fraction sum = frac1.add(frac2);` then `sum.printAsDouble();`. By the way, this kind of thing will be much easier if you add a constructor to your `Fraction` class, that takes the numerator and denominator as parameters. – Dawood ibn Kareem Nov 26 '14 at 21:54