Results 1 to 4 of 4
  1. #1
    Lennon-Guru is offline Member
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0

    Default 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

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

    public interface Comparable<T>
    {

    int compareTo(T rhs);

    }


    Java Code:
      

  2. #2
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    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.

  3. #3
    Lennon-Guru is offline Member
    Join Date
    Dec 2007
    Posts
    4
    Rep Power
    0

    Default

    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

  4. #4
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    Oh ok. Well what is the error you get when trying to compile? And what line does it occur in?

Similar Threads

  1. Creating a Comparable object
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 08:38 PM
  2. Problem With RSA Interface
    By Floetic in forum AWT / Swing
    Replies: 2
    Last Post: 03-25-2008, 11:31 AM
  3. Using Comparable and Comparator interfaces
    By barney in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •