// develop the Fraction class here, no main method
class Fraction
{
int num;
int den;
// This replaces the default constructor for the Fraction class
public Fraction()
{
this.num = 0;
this.den = 1;
}
// This is another constructor for the Fraction class.
// It can be distinguished from other constructors
// by the parameter list. The function is *overloaded*.
public Fraction(int n, int d)
{
this.num = n;
this.den = d;
}
// Instance method to determine the size (absolute value)
// of the fraction as a decimal.
public double size ()
{
return Math.abs((double)this.num/this.den);
}
// compare two Fraction objects, return reference to
// larger Fraction object
public Fraction larger (Fraction other)
{
if (this.size() >= other.size())
return this;
else
return other;
}
// multiply the current fraction by the given fraction, p
// store the result in the current fraction
public void timesEquals(Fraction p)
{
this.num *= p.num;
this.den *= p.den;
}
// add the current fraction to the given fraction, and
// store the result in the current fraction
public void plusEquals(Fraction p)
{
this.num = this.num * p.den + p.num * this.den;
this.den = this.den * p.den;
}
// multiply two fractions, return reference to the
// (new) result
public Fraction times (Fraction other)
{
Fraction result = new Fraction();
result.num = this.num * other.num;
result.den = this.den * other.den;
return result;
}
// add two fractions, return reference to the
// (new) result
public Fraction plus (Fraction other)
{
Fraction result = new Fraction();
result.num = this.num * other.den + this.den * other.num;
result.den = this.den * other.den;
return result;
}
}
Comments (0)
You don't have permission to comment on this page.