java - Printing from Arrays -
i having trouble printing references in string method. used array story 1 of constructors' parameters. in order me use tostring have include variables including reference includes parameters constructor.
author au10 = new author("dan gheesling", 29); mydate my10 = new mydate(2011); books[9] = new book(au10, "big brother - survival guide", my10, 32.99); (book book : books) { system.out.println(book); system.out.println(); } public book(author author, string title, mydate publishing, double price) { this.author = author; this.title = title; this.publishing = publishing; } @override public string tostring() { return author.class.togenericstring() + "\n" + "title: " + this.title + "\n" + "publication date: " + this.publishing + "\n" + "price: " + this.price; }
instead of overriding tostring() method can directly print result in enhanced loop this:
for (book book : books) {
system.out.println("title: " + book.title + "\n" + "publication date: " + book.publishing + "\n" + "price: " + book.price;); system.out.println(); }
or can try using following code in yout tostring method
public string tostring() {
//whatever name of variable in author class return this.author.name + "\n" + this.author.value + "\n" + "title: " + this.title + "\n" + "publication date: " + this.publishing + "\n" + "price: " + this.price;
}
Comments
Post a Comment