Write a program to accept the year, month and the weekday name of the 1st day of that month and generate its calendar.
import java.util.*;
class Calendar {
String mnName, startDay;
int y;
int[] DAY_COUNT = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
String[] MONTH_NAME = {
"January", "February", "March", "April", "May", "June", "July", "August", "September", "October",
"November", "December"
};
String[] DAY_NAME = {
"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
};
int[][] Cal = new int[5][7];
Calendar(int year, String month, String day) {
y = year;
mnName = month.toLowerCase();
startDay = day.toLowerCase();
}
private int indexOf(String[] S, String v) {
for (int x = 0; x < S.length; x++)
if (v.equalsIgnoreCase(S[x]))
return x;
return -1;
}
void makeCalendar() {
int d = 0, start = 0;
int mnIndex = indexOf(MONTH_NAME, mnName);
if (y % 4 == 0 && mnIndex == 1)
d = 29;
else
d = DAY_COUNT[mnIndex];
start = indexOf(DAY_NAME, startDay);
mnName = MONTH_NAME[mnIndex];
for (int i = 0; i < d; i++) {
int rw = (i + start) / 7, cl = (i + start) % 7;
Cal[rw % 5][cl] = i + 1;
}
}
void display() {
System.out.println("\n\t----------------------------------------------------");
System.out.println("\t\t\t " + mnName + " " + y);
System.out.println("\t----------------------------------------------------");
System.out.println("\tSUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT");
System.out.println("\t----------------------------------------------------");
for (int i = 0; i < Cal.length; i++) {
System.out.print("\t");
for (int j = 0; j < Cal[0].length; j++) {
int vl = Cal[i][j];
System.out.printf("%s\t", vl != 0 ? vl : " ");
}
System.out.println();
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter year : ");
int y = sc.nextInt();
System.out.print("Enter month name : ");
String mName = sc.next();
System.out.printf("Enter the week day name of 1st day of %s : ", mName);
String wName = sc.next();
Calendar a = new Calendar(y, mName, wName);
a.makeCalendar();
a.display();
sc.close();
}
}
Name | Type | Uses |
---|---|---|
global | ||
mnName | String | to store the entered month name |
startDay | String | name of staring weekday |
y | int | to store entered year |
DAY_COUNT | int[] | this stores the number of days in month |
MONTH_NAME | Names of the 12 months | |
DAY_NAME | Names of weekdays | |
Cal | int[][] | This a 5 by 7 Matrix to store thr calendar |
int indexOf() | ||
S | String[] | This is the string array which is to be searched |
v | String | Value to search for in the array |
x | int | this is the index of array where element may be found |
void makeCalendar() | ||
d | int | to store the number of days of the current month |
start | int | to store the starting day of the week |
mnIndex | int | this stores the month index |
mnName | int | this stores the month name |
void display() | ||
i, j | int | counter variables to iterate over the calendar matrix |
void main() | ||
sc | Scanner | object to take user input |
y | int | to store entered year |
mName | String | to store entered month name |
wName | String | to store starting day of the week |
a | Calendar | object to call methods |