Help with Java Programming

Associate
Joined
25 May 2006
Posts
13
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.
 
Last edited:
What have you got so far?

It's terribly lame to just c+p your question into a forum and ask others to do it for you..
 
Don't know java but I do know C# and its fairly similar.

You will havee to subtely change it too java but it will be something laong the lines of
Code:
public int CompareTo(MyInt obj)
{
   If (MyInt != null)
      {
          return (obj.value.CompareTo(this.value);
      }
}
 
What this question test for I dont understand yet :p They ask you to use an interface and they give you the method already overriden :p That defeats the idea of what is interface IMO :p

They could just say write a simple method that does the xyz crap :)
 
drak3 said:
What this question test for I dont understand yet :p They ask you to use an interface and they give you the method already overriden :p That defeats the idea of what is interface IMO :p

They could just say write a simple method that does the xyz crap :)
They've done the complete opposite, infact.
 
Lame it may be, and I'm sorry to have to do it but if you understood the difficulties I'm having with Java then you might appreciate my position a bit better. The truth is unfortunately that I don't have anything so far. I have been able to write very simple programmes (ie, get methods, if statements etc) but I was hoping that someone would be able to show me how a more complex one was done -- I don't really want to start this course with no experience of Java at all.

I'm sure it's very easy to hardened Java programmers, but as Subliminal Aura so eloquently put it, I am indeed a Noob :)
 
eriedor said:
Don't know java but I do know C# and its fairly similar.

You will havee to subtely change it too java but it will be something laong the lines of
Code:
public int CompareTo(MyInt obj)
{
   If (MyInt != null)
      {
          return (obj.value.CompareTo(this.value);
      }
}

Unfortunately that wont work as the int is declared as private, so is only accessible from within the methods of this class.

So.... to answer the question...



MyInt(int x) {...}

Your Initialisation method. You pass it an int, you set "value = x;"

public String toString() {...}

If you want to return a string of the int, you call this.
(There's various ways to do this, look at the Integer class)


public int intValue() {...}

An accessor method, "return value"

public int compareTo(MyInt rhs){...}

The interesting bit, presumably you want to call the intValue method on the MyInt that you passed in and then compare the two. Use the comparison to return an int of the appropriate value.

public boolean equals(Object rhs) {...}

Similar to above, but return a boolean instead of an int.
 
This so looks like one of the practical questions I got asked in first year :p
 
Back
Top Bottom