Hi Guys,
I'm hoping (and praying) someone will be able to help me here.
I'm trying to teach myself a bit of Java coding before starting a programming course in the next few weeks. I'm OK with simple programs but am struggling with some of those which are a little more advanced (for me at least).
Lately, I've been battling with this question from an example exam paper I found, but really am having trouble figuring out even where to start.
"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) {...}
}
Would anyone be kind enough to show me how they'd go about it (assuming it's not too time consuming)? Or at least explain how to go about it? At least then I'd be able to go through it and see how it's done.
Thanks very much for any help.
I'm hoping (and praying) someone will be able to help me here.
I'm trying to teach myself a bit of Java coding before starting a programming course in the next few weeks. I'm OK with simple programs but am struggling with some of those which are a little more advanced (for me at least).
Lately, I've been battling with this question from an example exam paper I found, but really am having trouble figuring out even where to start.
"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) {...}
}
Would anyone be kind enough to show me how they'd go about it (assuming it's not too time consuming)? Or at least explain how to go about it? At least then I'd be able to go through it and see how it's done.
Thanks very much for any help.
Last edited: