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

Create a Java source code program that accepts a value for a month (e.g., 2 for

ID: 3535807 • Letter: C

Question

Create a Java source code program that accepts a value for a

month (e.g., 2 for February) and a year (2009) and

display the monthly calendar for the month of the year. The

program also repeats until a user enters

“n†or

“N†as

shown in the following example.

C:Code> java Calendar

What is the year? 2009

What is the month? 2

Su Mo Tu We Th Fr Sa

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

Enter y to continue or n to quit: y

What is the year? 2012

What is the month? 7

Su Mo Tu We Th Fr Sa

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

Enter y to continue or n to quit: n

C:Code>


Use a method with the two parameters (e.g., month and year) in

the same program which includes the main method.




Explanation / Answer

Try this....



import java.util.*;

public class Four29

{

public static void main(String[] args)


{


Scanner in = new Scanner(System.in);



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


int yr = in.nextInt();



System.out.print(" Enter first day, between 0 to 6, 0 for Sunday and so on..: ");

int day = in.nextInt();



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 && yr0 != 0) || yr@0 == 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(" " + 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("= ", p);


day+=1;


}


day%=7;



System.out.print(" ");


}


} // end of main method


} // end of class