Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int dayOfWeek(int month, int day, int year) {
- int n;
- n = day;
- n += 2*month;
- n += (3*(month+1)/5);
- n += year;
- n += year/4;
- n -= year/100;
- n += year/400;
- n += 2;
- return n%7;
- }
- public static void testDayOfWeek() {
- System.out.print("Testing dayOfWeek... ");
- // On 2/5/2006, the Steelers won Super Bowl XL on a Sunday!
- assert(dayOfWeek(2, 5, 2006) == 1);
- // On 6/15/1215, the Magna Carta was signed on a Monday!
- assert(dayOfWeek(6, 15, 1215) == 2);
- // On 3/11/1952, the author Douglas Adams was born on a Tuesday!
- assert(dayOfWeek(3, 11, 1952) == 3);
- // on 4/12/1961, Yuri Gagarin became the first man in space, on a Wednesday!
- assert(dayOfWeek(4, 12, 1961) == 4);
- // On 7/4/1776, the Declaration of Independence was signed on a Thursday!
- assert(dayOfWeek(7, 4, 1776) == 5);
- // on 1/2/1920, Isaac Asimov was born on a Friday!
- assert(dayOfWeek(1, 2, 1920) == 6);
- // on 10/11/1975, Saturday Night Live debuted on a Saturday (of course)!
- assert(dayOfWeek(10, 11, 1975) == 7);
- System.out.println("Passed all tests!");
- }
Add Comment
Please, Sign In to add comment