-4

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>();
    }
}
userb
  • 173
  • 2
  • 15
  • 1
    "gives me an error" - generally, it's helpful to say specifically what the error is – Dave Costa May 24 '15 at 19:18
  • You tried to instanciate an interface without implementing the compareTo() method. – Peter Paul Kiefer May 24 '15 at 19:20
  • error = "Cannot instantiate the Type Comparable" – userb May 24 '15 at 19:21
  • Does that mean I have to implement compareTo() next to Rational in main? – userb May 24 '15 at 19:21
  • No, what is your intention, why would you create the interface with new. You have a Class Rational1 that implements the interface. Instanciating the class does work. So why not use the class? By the way your copy contructor (the one where you pass a Rastional1 po object) is complete nonsense and the compareTo method is useless also. – Peter Paul Kiefer May 24 '15 at 19:25

2 Answers2

2

First things first, even if it is not direclty related with the question your constructor

public Rational1(Rational1 po) {
    po = new Rational1(numer, denom);
}

is a complete nonsense. It should be

public Rational1(Rational1 po) {
     this(po.numer, po.denom);
}

Your main method is also wrong, you have to provide a concrete class not an interface (an interface can never be instantiated) :

public static void main(String[] args){
    Rational1 rational = new Rational1(4,3);
    Comparable<Rational1> ration = new Rational1();
}

Finally, your implementation of the comparison is wrong because :

  • it's mathematically wrong
  • the signature of compareTo is not correct. It should be int compareTo(Rational1 that).
  • you are constructing a new instance instead of considering your parameter. It should be

    @Override
    public int compareTo(Rational1 that) {
        return Integer.compare(numer*that.denom, that.numer*denom);
    }
    

Always use @Override when implementing an abstract class/interface to make sure you are indeed overriding the abstract members.

Dici
  • 25,226
  • 7
  • 41
  • 82
  • "comparison code is correct" - seriously? It discards the object instance that it is passed and created a new one, does that seem logical? – Dave Costa May 24 '15 at 19:23
  • Well I scanned the code very quickly, I did not even see that the signature was not correct – Dici May 24 '15 at 19:24
  • @DaveCosta fixed, sorry for this I actually hesitated at first to close it as homework and then posted a very quick answer, but I should have read more carefully (I did not because of its poor quality, but it still my fault to post a wrong answer on top of it) – Dici May 24 '15 at 19:30
  • *"the signature of compareTo is not correct. It should be int compareTo(Rational1 that)"* His assignment says something else, so I assume he can't change it. – Tom May 24 '15 at 19:31
  • @Tom then he should not be using generics, otherwise he's simply not able to compile his code – Dici May 24 '15 at 19:33
  • @Dici Why shouldn't it compile? – Tom May 24 '15 at 19:34
  • @Tom because the signature `int compareTo(Object that)` does not override the methode `compareTo` in `Comparable` – Dici May 24 '15 at 19:36
  • @Dici No? The method `compareTo` in _his own_ `Comparable` Interface looks like this `public int compareTo(Object other)`. So there is the problem? – Tom May 24 '15 at 19:37
  • @Tom I assume it was a mistake because the parameter class is not used, but it might not be. – Dici May 24 '15 at 19:41
2
  1. Comparable<Rational1> is an interface. It has no constructor. You can't instantiate an instance of it. I assume this is the source of the error you are currently hitting, although you don't specify what the error is.

  2. From the problem description (first paragraph), it's not clear what you are trying to do in main() at all. Your main goal is to write a definition for the compareTo() method, which you've done.

  3. That said, the compareTo() method that you have written is nonsensical. You need to think (or read) more about what a rational number is and how you would go about comparing two rational numbers with one another.

Dave Costa
  • 47,262
  • 8
  • 56
  • 72