THINK FIRST·CODE LATER

← All labs

Book: overloaded constructors and equality

Problem

Write a class Book with private fields title (String), author (String) and year (int), and:

  • a constructor with the three values;
  • a second constructor with only title and author, which sets the year to 2026 (constructor chaining with this(...));
  • toString() returning title by author (year);
  • equals(Object other) returning true when the title and the author are the same (ignore the year).

The class BookApp reads one line for the title and one for the author, creates a book with each constructor and prints:

Java by Dean (2011)
Java by Dean (2026)
Same book: true

Read the year from the keyboard for the first book.

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.