Overview:
In this lab you are to write a Java program to prompt the user for an integer indicating a year
and then print the calendar for that year.
Objective:
This lab’s objective is to exercise you with the use of Java’s control statements. You are
required to use exactly one while statement, one for statement and one switch statement.
You will also practice on how to use some basic input methods of the Scanner class and some
formatting techniques of method printf().
Activities:
1. The JulianDate class is used to determine the day of the week for the 1st day
of January.
JulianDate JD = new JulianDate();
int date = JD.toJulian(yr,1,1);
int dayOfWeek = (date+1)%7; // 0 means Sunday, 1 means Monday, etc.
2. Notes:
1. No arrays are allowed in this lab.
2. Your output should be closely similar to the output of the instructor’s sample program.
3. To determine whether a year is a leap year or not:
a. If the year is a century year, the year must be divisible by 400.
b. If the year is not a century year, the year only needs to be divisible by 4.
Expert Answer
import java.time.*;
import java.util.Scanner;
public class Four29
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Enter year: “);
int yr = in.nextInt();
LocalDate ld=LocalDate.of(yr,Month.JANUARY,1);
int day = ld.getDayOfMonth()-1;
for (int m=1; m<=12; m++)
{
String monthN=””;
int numD=0;
switch (m)
{
case 1:
monthN=”January “;
numD=31;
break;
case 2:
monthN=”February “;
if ((yr%4 == 0 && yr%100 != 0) || yr%400 == 0 )
numD=29;
else numD=28;
break;
case 3:
monthN=”March “;
numD=31;
break;
case 4:
monthN=”April “;
numD=30;
break;
case 5:
monthN=”May “;
numD=31;
break;
case 6:
monthN=”June “;
numD=30;
break;
case 7:
monthN=”July “;
numD=31;
break;
case 8:
monthN=”August “;
numD=31;
break;
case 9:
monthN=”September “;
numD=30;
break;
case 10:
monthN=”October “;
numD=31;
break;
case 11:
monthN=”November “;
numD=30;
break;
case 12:
monthN=”December “;
numD=31;
break;
}
System.out.println(“n ” + monthN + yr);
System.out.println(“_______________________________________”);
System.out.println(“Sun Mon Tue Wed Thu Fri Sat”);
for (int sp=1; sp<=day; sp++)
System.out.print(” “);
for (int p=1; p<=numD; p++)
{
if (day%7==0 && day!=0)
System.out.println();
System.out.printf(“%3d “, p);
day+=1;
}
day%=7;
System.out.print(“nn”);
in.close();
}
}
}