brand new to java so im struggling a bit in this intro to java class. Doing my best to make this program work but it is just not happening and im not sure why. Right now running into an error where it is telling me it cannot find symbol?
Ive spent HOURS on this and i feel like im getting closer. Your help is much appreciated.
Here is the assignment:
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) / 12x = y0 + y0/4 - y0/100 + y0/400m0 = m + 12 * ((14 - m) / 12) - 2d0 = (d + x + (31*m0)/ 12) mod 7
For example, on what day of the week was August 2, 1953?
y = 1953 - 0 = 1953x = 1953 + 1953/4 - 1953/100 + 1953/400 = 2426m = 8 + 12*0 - 2 = 6d = (2 + 2426 + (31*6) / 12) mod 7 = 2443 mod 7 = 0 (Sunday)
and the output needs to look exactly like this:
8 2 1953 falls on 0.
And here is my code so far:
public class DayOfWeekTest{public static void main(String[] args){int monrh, day, year;month = Integer.parseInt(args[0]);day = Integer.parseInt(args[1]);year = Integer.parseInt(args[2]);int y0 = year - (14 - month) / 12;System.out.println(y0);int x = y0 + y0/4 - y0/100 + y0/400;System.out.println(x);int m0 = month + 12 * ((14 - month) / 12) - 2;System.out.println(m0);int d0 = (day + x + (31 * m0)/ 12) % 7;System.out.println(d0);System.out.println("Falls on a " + d0);}}
Any help would be greatly appreciated, If you wouldnt mind kind of explaining what im doing wrong that would be even better. I really want to learn this stuff. thanks so much guys.
Best Answer
Your code does exactly what it should!
instead of printing y0
, x
and m0
, you should instead print year
, month
and day
. That will give you the right output. Your code should look something along the lines of this:
int y0 = year - (14 - month) / 12;int x = y0 + y0/4 - y0/100 + y0/400;int m0 = month + 12 * ((14 - month) / 12) - 2;int d0 = (day + x + (31 * m0)/ 12) % 7;System.out.print( month + " " + day + " " + year + " falls on " + d0 );