I am trying to add on to this program so I can compare two rational numbers (that will be defined later in a driver class) using the Comparable interface. However when I make the public int compareTo, it does not allow me to declare more than 1 rational obbject. How can I do this?
public class Rational implements Comparable <Rational, Rational>
{
private int numerator;
private int denominator;
public Rational (int numer, int denom)
{ // begins block
if (denom == 0)
denom = 1;
if (denom < 0)
{
numer = numer * -1;
denom = denom * -1;
}
numerator = numer;
denominator = denom;
reduce();
}
public int getNumerator ()
{
return numerator;
}
public int getDenominator ()
{ // begins block
return denominator;
}
public Rational reciprocal ()
{ // begins block
return new Rational (denominator, numerator);
}
public Rational add (Rational op2)
{ // begins block
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int sum = numerator1 + numerator2;
return new Rational (sum, commonDenominator);
}
public Rational subtract (Rational op2)
{ // begins block
int commonDenominator = denominator * op2.getDenominator();
int numerator1 = numerator * op2.getDenominator();
int numerator2 = op2.getNumerator() * denominator;
int difference = numerator1 - numerator2;
return new Rational (difference, commonDenominator);
}
public Rational multiply (Rational op2)
{ // begins block
int numer = numerator * op2.getNumerator();
int denom = denominator * op2.getDenominator();
return new Rational (numer, denom);
}
public Rational divide (Rational op2)
{ // begins block
return multiply (op2.reciprocal());
}
public boolean equals (Rational op2)
{ // begins block
return ( numerator == op2.getNumerator() && denominator == op2.getDenominator() );
}
public String toString ()
{ // begins block
String result;
if (numerator == 0)
result = "0";
else
if (denominator == 1)
result = numerator + "";
else
result = numerator + "/" + denominator;
return result;
}
private void reduce ()
{ // begins block
if (numerator != 0)
{ // begins block
int common = gcd
(Math.abs(numerator), denominator);
numerator = numerator / common;
denominator = denominator / common;
} // ends block
}
private int gcd (int num1, int num2)
{
while (num1 != num2)
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
return num1;
}
public int compareTo (Rational r1, Rational r2)
{
Float value1 = new Float ((float)r1.getNumerator() / r1.getDenominator());
Float value2 = new Float ((float)r2.getNumerator() / r2.getDenominator());
int answer = value1.compareTo(value2);
return answer;
}
}
The compareTo
method of the Comparable
interface needs only one parameter: the other instance to compare against.
And so it takes only one type parameter.
To fix your class declaration:
public class Rational implements Comparable<Rational>
Following the pattern in your current compareTo
implementation,
with some minor corrections and improvements,
here's one way to implement the compareTo
of Comparable
:
@Override
public int compareTo(Rational o) {
float mine = (float) getNumerator() / getDenominator();
float other = (float) o.getNumerator() / o.getDenominator();
return Float.compare(mine, other);
}