Results 1 to 6 of 6

Thread: Constructing a class from a different class

  1. #1

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Resolved Constructing a class from a different class

    I have two class: Library and Book.

    I want to construct a Book class by calling its class constructor from the Library class. How can i do this? My code doesn't work (Error: Cannot find symbol - Constructor Book() )
    Code:
    public void createBook()
    {
       Book newBook;
    	    
       newBook = new Book(); //Error line  
    }
    Here is the book constructor:
    Code:
    public Book(String BookAuthor, String BookTitle, String BookCost, String NumberOfPages, String BookGenre, int bookEnjoyability)
    {
       //Initialise variables
       this.BookAuthor = BookAuthor;
       this.BookTitle = BookTitle;
       this.BookCost = BookCost;
       this.NumberOfPages = NumberOfPages;
       this.BookGenre = BookGenre;
    		
       this.bookEnjoyability = bookEnjoyability;
    }
    Last edited by x-ice; Feb 25th, 2007 at 07:20 PM.

  2. #2
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Constructing a class from a different class

    You don't have the correct constructor:

    You specify this:
    public Book(String BookAuthor, String BookTitle, String BookCost, String NumberOfPages, String BookGenre, int bookEnjoyability)


    But your calling a constructor that looks like this:

    public Book()


    Specify all the parameters and it should work:


    Book b = new Book("author","title","cost","pages","genre","enjoyment");

    I would also recommend making cost and pages ints.

  3. #3
    Frenzied Member DeadEyes's Avatar
    Join Date
    Jul 2002
    Posts
    1,196

    Re: Constructing a class from a different class

    In java a default constructor is automatically created, if you don't supply your own. Once you add your own constructor then the default one is not created.

  4. #4

    Thread Starter
    Fanatic Member x-ice's Avatar
    Join Date
    Mar 2004
    Location
    UK
    Posts
    671

    Re: Constructing a class from a different class

    Quote Originally Posted by System_Error
    I would also recommend making cost and pages ints.
    Thanks for your answer, i cant change the types of these variables as the specification for my assignment says they have to be string for some reason.

  5. #5
    Lively Member
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    95

    Re: Constructing a class from a different class

    Quote Originally Posted by System_Error
    I would also recommend making cost and pages ints.
    As long as no calculations are being done on cost and pages then a string would be fine. Although you would want cost to be double, not int anyway.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Constructing a class from a different class

    Quote Originally Posted by RyanEllis
    As long as no calculations are being done on cost and pages then a string would be fine. Although you would want cost to be double, not int anyway.
    I disagree. The inexactness of floating point values makes them unsuitable for monetary calculations. You should use scaled ints or even longs. (That is, establish a convention that says that the int now stands for cents, or if you need the precision, hundredths of cents.)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width