First thing I noticed Book constructors are the main issue.
Your first constructor has two parameters title
and year
.
Book b1 = new Book(title, year);
But in the second constructor has one parameter title
and you have missed the year
. That’s why you didn’t get year
.
Book b2 = new Book(title);
You can correct it by editing the above statement from the below statement.
Book b2 = new Book(title, year);
Second thing you try to print object like System.out.println(b2);
without implementing toString()
method.
Please try following code. You can get the expected output without directly using the Book()
constructor.
class Test {
public static void main(String[] args) {
Book b1 = new Book();
b1.setYear(1975);
b1.setTitle("The God and the Sword");
System.out.println(b1);
b1.setRead(true);
System.out.println(b1);
Book b2 = new Book();
b2.setTitle("The Light of Spirind");
b2.setYear(1960);
System.out.println(b2);
b2.setRead(true);
System.out.println("The book has now been read: " + b2.isRead());
}
}
class Book {
private boolean read;
private int year;
private String title;
public boolean isRead() {
return read;
}
public void setRead(boolean read) {
this.read = read;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
Book() {
}
public String toString() {
return "This Book\ntitle: " + getTitle() + ",\n " + "year of publication: " + getYear() + ",\n " + "read: " + isRead();
}
}
solved Class to return