Loading...
Loading...
A booking form receives dates typed by people, and some are impossible, like 31 April. For each date print INVALID, or the date and the next day.
Write a class Date with fields day, month, year and valid. The constructor Date(int day, int month, int year) sets valid. A method Date next() returns a new Date for the following day. Leap years: divisible by 4, except years divisible by 100 that are not divisible by 400.
INVALID, or dd-mm-yyyy -> dd-mm-yyyy with leading zeros.Example
Input
4 28 2 2024 29 2 2023 31 12 1999 31 4 2025
Output
28-02-2024 -> 29-02-2024 INVALID 31-12-1999 -> 01-01-2000 INVALID
2023 is not a leap year, April has 30 days, and 31 December rolls into a new year.
Try the problem first. Open one hint at a time.
Write a method int daysIn(int month, int year) in the class and use it both in the constructor and in next().
A date is valid when 1 ≤ month ≤ 12 and 1 ≤ day ≤ daysIn(month, year); check the month first.
next() has three cases: the same month, the next month, or 1 January of the next year.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor