# Zellers Congruence

• February 17th, 2013, 09:07 PM
malith
Zellers Congruence
Hi I am trying to solve Zellers Congruence and I am having a little trouble. Here's my instructions:

1) Allow the user to enter a month. For calculation purposes, January = 1, February = 2, ... December = 12.
2) Enter a day of the month(1..31) Error checking is not necessary at this point, but would be helpful.
3) Enter a year using 4 digits (e.g., 1776, 1994, 2001)
If the month entered is less than or equal to 2
Subtract 1 from the year entered

Otherwise
Subtract 2 from the month
Leave the year alone
5)Find the month correctionby
Multiplying the adjusted month by 26
Subtract 2 from the above
Integer divide the above by 10
6) Determine the century by integer dividing the adjusted year by 100
7)Determine the century remainder by finding the modulus 100 of the adjusted year
8)Determine the year correction by
Multiplying the century by five then
The century remainder
The century remainder integer divided by 4
The century integer divided by 4
9) Determine which weekday by
Adding the day, month correction, and year correction
Finding the above sum's modulus 7

If the solution to step 9 is '0', then the day is "Sunday". If it's '1', then the day is "Monday", ... if it's 6, then "Saturday".

So far i have this:

System.out.print("Enter the month (1-12): ");
int month = keyboard.nextInt();

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

System.out.println("Enter a year (e.g, 1776,2003,2005):");
int year1 = keyboard.nextInt();

if (month <= 2)
{
}

else
{
}
//step 5
monthCorrection = (((adjustedMonth * 26) - 2) % 10);

I am wondering if i am on the right track and if someone could help me guide the right way. I would appreciate it. So far i got up to step 6 of the instructions i was to do. Any help is great! Thank you!
• February 18th, 2013, 12:23 PM
Zaphod_b
Re: Zellers Congruence
Quote:

Originally Posted by malith
...
I am wondering if i am on the right track,,,

Here is a track that I recommend for all (yes, all) projects:

1. Make a complete program that you can compile and execute. Start with a public class definition and an empty main() method.

2. Implement the assignment a step at a time by adding code to main(). If other methods are required, implement and test each one.

3. Test the results of each step before going on to the next step. Maybe put print statements after each user input and after each calculation. Sometimes you may want to put print statements inside an "if{}" block or some loop or whatever. In particular, make absolutely sure that the program is getting what you expect for input data.

The main thing, as far as I am concerned is this:

Plan the entire program (figure out what classes, what variables and what methods you will be needing) but don't write any code just yet.

Make up a few test cases for which you know what values will be calculated and used by the program. Make sure you know what the right answers are for the test cases. (If there are no published test cases, follow the instructions "by hand" using pencil and paper for the calculations.)

Now, your assignment seems to define the steps completely and unambiguously, right? If there is some part you don't understand, figure it out (or ask whoever it was that made the assignment to clarify).

Then, and only then, you are ready to start coding.

So,,,

Implement and test each step before going to the next: Run your test cases through the program and make sure that the results of what you have, so far, are valid.

Here are some of the steps (that you have already implemented), with some print statements to make sure you know what the program is seeing and what it is doing at each step:

Code java:

```// Import whatever packages you need   // Declare a class // public class Whatever   //Create a main() method // public static void main(String [] args) { // // Put whatever variable declarations/definitions you need. // For example, define keyboard and assign an appropriate value. //   System.out.print("Enter the month (1-12) : "); int month = keyboard.nextInt(); System.out.println("You entered " + month);   System.out.print("Enter the day of the month (1-31) : "); int day1 = keyboard.nextInt(); System.out.println("You entered " + day1);   System.out.print("Enter the year (e.g, 1776, 2013) : "); int year1 = keyboard.nextInt(); System.out.println("You entered " + year1);     int adjustedMonth, adjustedYear, century, monthCorrection; if (month <= 2) { adjustedMonth = month += 10; adjustedYear=year1 -=1; }   else { adjustedMonth = month-=2; adjustedYear = year1; } System.out.println("\nadjustedMonth = " + adjustedMonth + ", adjustedYear = " + adjustedYear);   // // If either of the two previous values is not what you expect, look // carefully at the code. If you can't see what the problem is, go back // and put print statements inside the if{} block and inside the else{} block.   monthCorrection = (((adjustedMonth * 26) - 2) % 10); // For debugging: print monthCorrection   century = adjustedYear % 100; // For debugging: print century // // etc. // } // End of main() } // End of class definition```

A run:
Code :

```Enter the month (1-12) : 1 You entered 1 Enter the day of the month (1-31) : 28 You entered 28 Enter the year (e.g, 1776, 2013) : 2013 You entered 2013   adjustedMonth = 11, adjustedYear = 2012```

So, now I will ask you: Is this correct so far? If it is, then you are not only on the right track, you are headed in the right direction!

After a few projects, maybe you won't see the need for stinkin' print statements at each and every stinkin' step, so maybe just verify user input and then print results. If the results don't agree with your expectations, go back and print some intermediate results to let you zero in on the problem area. (Or, maybe, adjust your expectations. Are you absolutely sure that you know the correct answer for the test case that failed?)

Or some such thing.

Cheers!

Z

Quote:

Originally Posted by Gold Hat
Print statements?
We ain't got no print statements.
We don't need no print statements!
I don't have to show you any stinkin' print statements!

---What Gold Hat (in the movie The Treasure of the Sierra Madre)
would have said if he were a Java programmer with no clue as to
how to debug a program, and if he were challenged by a forum
contributor to show some print statements to validate some of the
intermediate calculations.
• February 18th, 2013, 01:33 PM
Norm
Re: Zellers Congruence
Great post. It should be in the blog section where we can point links to for future OPs to consider.
• February 18th, 2013, 02:15 PM
malith
Re: Zellers Congruence
Thank you Zaphod! This actually helped a lot. Good tips and ways to approach a program for every person who is doing programming. I got it all completed with no errors. I will definitely use your tips to approach my upcoming programs and I agree with Norm. It should be in the blog section where we can point links to for future. Thanks again.