Results 1 to 7 of 7
  1. #1
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default Comparable checks

    hi i am trying to implement a sorted tree structure where each treenode element must be Comparable. The problem i am having is in the constructors.

    I first have a constructor such that:
    Java Code:
     public SortedTreeNode(Comparable element, ArrayList<SortedTreeNode> offspring) {
                this.offspring = offspring;
                this.element = element;
            }
    I think this is fine and will only allow a treenode to be constructed with an element that must be Comparable However i have another constructor with no parameters (Default Constructor) but i cant make element be of type Comparable when i try:
    Java Code:
     public SortedTreeNode() {
                this.offspring = new ArrayList();
                this.element = new Comparable();
            }
    This throws error saying comparable is abstract ? How can i make the check so that when a tree node is constructed it must be of type Comparable??

    Thanks.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Comparable checks

    Comparable is an interface, rather than a class. So you have to specify the behaviour of a Comparable thing if you create one. (Likewise if a class implements Comparable you have to give the specific Comparable behaviour of that class.) In the jargon Comparable is said to be "abstract": meaning that the compiler will know that element has a compareTo() method, but are supposed to actually define it - or use a class that already implements Comparable. The error you get is the compiler saying "A comparable what?!".

    What did you mean the no argument constructor to do? I can see that it creates a node with no offspring. But what did you expect the payload (element) to be in that case?

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Comparable checks

    How can i make the check so that when a tree node is constructed it must be of type Comparable?
    The most obvious way would be not to have a no argument constructor. Instead have a constructor that takes a Comparable as its only argument.

  4. #4
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default Re: Comparable checks

    the default is supposed to just create a node with empty offspring and an empty element of type comparable.

  5. #5
    Billywizz is offline Member
    Join Date
    Jan 2011
    Posts
    57
    Rep Power
    0

    Default Re: Comparable checks

    So element could be anything comparable eg. Integer, String ...

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Comparable checks

    What good is an empty Comparable object? What would it be used for?
    Can you set the variable to null?

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Comparable checks

    The concept of an "empty" object doesn't really make sense. The nearest, I guess, is a variable whose value is null. I agree with Norm's question: what will the element be used for?

    (Comparable)null will keep the compiler happy. Although element will have the value null anyway. (If you do that you have to be careful thereafter not to call element.compareTo() since it'll give a null pointer exception.)

Similar Threads

  1. Problem With A Program That Checks For User's Name
    By Interista in forum New To Java
    Replies: 3
    Last Post: 11-17-2011, 04:01 PM
  2. Comparable and Comparator
    By jeanjiang in forum New To Java
    Replies: 7
    Last Post: 04-23-2011, 07:59 AM
  3. A String method that checks to see the data is numeric?
    By eLancaster in forum New To Java
    Replies: 7
    Last Post: 02-09-2011, 06:32 PM
  4. Replies: 8
    Last Post: 01-15-2011, 09:39 PM
  5. Replies: 5
    Last Post: 09-09-2008, 04:37 PM

Posting Permissions

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