Vote count:
0
I'm a newbie working on a basic class creation tutorial and have been given the below instructions.
1) Create a new class called Book.
2) Create fields for title, author, numPages, and isbn.
3) Create a method called toString that appropriately describes the book.
4) In the file, BookRunner.java:
a) Declare two book variables
b) Instantiate two book objects and set their fields.
c) Print descriptions of the book objects using their toString methods.
So I've been working with two classes as instructed, and you can see the code below
public class Book {
public String title;
public String author;
public String numPages;
public String isbn;
public void toString(String bookName) {
String description = "Title:" + title + "Author"+ author + "Num. of pages" + numPages + "ISBN" + isbn;
System.out.println(description);
}
public class Ex1_BookRunner {
public static void main(String[] args) {
Book firstBook;
Book secondBook;
firstBook = new Book();
secondBook = new Book();
firstBook.title = "One Piece";
firstBook.author = "Oda-Sensei";
firstBook.numPages = "100";
firstBook.isbn = "123456";
secondBook.title = "Life of Megan Fox";
secondBook.author = "Micheal Bay";
secondBook.numPages = "200";
secondBook.isbn = "098765";
toString(firstBook);
toString(secondBook);
}
}
My code shows no errors until the last two lines where I call the method toString
.
I get the error below
The method toString() in the type Object is not applicable for the arguments (Book)
Am I making some fundamental error somewhere in my method declaration? I've looked at other posts on SO with the same question but I have trouble understanding the explanations given because they mostly cover code syntax I haven't learnt yet.
Any help is appreciated :)
asked 1 min ago
Method in the type is not applicable to arguments
Aucun commentaire:
Enregistrer un commentaire