0

I'm trying to solve this very basic question that involves some calculation. But my calculation is getting different result. (It returns 6, using the input given in the example, it should return 1 (sunday).

Here's the question:

enter image description here

This is what I am trying:

Scanner sc = new Scanner(System.in);

System.out.println("Enter year: (e.g., 2012): ");
int year = sc.nextInt();

System.out.println("Enter month: 1-12: ");
int month = sc.nextInt();

System.out.println("Enter the day of the month: 1-31: ");
int day = sc.nextInt();

//calculate the day using the forumla
int k = year % 100;
int j = year / 100;

int weekDay = (int)((day + ( 26 * (month + 1))/10.0 + k + k/4.0 + j/4.0 + (5*j)) % 7);
3
  • 2
    why do you work with floating point numbers if the description of the algorithm explicitly states that integer divisions have to be performed? Commented Apr 19, 2020 at 10:21
  • 1
    You're using integers which leads to integer division Commented Apr 19, 2020 at 10:21
  • @Ronald originally i tried using the integer but because I was not getting the correct result so I thought maybe it was because of it, that's why I tried this. Commented Apr 19, 2020 at 10:27

3 Answers 3

4

If you look up Zeller's congruence, e.g. on Wikipedia, you will see that you're supposed to do the math with integers, because those division results are supposed to be floor'ed.

However that is not the problem causing your issue. The problem is that you didn't read the description of m in full. As Wikipedia says it:

NOTE: In this algorithm January and February are counted as months 13 and 14 of the previous year. E.g. if it is 2 February 2010, the algorithm counts the date as the second day of the fourteenth month of 2009 (02/14/2009 in DD/MM/YYYY format)

The bolded part is exactly what your assignment says too.

Sign up to request clarification or add additional context in comments.

4 Comments

The floating point divisions are part of the problem, but you are right that this was not the full story.
@Cerenia Changing to int math will not change the result for this test case, so I stand by my answer: It is not the problem here. It is a problem, which is why I mentioned it, but it's not the main problem.
It working for the test case does not imply that it will work for every case.
@Cerenia Correct, and I haven't said otherwise anywhere, but I've now tried to clarify that, since you apparently thought I did.
1

m is the month (3:March, 4: April, .... 12: December). January and February are counted as months 13 and 14 of the previous year

Therefor you need to add some check if the inputed month is Jan. or Feb.

Scanner sc = new Scanner(System.in);

System.out.println("Enter year: (e.g., 2012): ");
int year = sc.nextInt();

System.out.println("Enter month: 1-12: ");
int month = sc.nextInt();
if(month < 3){
    month += 12;
    year -= 1; 
}

System.out.println("Enter the day of the month: 1-31: ");
int day = sc.nextInt();

//calculate the day using the forumla
int k = year % 100;
int j = year / 100;
int weekDay =  (day + (26 * (month + 1)) / 10 + k + k / 4 + j / 4 + (5 * j)) % 7;
System.out.println(weekDay);

Comments

0

As Ronald pointed out in the comments writing numbers like 4.0 leads to a floating point conversion. Write 4 instead (without the decimal point). Adding an int conversion to the whole formula will only convert back to int at the end when everything has been calculated, but you want integer divisions throughout the whole calculation for this formula to work.

Also Andreas rightly pointed out that you didn't read the whole description of the algorithm carefully.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.