THINK FIRST·CODE LATER

← All labs

SimpleDate: three constructors and methods that return objects

Problem

Write an immutable class SimpleDate (private final year, month, day) and a public class DateApp.

  • Three overloaded constructors: SimpleDate(int, int, int), SimpleDate(String iso) for "2026-03-07", and a copy constructor — the last two must call the first with this(…).
  • Static helpers isLeap(year) and daysInMonth(year, month).
  • nextDay() and plusDays(n) return a new SimpleDate; isBefore(other), equals(other), toString() → 2026-03-07.

main reads a date as text, a date as three integers, and a number of days.

For the input

2026-02-27 2026 3 1 3

the program prints

a = 2026-02-27, b = 2026-03-01
day after a: 2026-02-28
a + 3 days: 2026-03-02
a is unchanged: 2026-02-27
a before b: true
a equals b: false
copy equals a: true, same object: false

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

l11-5-data-1.txt

2026-02-27 2026 3 1 3
What your program must print for l11-5-data-1.txt
a = 2026-02-27, b = 2026-03-01
day after a: 2026-02-28
a + 3 days: 2026-03-02
a is unchanged: 2026-02-27
a before b: true
a equals b: false
copy equals a: true, same object: false

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-5-data-1.txt

Windows PowerShell does not understand <:

Get-Content l11-5-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.