THINK FIRST·CODE LATER

← All labs

Days in a month (leap years)

Problem

Write a class DaysInMonth that reads a year and a month (1–12) and prints the number of days, as Days: 29.

A year is a leap year when it is divisible by 4, except years divisible by 100, unless they are also divisible by 400. So 2000 is a leap year, 1900 is not.

If the month is not between 1 and 12, print Invalid month.

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.

l6-2-data-1.txt

2024 2
What your program must print for l6-2-data-1.txt
Days: 29

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

Windows PowerShell does not understand <:

Get-Content l6-2-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.