Teaching myself Java.

• 11-25-2011, 10:47 PM
Jsin
Teaching myself Java.
Hello guys,

I'm teaching myself Java and I am at the end of chapter 3 in Introduction to Java Programming. 8th ed. by Y. Daniel Liang. I am working on one of the programming exercises and I am having an issue with this.

dayOfWeek = [day + [26*(month + 1)/10] + year + [year/4] + [century/4] + 5*century] * .07;

I don't think my use of parentheses and square brackets are correct, but I have played around with for a while and still don't know where my errors are.

Thanks for the help.
• 11-25-2011, 11:09 PM
Fubarable
Re: Teaching myself Java.
It all should be parenthesis and there should be no square brackets unless you're somehow dealing with arrays but not showing us (which I doubt). If the book is showing square brackets, consider getting another book.
• 11-25-2011, 11:16 PM
Jsin
Re: Teaching myself Java.
The book did show brackets but it was the actual formula and not code. I did try using all parenthesis but I wasn't getting the answer I needed. Maybe my problem is else where then. Thanks for the help.
• 11-25-2011, 11:18 PM
Fubarable
Re: Teaching myself Java.
Quote:

Originally Posted by Jsin
The book did show brackets but it was the actual formula and not code. I did try using all parenthesis but I wasn't getting the answer I needed. Maybe my problem is else where then. Thanks for the help.

• 11-25-2011, 11:27 PM
Jsin
Re: Teaching myself Java.
Ok here is my best attempt at the code.

dayOfWeek = (day + (26*(month + 1)/10) + year + (year/4) + (century/4) + 5*century) * .07;

The answer I get is 185.01000000000002
If I do it by hand I get 150.04

day:26
month:3
year:2002
• 11-26-2011, 12:05 AM
Jsin
Re: Teaching myself Java.
Ok, I may have read the pseudocode wrong after doing a little more reading I think this might be the right code...

dayOfWeek = (day + (26*(month + 1)/10) + year + (year/4) + (century/4) + 5*century) %7;

Notice the end I think the code is asking for a remainder not a percentage, but my answer is still not right. Now what I am getting is a 4 when I should be getting a 3. Can anybody tell me if I am getting closer to this?
• 11-26-2011, 12:08 AM
Fubarable
Re: Teaching myself Java.
You'll have to let us know what numbers you use and what are the rules and restrictions here.
• 11-26-2011, 12:20 AM
Jsin
Re: Teaching myself Java.
What I am trying to do is use the Zeller's congruence to determine the day of the week. After doing some more research I think the formula in my book might be wrong. The numbers I am using are

day:26
month:3
year:2002

and I am trying to come up with Tuesday as the answer. My main question is if I was using the parentheses and square brackets correctly.
• 11-26-2011, 08:54 PM
Jsin
Re: Teaching myself Java.
This is the working code.

dayOfWeek = (day + (13*(month + 1)/5) + (year%100) + ((year%100)/4) + (century/4) + 5*century) %7;

Thanks guys for the help.