Design a class to print a monthly calendar given a month and a year. Use a dayTy
ID: 3805722 • Letter: D
Question
Design a class to print a monthly calendar given a month and a year. Use a dayType object to hold the first day of the month, use an extendedDateType object with the day set to 1 to store the month and year. Note that these objects are attributes of a calendar object and are hidden from a user of the calendar class. Use the following CRC specification to develop this class:
Class: calendarType Super Classes: Sub Classes: Description: Prints a calendar for a specified month and year Responsibilities: Collaborations: Initialize and set the month and year extendedDateType Initialize and set the first day of the month (For example, April 1, 2016 starts on a Friday). Assume that you can print a calendar for any month starting January 1, 1500 and note that January 1, 1500 is a Monday). dayType Print the calendar. Note that extendedDateType can provide the label for the calendar and the number of days in the month). Also dayType can provide the first day of the week for the current month and year. extendedDateType, dayTypeExplanation / Answer
import java.util.Scanner;
public class calendarType {
public static int day(int month, int day, int year) {
int y = year - (14 - month) / 12;
int x = y + y/4 - y/100 + y/400;
int m = month + 12 * ((14 - month) / 12) - 2;
int d = (day + x + (31*m)/12) % 7;
return d;
}
// return true if the given year is a leap year
public static boolean isLeapYear(int year) {
if ((year % 4 == 0) && (year % 100 != 0)) return true;
if (year % 400 == 0) return true;
return false;
}
public static void main(String[] args) {
int month,year;
Scanner sc=new Scanner(System.in);
System.out.println("Enter The Month(Plz Enter Numeric Value):");
month = sc.nextInt(); // month (Jan = 1, Dec = 12)
System.out.println("Enter The Year(Plz Enter Numeric Value):");
year = sc.nextInt(); // year
sc.close();
// months[i] = name of month i
String[] months = {
"", // leave empty so that months[1] = "January"
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
// days[i] = number of days in month i
int[] days = {
0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// check for leap year
if (month == 2 && isLeapYear(year)) days[month] = 29;
// print calendar header
System.out.println(" " + months[month] + " " + year);
System.out.println(" S M Tu W Th F S");
// starting day
int d = day(month, 1, year);
// print the calendar
for (int i = 0; i < d; i++)
System.out.print(" ");
for (int i = 1; i <= days[month]; i++) {
System.out.printf("%2d ", i);
if (((i + d) % 7 == 0) || (i == days[month])) System.out.println();
}
}//end of main method
}//end of class calendarType
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.