• Post Reply Bookmark Topic Watch Topic
  • New Topic

getDayOfTtheWeek (without java.util cal) tips?  RSS feed

 
kurtis foody
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

i have an assignment to create a method to retrieve the proper day of the week for any given date. without using the java calendar,
after countless hours of trying to implement the algorithm in the outline ive finally finished it and it appears to work! i am using blue j by the way,
only thing is i feel like its really sloppy, but maybe im just being paranoid. any tips on how to clean it up or remove any dead code you see?

much appreciated.

heres the algorithm i had to implement

Step 1: Only look at the last two digits of the year and determine how many 12s fit in it

Step 2: Look at the remainder of this division:

Step 3: How many 4s fit into that remainder:

Step 4: Add the day of the month:


Step 5: Add the month code:

Jan = 1

Feb = 4

Mar = 4

Apr = 0

May = 2

Jun = 5

Jul = 0

Aug = 3

Sep = 6

Oct = 1

Nov = 4

Dec = 6

Step 6: Add your numbers, then mod by 7:


here is my code



 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You had the code tags in the wrong place, but don't worry; I have sorted that out. I also corrected your comments (only use // for something short) because the lines were too long.
That method looks too long, and shou‍ld be divided in to several smaller methods.
Never use == true or == false (line 18). Simply write && isLeapYear().
What do lines 22-25 do? Why are they there at all?
I seem to have cleaned your code up quite a bit already. Pleased to see you are using constants rather than magic numbers. Somebody has taught you well there
Check for consistency of indentation. Check for spaces around operators. Have a look at our formatting suggestions.
 
kurtis foody
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie thanks alot for the tips. ill definitely update that.
lines 22-25 is the else statement for the if statement for the decremented variable (temp--) . So are you saying that for the statement i made in line 20 i dont need an else statement? i thought all if statements need an else statements?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kurtis foody wrote:. . . all if statements need an else statements?
Yes, but if you want the else to do nothing, it is permissible to omit it entirely. Lines 22‑25 do nothing, so they can simply be deleted. Imagine there is a sort of implicit else doing nothing.
 
kurtis foody
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ahh yes that totally makes sense, for some reason i thought the blue j compiler wouldn't compile without an else statement for every single if statement but i removed it works fine. thanks again!!
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've probably learned that it's good practice to not have "magic numbers" in your source code, so you've defined constants for all those numbers.

However, creating constants for simple numbers such as FOUR and TWELVE, is, in my opinion, taking that advice a step too far. What's the practical advantage of creating constants such as FOUR and TWELVE instead of just using the numbers?

The advantage of creating constants is that you can give them meaningful names that explain what the constant is supposed to represent, for example: MONTHS_PER_YEAR = 12 - you see that the name MONTHS_PER_YEAR indicates what the meaning of the number is. A name such as TWELVE doesn't give you any useful information beyond the obvious fact that the value of that constant is 12. By using a name such as TWELVE you are losing the reason to define constants instead of just using the numbers directly.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider changing lines 47ff to a switch statement.
 
kurtis foody
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:You've probably learned that it's good practice to not have "magic numbers" in your source code, so you've defined constants for all those numbers.
However, creating constants for simple numbers such as FOUR and TWELVE, is, in my opinion, taking that advice a step too far.


@Jesper thanks for the tip!

ironically i realized that one myself before i saw your post, i realized it didn't make any sense at all ! all my magic numbers that are too simplified have been changed! thanks for the advice !

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!