Results 1 to 4 of 4
Thread: interface Comparable<T> problem
- 03-04-2008, 08:22 PM #1
Member
- Join Date
- Dec 2007
- Posts
- 4
- Rep Power
- 0
interface Comparable<T> problem
Hello,
I have an Java library interface, and I have to implement the MyInt class, it gives me the error int cannot be dereferenced:confused: I can't seem to understand why it's not compiling, can anyone help me please?
Thank You
Here's the code so far
import java.util.*;Java Code:import java.util.*; public class MyInt { private int value; public MyInt(int value) { this.value = value; } public String toString() { return ("My integer " + value); } public int getInteger() { return value; } public int compareTo(MyInt rhs) { if (value < rhs.value) return -1; if (value > rhs.value) return 1; return 0; } // public int intValue() // { // return intValue; // // } public boolean equals(Object rhs) { if (!(rhs instanceof MyInt)) return false; MyInt m = (MyInt)rhs; return m.value.equals(value); } }
public interface Comparable<T>
{
int compareTo(T rhs);
}
Java Code:
- 03-04-2008, 08:24 PM #2
Why are you using Comparable generically. If something implements Comparable, you don't really need to use generics. But idk, maybe I'm not understanding something.
- 03-04-2008, 08:34 PM #3
Member
- Join Date
- Dec 2007
- Posts
- 4
- Rep Power
- 0
Hello, gibsonrocker800
It's coursework from Uni, and that's the spec I was given.
Here it it is
5. Consider the following Java Library interface
public interface Comparable<T>
{
int compareTo(T rhs);
}
Complete the implementation of the class below that implements the above interface. The compareTo method should return -1 if value is less than rhs.value, 0 if both sides are equal and +1 if value is greater than rhs.value.
public class MyInt implements Comparable<MyInt>
{
private int value;
MyInt(int x) {...}
public String toString() {...}
public int intValue() {...}
public int compareTo(MyInt rhs){...}
public boolean equals(Object rhs) {...}
}
Thanks
- 03-05-2008, 12:17 AM #4
Similar Threads
-
Creating a Comparable object
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:38 PM -
Problem With RSA Interface
By Floetic in forum AWT / SwingReplies: 2Last Post: 03-25-2008, 10:31 AM -
Using Comparable and Comparator interfaces
By barney in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:10 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks