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.

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below.

l11-1-data-1.txt

Java
Dean
2011
What your program must print for l11-1-data-1.txt
Java by Dean (2011)

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < l11-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content l11-1-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

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.