|
-
Oct 25th, 2005, 04:15 PM
#1
Thread Starter
Fanatic Member
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.
-
Oct 25th, 2005, 06:31 PM
#2
Frenzied Member
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.
-
Oct 26th, 2005, 03:01 AM
#3
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.
-
Oct 26th, 2005, 03:28 AM
#4
Thread Starter
Fanatic Member
Re: Constructing a class from a different class
 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.
-
Oct 27th, 2005, 01:21 PM
#5
Lively Member
Re: Constructing a class from a different class
 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.
-
Oct 28th, 2005, 05:19 AM
#6
Re: Constructing a class from a different class
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|