Question: 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
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 Exercise13_04 5 2016This displays the calendar shown in Figure 13.9.
The program displays a calendar for May 2016.
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.
Listing



0a. Command Prompt c:\exercise>java Exercise13_04 5 2016 May, 2016 Sun Mon Tue Wed Thu Fri Sat 1 2 3 4 5 6 7 10 11 12 13 14 19 20 21 15 16 17 18 23 24 27 28 25 26 29 30 31 C:\exercise> 1 import java.util.Scanner; 3 public class PrintCalendar { /** Main method */ public static void main(String[] args) { Scanner input = new Scanner(System.in); // Prompt the user to enter year System.out.print("Enter full year (e.g., 2012): "); int year = input.nextInt(); 11 // Prompt the user to enter month System.out.print("Enter month as a number between 1 and 12: "); int month = input.nextInt (); 12 13 14 15 // Print calendar for the month of the year printMonth(year, month); 16 17 18 19 /** Print the calendar for a month in a year */ public static void printMonth(int year, int month) { // Print the headings of the calendar printMonthTitle (year, month); 20 21 22 // Print the body of the calendar printMonthBody(year, month); 25 26 27 28 29 /** Print the month title, e.g., March 2012 */ public static void printMonthTitle(int year, int month) { System.out.println(" + "" + year); System.out.println("- System.out.println(" Sun Mon Tue Wed Thu Fri Sat"); 30 31 32 + getMonthName (month) 33 -"); 34 35 36 37 /** Get the English name for the month */ public static String getMonthName (int month) { String monthName - ""; switch (month) { case 1: monthName = 38 39 40 41 "January"; break; "February"; break; "March"; break; "April"; break; "May"; break; "June"; break; 42 case 2: monthName case 3: monthName - case 4: monthName case 5: monthName = case 6: monthName case 7: monthName = "July"; break; case 8: monthName case 9: monthName case 10: monthName = "October"; break; case 11: monthName = "November"; break; case 12: monthName = "December"; 43 44 45 46 47 48 "August"; break; "September"; break; 49 50 51 52 53 54 55 return monthName; 56 57 58 /** Print month body */ HNm S67 0090 1234 567 c0 9C m45 67 00 0 O12 m4Se 7 00 90 222 22~ 233 mmm m3m 3
Step by Step Solution
3.40 Rating (166 Votes )
There are 3 Steps involved in it
Program Plan 1 Create a class DisplayCalendars 2 Declare and Initialize the Calendar object with the ... View full answer
Get step-by-step solutions from verified subject matter experts
