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

Work programming exercise 13.4, page 529, Display Calendar. Please follow the sp

ID: 3586304 • Letter: W

Question

Work programming exercise 13.4, page 529, Display Calendar. Please follow the specifications given in the problem statement. You do not need to use the command prompt when running the program. Have your test program ask the user to enter for the month and year values. Notice the first three lines of the out are just strings. After that, it is a matter for extracting the day value and displaying it as shown using tabs and new lines. Notice every 7 values you need to move to a new line.

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 Exercise13_04 5 2016
This displays the calendar shown in Figure 13.9

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

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.

Explanation / Answer

import java.util.Scanner;

import java.util.Calendar;

import java.util.GregorianCalendar;

public class myCalendar{

final static int CalendarStart=3;

static String[] months={"January","February","March","April","May","June","July","August","September","October","November","December"};

public static void main(String[] args){

Calendar c=Calendar.getInstance();

int year=c.get(Calendar.YEAR);

int month=c.get(Calendar.MONTH);

Scanner input = new Scanner(System.in);

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

year=input.nextInt();

System.out.print("Enter the month(1-12): ");

month=input.nextInt();

displayCalendar(year,month);

}

public static void displayCalendar(int year, int month) {

System.out.println(" " + months[month-1]+ " " + year);

System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

int totalNumberOfDays = daysInYear(year, month);

int startDay=(totalNumberOfDays + CalendarStart) % 7;

int numberOfDaysInMonth = daysInMonth(year, month);

for(int i=0;i<startDay;i++)

System.out.print(" ");

for(int i=1;i<=numberOfDaysInMonth; i++) {

System.out.printf("%4d", i);

if ((i + startDay)%7==0)

System.out.println();

}

System.out.println();

}

public static int daysInMonth(int year, int month) {

boolean leapyear=year%400==0 || (year%4==0 && year%100!=0);

if((month<=7 && month%2!=0) || (month>7 && month%2==0))

return 31;

else if(month!=2)

return 30;

else return leapyear?29:28;

}

public static int daysInYear(int year, int month) {

int count=0;

boolean leapyear=year%400==0 || (year%4==0 && year%100!=0);

for (int i=1800;i<year;i++)

if(leapyear)

count=count+366;

else

count=count+365;

for(int i=1;i<month;i++)

count=count+daysInMonth(year, i);

return count;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote