Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have to answer the following question: I got this answer: Book.java public cla

ID: 3594992 • Letter: I

Question

I have to answer the following question:

I got this answer:

Book.java

public class Book {

private int year;

private String title;

private boolean read;

// Book(year, title) constructor will set the read value to false by default

public Book(int year, String title) {

super();

this.year = year;

this.title = title;

this.read = false;

}

// Book(title) constructor will set the read value to false, year to 1960 by default

public Book(String title) {

super();

this.year = 1960;

this.title = title;

this.read = false;

}

// getter method for read

public boolean isRead() {

return read;

}

// setter method for read

public void setRead(boolean read) {

this.read = read;

}

// Override toString method to print the customized output when class object passed to print statement.

@Override

public String toString() {

return "This Book title: " + title + ", year of publication: " + year + ", read: " + read;

}

}

Test.java

public class Test {

public static void main(String[] args) {

int year = 2009;

String title = "Heart and Empire";

Book b1 = new Book(year, title);

System.out.println(b1);

b1.setRead(true);

System.out.println(b1);

title = "Desert of the Shining Stone";

Book b2 = new Book(title);

System.out.println(b2);

b2.setRead(true);

System.out.println("This book has now been read: " + b2.isRead());

}

}

But then this error message:

Can someone please tell me what I have to change?

Book.java

public class Book {

private int year;

private String title;

private boolean read;

// Book(year, title) constructor will set the read value to false by default

public Book(int year, String title) {

super();

this.year = year;

this.title = title;

this.read = false;

}

// Book(title) constructor will set the read value to false, year to 1960 by default

public Book(String title) {

super();

this.year = 1960;

this.title = title;

this.read = false;

}

// getter method for read

public boolean isRead() {

return read;

}

// setter method for read

public void setRead(boolean read) {

this.read = read;

}

// Override toString method to print the customized output when class object passed to print statement.

@Override

public String toString() {

return "This Book title: " + title + ", year of publication: " + year + ", read: " + read;

}

}

Test.java

public class Test {

public static void main(String[] args) {

int year = 2009;

String title = "Heart and Empire";

Book b1 = new Book(year, title);

System.out.println(b1);

b1.setRead(true);

System.out.println(b1);

title = "Desert of the Shining Stone";

Book b2 = new Book(title);

System.out.println(b2);

b2.setRead(true);

System.out.println("This book has now been read: " + b2.isRead());

}

}

Consider the following test code placed in the test class within the main() methood Your task is to write a Java class Book so that the expected output is produced For example Test Result int year = 2009; String title "Heart and Empire"; Book b1-new Book (title, year); System.out.println(b1); b1.setRead (true); System.out.println(b1); title- "Desert of the Shining Stone"; Book b2 = new Book(title); System.out.println(b2); b2.setRead (true); System.out.println("The book has now been read: " b2.isRead)); This Book title: Heart and Empire, year of publication: 2009, read: false This Book title: Heart and Empire, year of publication: 2009, read: true This Book title: Desert of the Shining Stone, year of publication: 1960, read: false The book has now been read: true Answer: (penalty regime: 0,10,20,50,… %) 1 public class Book f 2 II TODO your code goes here

Explanation / Answer

*************************************************************************************************************************************

Note:

The errors you are getting are might be compile time errors.

The things that could go wrong are:

1.There is a missing brackets.

2.Class declaration missing

3.Running the wrong class. Only the main method should be run, so carefully run the Test.java as it contains the main method. Book.java does not contain the main method

4.Declaring classname like class ClassName(). Do not use ()

5.The classes are in different packages.

Kindly see to it that both the classes are in the same package and carefully copy the code from the starting to the end.

I have attached the working code with this, both the classes are included and also the required output is given at the end of this answer. [You might be missing a ''{" or "}"]

Kindly rate the answer if you find it helpful.

*************************************************************************************************************************************

Book.java

public class Book {

   private int year;

   private String title;

   private boolean read;

   // Book(year, title) constructor will set the read value to false by default

   public Book(int year, String title) {

       super();

       this.year = year;

       this.title = title;

       this.read = false;

   }

   // Book(title) constructor will set the read value to false, year to 1960 by
   // default

   public Book(String title) {

       super();

       this.year = 1960;

       this.title = title;

       this.read = false;

   }

   // getter method for read

   public boolean isRead() {

       return read;

   }

   // setter method for read

   public void setRead(boolean read) {

       this.read = read;

   }

   // Override toString method to print the customized output when class object
   // passed to print statement.

   @Override

   public String toString() {

       return "This Book title: " + title + ", year of publication: " + year + ", read: " + read;

   }

}

Test.java

public class Test {

   public static void main(String[] args) {

       int year = 2009;

       String title = "Heart and Empire";

       Book b1 = new Book(year, title);

       System.out.println(b1);

       b1.setRead(true);

       System.out.println(b1);

       title = "Desert of the Shining Stone";

       Book b2 = new Book(title);

       System.out.println(b2);

       b2.setRead(true);

       System.out.println("This book has now been read: " + b2.isRead());

   }

}