Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user to enter the year and an integer for the f

ID: 3590203 • Letter: W

Question

Write a program that prompts the user to enter the

year and an integer for the first day of the year (Sunday is 0,

Monday is 1, ...., and Saturday is 6) and displays the calendar

table for the year. For example, if the user entered the year

2017 and 0 for Sunday, January 1, 2017, your program should

display the calendar for each month in the year as follows:

Enter a year: 2017

Enter the first day of the year

(Sunday is 0, Monday is 1, ..., and Saturday is 6): 0

January 2017

---------------------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

.....

December 2017

-----------------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

Explanation / Answer

import java.util.*;

//Class MyCalendar definition

public class MyCalendar

{

//Main method definition

public static void main(String[] args)

{

//Scanner class object created

Scanner sc = new Scanner(System.in);

//Accepts year

System.out.print("Enter the year: ");

int year = sc.nextInt();

//Accepts day

System.out.print("Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): ");

int userFirstDay = sc.nextInt();

//Loops from 1 to 12 for months

for ( int months = 1; months <= 12; months++ )

{

//Initializes days to zero

int days = 0;

//Initializes monthName to null

String monthName = " ";

//Switch begins

switch (months)

{

case 1:

//Set the monthName to January

monthName = "January";

//Set the days to 31

days = 31;

break;

case 2:

//Set the monthName to February

monthName = "February";

//Checks for leap year

if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))

{

//Set the days to 29

days = 29;

}

//Otherwise if not leap year

else

{

//Set the days to 28

days = 28;

}

break;

case 3:

//Set the monthName to March

monthName = "March";

//Set the days to 31

days = 31;

break;

  

case 4:

//Set the monthName to April

monthName = "April";

//Set the days to 30

days = 30;

break;

  

case 5:

//Set the monthName to May

monthName = "May";

//Set the days to 31

days = 31;

break;

  

case 6:

//Set the monthName to June

monthName = "June";

//Set the days to 30

days = 30;

break;

  

case 7:

//Set the monthName to July

monthName = "July";

//Set the days to 31

days = 31;

break;

  

case 8:

//Set the monthName to August

monthName = "August";

//Set the days to 31

days = 31;

break;

  

case 9:

//Set the monthName to September

monthName = "September";

//Set the days to 30

days = 30;

break;

  

case 10:

//Set the monthName to October

monthName = "October";

//Set the days to 31

days = 31;

break;

  

case 11:

//Set the monthName to November

monthName = "November";

//Set the days to 30

days = 30;

break;

//Set the monthName to December

case 12: monthName = "December";

//Set the days to 31

days = 31;

break;

default: System.out.print("Error: this month does not exist"); System.exit(0);

break;

}//End of switch - case

//Displays the month name

System.out.println(" " + monthName + " " + year);

System.out.println("-----------------------------------");

System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

int c = 0;

int firstDay = 0;

//Switch begins

switch(months)

{

//Sets the firstDay as per the day number entered by the user

case 1:

firstDay = userFirstDay;

break;

case 2:

firstDay = userFirstDay + 3;

break;

case 3:

firstDay = userFirstDay + 3;

break;

case 4:

firstDay = userFirstDay + 6;

break;

case 5:

firstDay = userFirstDay + 8;

break;

case 6:

firstDay = userFirstDay + 11;

break;

case 7:

firstDay = userFirstDay + 13;

break;

case 8:

firstDay = userFirstDay + 16;

break;

case 9:

firstDay = userFirstDay + 19;

break;

case 10:

firstDay = userFirstDay + 21;

break;

case 11:

firstDay = userFirstDay + 24;

break;

case 12:

firstDay = userFirstDay + 26;

break;

}//End of switch - case

//Checks for leap year

if(((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))

{

//Switch case begins

switch(months)

{

//Sets the firstDay as per the day number entered by the user

case 1:

firstDay = userFirstDay;

break;

case 2:

firstDay = userFirstDay + 3;

break;

case 3:

firstDay = userFirstDay + 4;

break;

case 4:

firstDay = userFirstDay + 7;

break;

case 5:

firstDay = userFirstDay + 9;

break;

case 6:

firstDay = userFirstDay + 12;

break;

case 7:

firstDay = userFirstDay + 14;

break;

case 8:

firstDay = userFirstDay + 17;

break;

case 9:

firstDay = userFirstDay + 20;

break;

case 10:

firstDay = userFirstDay + 22;

break;

case 11:

firstDay = userFirstDay + 25;

break;

case 12:

firstDay = userFirstDay + 27;

break;

}//End of switch - case

}//End of if

  

int dayOfWeek = 0;

//Sets the day of the week

if ( (firstDay % 7 ) >= 0 )

{

if ( (firstDay % 7 ) == 0 )

{

dayOfWeek = 0;

}

else if ( (firstDay % 7 ) == 1 )

{

dayOfWeek = 1;

System.out.print(" " );

}

else if ( (firstDay % 7 ) == 2 )

{

dayOfWeek = 2;

System.out.print(" " );

}

else if ( (firstDay % 7 ) == 3 )

{

dayOfWeek = 3;

System.out.print(" " );

}

else if ( (firstDay % 7 ) == 4 )

{

dayOfWeek = 4;

System.out.print(" " );

}

else if ( (firstDay % 7 ) == 5 )

{

dayOfWeek = 5;

System.out.print(" " );

}

else if ( (firstDay % 7 ) == 6 )

{

dayOfWeek = 6;

System.out.print(" " );

}

}//End of outer if

//Loops from 1 to days

for ( c = 1; c <= days; c++ )

{

if (c < 10)

System.out.print(" " + c );

else

System.out.print(" " + c );

if ((c + firstDay ) % 7 == 0 )

System.out.println();

}//End of for loop

System.out.println();

}//End of for loop

}//End of main method

}//End of class

Sample Run:

Enter the year: 2017

Enter 1st day of year ( 0 = Sunday, 6 = Satuday ): 0

January 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

February 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28

March 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30 31

April 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30

May 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5 6

7 8 9 10 11 12 13

14 15 16 17 18 19 20

21 22 23 24 25 26 27

28 29 30 31

June 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3

4 5 6 7 8 9 10

11 12 13 14 15 16 17

18 19 20 21 22 23 24

25 26 27 28 29 30

July 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1

2 3 4 5 6 7 8

9 10 11 12 13 14 15

16 17 18 19 20 21 22

23 24 25 26 27 28 29

30 31

August 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30 31

September 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

October 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4 5 6 7

8 9 10 11 12 13 14

15 16 17 18 19 20 21

22 23 24 25 26 27 28

29 30 31

November 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2 3 4

5 6 7 8 9 10 11

12 13 14 15 16 17 18

19 20 21 22 23 24 25

26 27 28 29 30

December 2017

-----------------------------------

Sun Mon Tue Wed Thu Fri Sat

1 2

3 4 5 6 7 8 9

10 11 12 13 14 15 16

17 18 19 20 21 22 23

24 25 26 27 28 29 30

31

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote