Results 1 to 5 of 5
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Constructor Not Used ?

    Consider the following Code:

    class Node
    {
    int iData; // data used as key value
    double fData; // other data
    node leftChild; // this node’s left child
    node rightChild; // this node’s right child
    public void displayNode()
    {
    // (see Listing 8.1 for method body)
    }
    }

    class Tree
    {
    private Node root; // the only data field in Tree
    public void find(int key)
    {
    }
    public void insert(int id, double dd)
    {
    }
    public void delete(int id)
    {
    }
    // various other methods
    } // end class Tree

    class TreeApp
    {
    public static void main(String[] args)
    {
    Tree theTree = new Tree; // make a tree
    theTree.insert(50, 1.5); // insert 3 nodes
    theTree.insert(25, 1.7);
    theTree.insert(75, 1.9);
    node found = theTree.find(25); // find node with key 25
    if(found != null)
    System.out.println(“Found the node with key 25”);
    else
    System.out.println(“Could not find node with key 25”);
    } // end main()
    } // end class TreeApp

    In the above class, TreeApp, why the constructor with no arguments is not called while creating the object of the "Tree" class using the following statement:

    Tree theTree = new Tree;


    Shouldn't it be like the following?:

    Tree theTree = new Tree();

    Please state the reason.

    Thanks

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,380
    Blog Entries
    7
    Rep Power
    20

    Default Re: Constructor Not Used ?

    Yep, there are parentheses needed there if you want to call the no-arg constructor. That book isn't even worth the trees it's made from ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Re: Constructor Not Used ?

    Any other Java book recommended for Algorithms?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Constructor Not Used ?

    Don't double post the same question. See the moderator remarks in Reference !

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    kaydell2 is offline Senior Member
    Join Date
    Dec 2012
    Posts
    107
    Rep Power
    0

    Default Re: Constructor Not Used ?

    Yes, when you call a constructor, you will always have parentheses.

    Also, I found some other problems with the code that you posted:

    1. Some places have "node" with a lower-case "n" that should be "Node" with an upper-case "N". "Node" is the class name which begins with an upper-case "N".

    2. There were some curly-quotation marks that need to be straight-quotation marks:

    System.out.println("Found the node with key 25"); // I fixed this line of code by changing the curly quotes to straight quotes.

    3. The method, find() should return a Node rather than having "void" as the return type.

Similar Threads

  1. Replies: 5
    Last Post: 08-11-2012, 09:50 AM
  2. Java - Constructor Method versus Constructor
    By brocksoffice in forum New To Java
    Replies: 1
    Last Post: 08-01-2012, 09:17 AM
  3. Replies: 5
    Last Post: 12-14-2011, 01:47 PM
  4. Replies: 0
    Last Post: 12-19-2007, 09:10 AM
  5. Replies: 0
    Last Post: 12-19-2007, 09:01 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
  •