Assuming you have a class named Rational where each object contains two ints representing the numerator and the denominator, write the class definition line if you wanted to indicate that it would implement the generic interface Comparable and write the body of the required method with the signature:
public int compareTo(Object other);
so that it will only return -1 or 0 or +1 based on the relative order of the two objects based on the numerators and denominators.
I don't understand how to create a generic for Rational1, that takes the two integers (numer, denom). Any help with this, will be greatly appreciated. This is my Rational1 class so far:
public class Rational1 implements Comparable<Rational1> {
private int numer;
private int denom;
public Rational1(int numer,int denom){
this.numer = numer;
this.denom = denom;
}
public Rational1(Rational1 po){
po = new Rational1(numer, denom);
}
public int compareTo(Object other){
other = new Rational1(numer, denom);
if(numer>denom){
return 1;
}
else if(numer<denom){
return -1;
}
else{
return 0;
}
}
}
And this is my interface:
public interface Comparable<Rational1> {
public int compareTo(Object other);
}
And Lastly, my main which gives me an error on the last line when I call the generic:
public class Rational {
public static void main(String[] args){
Rational1 rational = new Rational1(4,3);
Comparable<Rational1> ration = new Comparable<Rational1>();
}
}