Question & Answer: Overview: In this lab you are to write a Java program to prompt the user for an integer indicating a year…..

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.util.Scanner;
public class LabTwo{
public static void main(String[] args){
System.out.println(“what year is it?”);
Scanner year = new Scanner(System.in);
int yr = year.nextInt();
System.out.printf(“%11dn”, yr);
int days = 0;
JulianDate JD = new JulianDate();
int date = JD.toJulian(yr,1,1);
int dayOfWeek = (date+1)%7;
for(int i = 1; i<=12; i++){
switch(i){
case 1: System.out.printf(“%14sn”,”January”);
days = 31; break;
case 2: System.out.printf(“%15sn”,”February”);
if(yr%100 == 0){
if(yr%400 == 0){
days = 29;
}   else{
days = 28;
}
}   else if(yr%4 == 0){
days = 29;
}   else{
days = 28;
}
break;
case 3:System.out.printf(“%13sn”,”March”);
days = 31; break;
case 4: System.out.printf(“%13sn”, “April”);
days = 30; break;
case 5: System.out.printf(“%12sn”, “May”);
days = 31; break;
case 6: System.out.printf(“%13sn”, “June”);
days = 30; break;
case 7: System.out.printf(“%13sn”, “July”);
days = 31; break;
case 8: System.out.printf(“%14sn”, “August”);
days = 31; break;
case 9: System.out.printf(“%15sn”, “September”);
days = 30; break;
case 10: System.out.printf(“%14sn”, “October”);
days = 31; break;
case 11: System.out.printf(“%14sn”, “November”);
days = 30; break;
case 12: System.out.printf(“%15sn”, “December”);
days = 31; break;
}
System.out.println(” S M T W T F S”);
if(dayOfWeek > 0){
System.out.printf(“%” + (dayOfWeek * 3) +”c”, ‘ ‘);
}
int j = 1;
while(j <= days){

System.out.printf(“%3d”, j);
dayOfWeek++;
if(dayOfWeek == 7 ){
System.out.println();
dayOfWeek = 0;
}
j++;
}
System.out.println();
System.out.println();
}

}
}

Still stressed from student homework?
Get quality assistance from academic writers!