Question: A program named MonthCalendars.java can be found in the end of this file. This program has a menu from which the user can select a

A program named MonthCalendars.java can be found in the end of this file. This program has a menu from which the user can select a few operations related to month calendars. The program contains a class named EnglishCalendar, which is inherited by another class called SpanishCalendar. Class EnglishCalendar contains, among other things, a method named print()that prints the calendar of the month that is specified in data members. The print()method, that may look quite complicated, is inherited by the lower classes and does not need to be modified in these exercises.

a) By typing letter n the user can now print the calendar of the next month. By typing p the user can print the current calendar. Modify the program so that, instead of current calendar, the selection p prints the calendar of the previous month[ new method named decrement_calendar_month() to class EnglishCalendar. This can be done quite easily by first making a copy of the increment_calendar_month() method.]

b) Add a new menu item so that by typing y the user can see the calendar of the first month of the next year

MonthCalendars.java

import java.time.* ; // for classes LocalDate, Period import java.time.temporal.WeekFields ; import java.util.Scanner ; class EnglishCalendar { protected int this_year ; protected int this_month ; protected String[] names_of_months ; protected String week_description ; public EnglishCalendar() { } public EnglishCalendar( int given_year, int given_month ) { String[] english_names_of_months = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" } ; String english_week_description = " Week Mon Tue Wed Thu Fri Sat Sun" ; names_of_months = english_names_of_months ; week_description = english_week_description ; this_year = given_year ; this_month = given_month ; } public int get_calendar_year() { return this_year ; } public int get_calendar_month() { return this_month ; } public void increment_calendar_month() { this_month ++ ; if ( this_month > 12 ) { this_month = 1 ; this_year ++ ; } } public void print() { LocalDate a_day_in_this_month = LocalDate.of( this_year, this_month, 1 ) ; // Days of week are numbered from 1 to 7. // The first day of week is Monday. int day_of_week_value = 1 ; int day_of_week_of_first_day = a_day_in_this_month.getDayOfWeek().getValue() ; System.out.print( " " + names_of_months[ this_month - 1 ] + " " + this_year + " " + week_description + " " ); System.out.printf( "%4d ", a_day_in_this_month.get( WeekFields.ISO.weekOfWeekBasedYear() ) ) ; // The first week of a month is often an incomplete week, // i.e., the first part of week belongs to the previous // month. In place of the days that belong to the previous // month we print just spaces. while ( day_of_week_value != day_of_week_of_first_day ) { System.out.print( " " ) ; day_of_week_value ++ ; } while ( this_month == a_day_in_this_month.getMonthValue() ) { if ( day_of_week_value >= 8 ) { System.out.printf( " %4d ", a_day_in_this_month.get( WeekFields.ISO.weekOfWeekBasedYear() ) ) ; day_of_week_value = 1 ; } System.out.printf( "%5d", a_day_in_this_month.getDayOfMonth() ) ; a_day_in_this_month = a_day_in_this_month.plusDays( 1 ) ; day_of_week_value ++ ; } System.out.print( " " ) ; } }class SpanishCalendar extends EnglishCalendar { public SpanishCalendar( int given_year, int given_month ) { String[] spanish_names_of_months = { "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre" } ; String spanish_week_description = "Semana Lun Mar Mie Jue Vie Sab Dom" ; names_of_months = spanish_names_of_months ; week_description = spanish_week_description ; this_year = given_year ; this_month = given_month ; } } class MonthCalendars { public static void main( String[] not_in_use ) { Scanner keyboard = new Scanner( System.in ) ; SpanishCalendar a_spanish_calendar = new SpanishCalendar( 2015, 11 ); a_spanish_calendar.print() ; EnglishCalendar calendar_to_print = new EnglishCalendar( 2015, 12 ) ; calendar_to_print.print() ; String user_selection = "????" ; System.out.print(" This program prints calendars. Please, select from" + " the following menu by typing in a letter. ") ; while ( user_selection.charAt( 0 ) != 'e' ) { System.out.print(" p Print current calendar." + " n Print next calendar." + " s Switch to Spanish calendars." + " e Exit the program. " ) ; user_selection = keyboard.nextLine() ; if ( user_selection.charAt( 0 ) == 'p' ) { calendar_to_print.print() ; } else if ( user_selection.charAt( 0 ) == 'n' ) { calendar_to_print.increment_calendar_month() ;calendar_to_print.print() ; } else if ( user_selection.charAt( 0 ) == 's' ) { // We'll take the calendar year and month from the old calendar // object and use them to create a SpanishCalendar object. calendar_to_print = new SpanishCalendar( calendar_to_print.get_calendar_year(),

calendar_to_print.get_calendar_month() ) ; calendar_to_print.print() ; } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!