Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-04-2008, 10:22 PM
Member
 
Join Date: Dec 2007
Posts: 4
Lennon-Guru is on a distinguished road
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 I can't seem to understand why it's not compiling, can anyone help me please?

Thank You

Here's the code so far

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); } }
import java.util.*;

public interface Comparable<T>
{

int compareTo(T rhs);

}


Code:
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-04-2008, 10:24 PM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
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.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-04-2008, 10:34 PM
Member
 
Join Date: Dec 2007
Posts: 4
Lennon-Guru is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-05-2008, 02:17 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Oh ok. Well what is the error you get when trying to compile? And what line does it occur in?
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a Comparable object Java Tip java.lang 0 04-15-2008 09:38 PM
Problem With RSA Interface Floetic AWT / Swing 2 03-25-2008 12:31 PM
An interface extending an interface(II) JavaForums Java Blogs 0 03-12-2008 05:00 PM
An interface extending an interface(I) JavaForums Java Blogs 0 03-12-2008 05:00 PM
Using Comparable and Comparator interfaces barney New To Java 1 08-07-2007 09:10 AM


All times are GMT +3. The time now is 04:43 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org