10. Using the classes extDateType (Programming Exercise 9) and dayType (Chapter
ID: 3572632 • Letter: 1
Question
10. Using the classes extDateType (Programming Exercise 9) and dayType
(Chapter 12, Programming Exercise 2), design the class calendarType so
that, given the month and the year, we can print the calendar for that month.
To print a monthly calendar, you must know the first day of the month and
the number of days in that month. Thus, you must store the first day of
the month, which is of the form dayType, and the month and the year of the
calendar. Clearly, the month and the year can be stored in an object of
the form extDateType by setting the day component of the date to 1 and
themonth and year as specified by the user. Thus, the class calendarType
has two member variables: an object of the type dayType and an object of the
type extDateType.
Design the class calendarType so that the program can print a calendar
for any month starting January 1, 1500. Note that the day for January 1 of the
year 1500 is a Monday. To calculate the first day of a month, you can add the
appropriate days to Monday of January 1, 1500.
For the class calendarType, include the following operations:
a. Determine the first day of the month for which the calendar will be
printed. Call this operation firstDayOfMonth.
b. Set the month.
c. Set the year.
d. Return the month.
e. Return the year.
f. Print the calendar for the particular month.
g. Add the appropriate constructors to initialize the member variables.
Explanation / Answer
import java.util.*;
class Calender
{
static extDateType et = new extDateType();
static dayType dt =new dayType();
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();// Scanning Month from user
int year = sc.nextInt(); // Scanning Year from user
dt=firstDayOfMonth(month,year);
String[] months = {
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December"
};
int[] days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (month == 2 && ((year % 4 == 0) && (year % 100 != 0) || (year%400)==0)) days[1] = 29;
System.out.println(" S M T W T F S");
for (int i = 0; i < dt.d; i++)
System.out.print(" ");
for (int i = 1; i <= days[month-1]; i++) {
System.out.printf("%02d ", i);
if (((i + dt.d) % 7 == 0) || (i == days[month-1])) System.out.println();
}
}
static dayType firstDayOfMonth(int month,int year)
{
et.day=1;
et.month=month;
et.year=year;
return et.getDayType();
}
}
class extDateType {
int day;
int month;
int year;
public dayType getDayType() {
int y = this.year - (14 - this.month) / 12;
int x = y + y/4 - y/100 + y/400;
int m = this.month + 12 * ((14 - this.month) / 12) - 2;
int d = (this.day + x + (31*m)/12) % 7;
dayType dt=new dayType(d);
return dt;
}
}
class dayType{
String arr[]={"Mon","Tue","Wed","Thu","Fri","Sat","Sun"};
String day;
int d;
dayType(){}
dayType(int d){
this.d=d;
this.day=arr[d-1];
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.