-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalendar.java
More file actions
203 lines (192 loc) · 6.36 KB
/
Calendar.java
File metadata and controls
203 lines (192 loc) · 6.36 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* Calendar Lab (Template Class and Test Cases)
* This is the template class and test cases for the Calendar Lab.
* Written for the Oberoi International School, JVLR
* in Mumbai, Maharasthra, India.
*
* @author Aryan Arora
* @version 1.0
*/
public class Calendar {
public static void main(String[] args) {
// Tests for calculateDayOfWeek:
System.out.println(calculateDayOfWeek(9, 7, 1983)); // Output: 6
System.out.println(calculateDayOfWeek(15, 4, 2016)); // Output: 5
System.out.println(calculateDayOfWeek(30, 1, 2000)); // Output: 0
// Tests for getDayOfWeek:
System.out.println(getDayOfWeek(9, 7, 1983)); // Output: "Saturday"
System.out.println(getDayOfWeek(15, 4, 2016)); // Output: "Friday"
System.out.println(getDayOfWeek(30, 1, 2000)); // Output: "Sunday"
// Tests for printCalendar:
// Use the calendar feature of your computer to verify correct output.
printCalendar(7, 1983);
printCalendar(4, 2016);
printCalendar(1, 2000);
}
/**
*
* w = (d + ⌊2.6m-0.2⌋+y + ⌊0.25y⌋+⌊0.25c⌋-2c) mod 7
*
*/
/**
* Calculates the day of the week as a number between 0 and 6.
* (0=Sunday, 6=Saturday)
*
* @param day
* Precondition: day is a valid day of the given month.
* @param month
* Precondition: month is a valid month of the year.
* @param year
* Precondition: year is a valid year after the adoption of the Gregorian
* Calendar.
* @return A number corresponding to the day of the week for the given
* date.
* (0=Sunday, 6=Saturday)
*/
public static int calculateDayOfWeek(int day, int month, int year) {
// To be implemented in Activity #1, Exercise 1
if(month==1||month==2)
{
year-=1;//year = year -1
month+=10;
}
else
{
month-=2;
}
int c = year/100;
int y = mod(year,100);
int days = (int)(day + Math.floor(2.6*month-0.2)+y + Math.floor(0.25*y)+Math.floor(0.25*c)-2*c);
int DayOfWeek = mod(days ,7);
return DayOfWeek;
}
/**
* Returns the day of the week as a string.
*
* @param day
* Precondition: day is a valid day of the given month.
* @param month
* Precondition: month is a valid month of the year.
* @param year
* Precondition: year is a valid year after the adoption of the Gregorian
* Calendar.
* @return A string corresponding to the day of the week for the given
date.
*/
public static String getDayOfWeek(int day, int month, int year) {
// To be implemented in Activity #1, Exercise 2
int d = calculateDayOfWeek(day,month,year);
String days[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
return days[d];
}
/**
* Conditions Required to be a leap year
* Year Must be divisible by 4
* If a year is a century year eg. 1700, 1800, 1900, the year must be divisible by 400
*/
/**
* Determines whether or not a given year is a leap year.
*
* @param year
* Precondition: year is a valid year after the adoption of the Gregorian
* Calendar.
* @return true if the given year is a leap year, false otherwise.
*/
public static boolean isLeapYear(int year) {
// To be implemented in Activity #2, Exercise 1
if(mod(year,4)==0)// Checks if a year is divisible by 4
{
if(mod(year,100)==0)// Checks if year is
{
if(mod(year/100,4)==0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
/**
* Prints a visual calendar for the given month and year.
*
* @param month
* Precondition: month is a valid month is the year.
* @param year
* Precondition: year is a valid year after the adoption of the Gregorian
* Calendar.
*/
public static void printCalendar(int month, int year) {
// To be implemented in Activity #2, Exercise 2
String months[] = {"","January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"};
int D[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
System.out.println("\t"+months[month]+","+year);
System.out.println("Mo Tu We Th Fr Sa Su");
int day = calculateDayOfWeek(1,month,year);
boolean monthStart = false;
if(day==0)
day=7;
if(isLeapYear(year))
{
D[2]=29;
}
int ctr=1;
outer:
for(int i=0;i<6;i++)
{
for(int j=0;j<7;j++)
{
if(i==0&&!(monthStart))
{
if(j+1==day)
{
System.out.print("0"+Integer.toString(ctr));
monthStart=true;
ctr++;
}
else
{
System.out.print(" ");
}
}
else
{
if(ctr/10<1)
{
System.out.print("0"+Integer.toString(ctr));
}
else
{
System.out.print(Integer.toString(ctr));
}
ctr++;
}
System.out.print(" ");
if(ctr>=D[month]+1)
break outer;
}
System.out.println();
}
System.out.println();
}
/**
* A fix to the Java built in modulus operator (%) to behave more
* like mathematical modulo.
* @param n
* @param m
* @return returns n mod m as expected mathematically
*/
public static int mod(int n, int m) {
return (n % m + m) % m;
}
}