r/programmingbydoing • • Sep 16 '13

#103 Weekday Calculator

The formula in the instructions is not correct if you pass the year 2000 if I am not mistaken. You must include the century variable if the year is not 19xx.

3 Upvotes

8 comments sorted by

View all comments

Show parent comment

2

u/kopytkopytko Apr 25 '14 edited Apr 26 '14

Hey, I think I need advice because I cant figure it out by myself. What's wrong with my code? It doesn't show the right weekday (automatic test is just one right and I think its just coincidence). BTW Thanks for the Java tutorial!

@edit Here's code: http://pastebin.com/vzJkZGuv

2

u/holyteach Apr 25 '14

(Next time, use pastebin for code this long. It's harder to read as a reddit comment.)

Okay, you have three problems, and two of them make your output wrong.

  1. The main problem is that your weekday_name() function isn't correct. You're supposed to make day 1 "Sunday" and day 2 "Monday", etc. Make sure to make both day 0 and day 7 return "Saturday". Fixing this should make all the test cases right except one.
  2. Because && happens before ||, you need to rewrite your if statement like so:

    if (is_leap(yyyy)==true && (mm==1 || mm==2))

The way you have it, it will subtract one when it's January and a leap year or when it's February (of any year).

3) You don't have to call functions twice. Just call them right inline where you're using the return value:

// is_leap(yyyy); <-- this isn't needed
if (is_leap(yyyy)==true && mm==1 || mm==2)

and

// weekday_name(remainder);  <-- this isn't needed
date = weekday_name(remainder) +", " + month_name(mm) +" "+dd+", " + yyyy;

1

u/kopytkopytko Apr 26 '14

I still don't know how I didnt notice that if statement is wrong. Everything works now. Pretty basics mistakes. Thanks!