Problem taken from Introduction to Programming in Java\" by Robert Sedgewick and
ID: 3742361 • Letter: P
Question
Problem taken from Introduction to Programming in Java" by Robert Sedgewick and Kevin Wayne. Write a program DayOfWeek.java that takes a date as input from the command line arguments and prints the day of the week that date falls on. Your program should take three command-line arguments: m (month), d (day), and y (year). For m use 1 for January, 2 for February, and so forth. For output print 0 for Sunday, 1 for Monday, 2 for Tuesday, and so forth. Use the following formulas, for the Gregorian calendar: y0 . y- (14-m) / 12 x = y0 + y0/4 -y0 / 100 + y0/400 mo-m 12(1 m) 12)2 do = (d + x + (1.no ) / 12) nod 7 For example, on what day of the week was August 2, 1953? y 1953 1953 19531953/4-1953/1001953/400 2426 m-812*0 26 d(22426 (316 12) mod 7 2443 mod 7 0Sunday) Compiling and Running You can type the following commands to compile and run your program from a terminal or select build/compiler from your IDE (e.g: Intellij IDEA) javac DayofWeek.java To run the the program, select run and set the command line arguments (e.g: 8 2 1953) as mentioned in class. If you use DOS cmd or terminal, you can type a command similar to the following java Dayofweek 8 2 1953 Output format The program output should be printed exactly as the following to recieve full credits: 8 2 1953 falls on 0.Explanation / Answer
public class DayOfWeek { public static void main(String[] args) { if(args.length > 2) { int m = Integer.parseInt(args[0]); int d = Integer.parseInt(args[1]); int y = Integer.parseInt(args[2]); int y0 = y - (14-m)/12; int x = y0 + y0/4 - y0/100 + y0/400; int m0 = m + 12*((14-m)/12) - 2; int d0 = (d + x + (31*m0)/12) % 7; System.out.printf("%d %d %d falls on %d. ", m, d, y, d0); } } }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.