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

Page import java.util.Scanner; public class PrintCalendar /The main method has o

ID: 3739090 • Letter: P

Question

Page import java.util.Scanner; public class PrintCalendar /The main method has only one 1ine of code and it calls the process method You do not need to change anything in this method/ public static void main(String[] args) process 0: /This method ask the required info and calls the appropriate methods the calendar for the month or for the year. You car declare your will only be used here / based on the user's input to either print Scanner object in this method since it public static void process0 /Ithis method prints the body of the calender for the given month by calling the the methods printMonthTable and printMonthBody public static void printMonth (int year, int month) printMonthTitle (year, month) printMonthBody (year, month): //this method prints the title of the days in each week (sunday Mon Tues Wed Thur Fir Sat) /This method prints the following month nane year Sun Mon Tue Wed Thu Fri Sat this method gets the month as an integer and you need to call the appropriate method to get the name of the month*/ public static void printMonthTitle (int year, int month) /*this method calls the method getstartday to find out the firat day of the month (aunday, monday, tuesday, wedneaday, thursday or friday then it calls the nethod print by passing the startday, year and cBook Pro

Explanation / Answer


//Code to copy
//PrintCalendar.java
import java.util.Calendar;
import java.util.Scanner;
public class PrintCalendar {

   /** Main method */
   public static void main(String[] args) {
       //calling process method
       process();
   }

   /*Method that prompts user to enter the choice
   * and then prompt for month or year and then
   * print the calendar on console.*/
   private static void process() {
       // Prompt the user to enter year
       Scanner scanner = new Scanner(System.in);
       int choice;
       // Prompt the user to enter year
       do
       {
           System.out.println("****Menu Choice****");
           System.out.println("1.Month");
           System.out.println("2.Year");
           System.out.println("Enter your choice ?");
           choice=Integer.parseInt(scanner.nextLine());
          
           if(choice<1 || choice>2)
               System.out.println("****Invalid Choice****");
          
       }while(choice<1 || choice>2);

       if(choice==1)
       {
           System.out.println("Enter month number : ");
           int month=Integer.parseInt(scanner.nextLine());
           printMonth(month);
       }
       else
       {         
           System.out.println("Enter year : ");
           int year=Integer.parseInt(scanner.nextLine());
           printYear(year);
       }

   }

   /** Print the calendar for a month in a year */
   static void printMonth(int month) {      
       int currentYear = Calendar.getInstance().get(Calendar.YEAR);
       // Print the headings of the calendar
       printMonthTitle(currentYear, month);
       // Print the body of the calendar
       printMonthBody(currentYear, month);
   }

   /** Print the calendar for a month in a year */
   static void printYear(int year) {
       for (int month = 1; month <=12; month++) {
           // Print the headings of the calendar
           printMonthTitle(year, month);
           // Print the body of the calendar
           printMonthBody(year, month);
       }
   }

   /** Print the month title */
   static void printMonthTitle(int year, int month) {
       System.out.println(" " + getMonthName(month)
               + " " + year);
       System.out.println("-----------------------------");
       System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
   }

   /** Get the English name for the month */
   static String getMonthName(int month) {
       String monthName = null;
       switch (month) {
       case 1: monthName = "January"; break;
       case 2: monthName = "February"; break;
       case 3: monthName = "March"; break;
       case 4: monthName = "April"; break;
       case 5: monthName = "May"; break;
       case 6: monthName = "June"; break;
       case 7: monthName = "July"; break;
       case 8: monthName = "August"; break;
       case 9: monthName = "September"; break;
       case 10: monthName = "October"; break;
       case 11: monthName = "November"; break;
       case 12: monthName = "December";
       }
       return monthName;
   }

   /** Print month body */
   static void printMonthBody(int year, int month) {

       // Get start day of the week for the first date in the month
       int startDay = getStartDay(year, month);
       // Get number of days in the month
       int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
       // Pad space before the first day of the month
       int i = 0;
       for (i = 0; i < startDay; i++)
           System.out.print(" ");
       for (i = 1; i <= numberOfDaysInMonth; i++) {
           if (i < 10)
               System.out.print(" " + i);
           else
               System.out.print(" " + i);
           if ((i + startDay) % 7 == 0)
               System.out.println();
       }
       System.out.println();
   }

   /** Get the start day of the first day in a month */
   static int getStartDay(int year, int month) {
       // Get total number of days since 1/1/1800
       int startDay1800 = 3;
       int totalNumberOfDays = getTotalNumberOfDays(year, month);
       // Return the start day
       return (totalNumberOfDays + startDay1800) % 7;
   }

   /** Get the total number of days since January 1, 1800 */
   static int getTotalNumberOfDays(int year, int month) {
       int total = 0;
       // Get the total days from 1800 to year - 1
       for (int i = 1800; i < year; i++)
           if (isLeapYear(i))
               total = total + 366;
           else
               total = total + 365;
       // Add days from Jan to the month prior to the calendar month
       for (int i = 1; i < month; i++)
           total = total + getNumberOfDaysInMonth(year, i);
       return total;
   }
   /** Get the number of days in a month */
   static int getNumberOfDaysInMonth(int year, int month) {
       if (month == 1 || month == 3 || month == 5 || month == 7 ||
               month == 8 || month == 10 || month == 12)
           return 31;
       if (month == 4 || month == 6 || month == 9 || month == 11)
           return 30;
       if (month == 2) return isLeapYear(year) ? 29 : 28;
       return 0; // If month is incorrect
   }

   /*Determine if it is a leap year */
   static boolean isLeapYear(int year) {
       return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
   }
}

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

Sample Ouptut:

****Menu Choice****
1.Month
2.Year
Enter your choice ?
1
Enter month number :
3
March 2018
-----------------------------
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

Sample Run2:

****Menu Choice****
1.Month
2.Year
Enter your choice ?
2
Enter year :
2018
January 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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 2018
-----------------------------
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