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

LISTING 6.12 **13.4 (Display calendars) Rewrite the PrintCalendar class in Listi

ID: 3592882 • Letter: L

Question

LISTING 6.12

**13.4 (Display calendars) Rewrite the PrintCalendar class in Listing 6.12 to display a calendar for a specified month using the Calendar and GregorianCalendar classes. Your program receives the month and year from the command line. For example java Exercisel3_04 5 2016 This displays the calendar shown in Figure 13.9. ea Command Prompt -Oxl e:exercise»java Exercise13_045 2016 May 2016 Sun Hon Tue Med Thu Fri Sat 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 1617 18 1920 21 22 2324 25 26 27 28 29 30 31 c: [exercise FIGURE 13.9 The program displays a calendar for May 2016. 530 Chapter 13 Abstract Classes and Interfaces You also can run the program without the year. In this case, the year is the current year. If you run the program without specifying a month and a year, the month is the current month

Explanation / Answer

import java.util.Scanner; 2 3 public class PrintCalendar { 4 /** Main method */ 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 8 // Prompt the user to enter year 9 System.out.print("Enter full year (e.g., 2012): "); 10 int year = input.nextInt(); 11 12 // Prompt the user to enter month 13 System.out.print("Enter month as a number between 1 and 12: "); 14 int month = input.nextInt(); 15 16 // Print calendar for the month of the year 17 printMonth(year, month); 18 } 19 20 /** Print the calendar for a month in a year */ 21 public static void printMonth(int year, int month) { 22 // Print the headings of the calendar 23 printMonthTitle(year, month); 24 25 // Print the body of the calendar 26 printMonthBody(year, month); 27 } 28 29 /** Print the month title, e.g., March 2012 */ 30 public static void printMonthTitle(int year, int month) { 31 System.out.println(" " + getMonthName(month) 32 + " " + year); 33 System.out.println("-----------------------------"); 34 System.out.println(" Sun Mon Tue Wed Thu Fri Sat"); 35 } 36 37 /** Get the English name for the month */ 38 public static String getMonthName(int month) { 39 String monthName = ""; 40 switch (month) { 41 case 1: monthName = "January"; break; 42 case 2: monthName = "February"; break; 43 case 3: monthName = "March"; break; 44 case 4: monthName = "April"; break; 45 case 5: monthName = "May"; break; 46 case 6: monthName = "June"; break; 47 case 7: monthName = "July"; break; 48 case 8: monthName = "August"; break; 49 case 9: monthName = "September"; break; 50 case 10: monthName = "October"; break; 51 case 11: monthName = "November"; break; 52 case 12: monthName = "December"; 53 } 54 55 return monthName; 56 } 57 58 /** Print month body */ 59 public static void printMonthBody(int year, int month) { 60 // Get start day of the week for the first date in the month 61 int startDay = getStartDay(year, month) 62 63 // Get number of days in the month 64 int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month); 65 66 // Pad space before the first day of the month 67 int i = 0; 68 for (i = 0; i