Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Java Basics
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 4th, 2007, 07:03 PM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Implementing The Comparable Interface

Hello,

I'm having a problem with a Java program that needs to implement the Comparable<E> interface. Its basically for a HuffmanNode class to construct elements of a Huffman Tree. My nodes need to be comparable to integer values (ie, the frequency of the characters in each node), but I'm having some problems writing the class header to start my problem.

A couple examples:
When my class header reads as

Code:
public class HuffmanNode implements Comparable<Integer> {
I get an error that says

Quote:
quote:HuffmanNode is not abstract and does not override abstract method compareTo(java.lang.Integer) in java.lang.Comparable
So I tried changing the type of comparable to another HuffmanNode. My logic was if I can compare two nodes, it should be easy enough to cast an int to a HuffmanNode. This brought about the header

Code:
public class HuffmanNode implements Comparable<HuffmanNode> {
Which brought about pretty much the same error as before:

Quote:
quote: HuffmanNode is not abstract and does not override abstract method compareTo(HuffmanNode) in java.lang.Comparable
Same idea again:
Header:

Code:
public class HuffmanNode implements Comparable {
produced the error

Quote:
quote:HuffmanNode is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
I then tried to make it generic:

Code:
public class HuffmanNode implements Comparable<E> {
produced:

Quote:
quote: cannot find symbol
symbol: class E
After this, I decided to try and change the header a bit more, so I changed implements to extends and that only produced a no interface expected error. That wasn't really a surprise, I knew it would work with an interface, but thought it was worth a try.

Anyway, to my question finally. How can I get my HuffmanNode class to implement the Comparable<E> interface? I'm using the Dr Java IDE, with Java 6, using Windows Vista.


 
Old March 5th, 2007, 02:03 PM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ah, never mind. I've finally figured out what I was doing wrong. My method signature for the compareTo method was wrong.

 
Old April 8th, 2007, 10:53 PM
Registered User
 
Join Date: Apr 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

I'm having the same problem but i don't understand what you mean by : My method signature for the compareTo method was wrong. I've look at my method compareTo and i can't find an error...well i'm not a pro so can you please help me with any indication?

thx.

 
Old April 23rd, 2007, 04:03 AM
Authorized User
 
Join Date: Nov 2005
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default


A signature of a method consists of its name and its arguments types but ignoring its return type.
For example, the method compareTo in the Comparable interface is written as:

public int compareTo(Object o);

(Remember that, because it is defined in an interface, the method is implicitly abstract) but its signature is just:

compareTo(Object)

If you have a class called ClassX that is supposed to implement the Comparable interface but instead of making a method in this class with the above signature you make a method with a different signature, such as one that is like the above method but has a different type of argument or has no arguments such as one with the signature:

compareTo( )

Then this will not override but overload the abstract method inherited from the Comparable interface and you will get an error message like:

ClassX is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable

 Also remember that java is case sensitive so that the signatures: CompareTo(Object) and compareto(Object) are not the same signatures as: compareTo(Object)

I hope this helps. AndrewH:)

 
Old May 18th, 2007, 05:03 AM
Authorized User
 
Join Date: Mar 2007
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The main purpose of Comparable interface is compare to Objects
the signature is look like this
c:/> javap java.lang.Comparable
public interface Comparable {
        public abstract int compareTo(java.lang.Object);
}
Note: if u implement the Comparable interace u need not to decrease the previlige
      i.e if Comparable interface the function always in public mode
      public int compareTo(Object);

for example u try to compare two Integer references the code look like this
public int compareTo(Integer i) {
       if(this.i > i)
             return 1;
        return 0;
}
the compareTo return only 0, 1 and -1




K.Mallikarjunarao





Similar Threads
Thread Thread Starter Forum Replies Last Post
implementing a region in interface mohammadalsayeh .NET Framework 2.0 1 August 27th, 2007 08:28 PM
Problem With Implementing an Interface brettk_1 General .NET 1 February 28th, 2007 11:47 AM
Implementing GPS aravwind General .NET 5 November 3rd, 2004 09:43 PM
implementing Graphs arvindwrites Classic ASP Basics 1 September 6th, 2004 05:08 AM
Implementing accelerators Ibn_Aziz Java GUI 0 April 1st, 2004 02:15 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.