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!
You probably just need this cast:
otherwise
double result = numer / denom;
would be cast into anint
.double printAsDouble()
looks actually semantically wrong to me.