• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ignoring week for a Method in a Class that has Day, Month, Year  RSS feed

 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a peculiar problem with a Method I wrote.
I created a class called DateTime that represents a Date with Time.
In this class the Date is represented by days, months, and years, as our regular Calendar.
Now 2 of the Methods I had to write, called isAfter(DateTime other) and isBefore(DateTime other) checked whether the current Object was after or before the other Parameter.

So far so good, I wrote these Methods and they work.

But now I have to write these Methods again with an additional Parameter like this: isAfter(DateTime other, boolean ignoreWeek) and the same for isBefore().

Now from the Idea these Methods should be very similar to the original two, but I cant seem to figure out a way of how to ignore the Week.
The only hint my Professor would give me was: Figure out the Weekday as a number. thats it. I assume he means which day of the week (1-7).
Now Im at a loss, how can I use the weekday number, which I already have a method for, to ignore the Week in this Method?

Below my isAfter and isBefore Methods:



And the ignoreWeek Methods:



Thanks for your Help!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by writing down exactly what your methods are supposed to do. What does ignoring week mean? Write it in simple English/other language in really small words, so even I can understand it.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish I knew what it meant myself, or else I would describe it better.
My teacher won't tell me more. His description of the method is: "Does the current Object lie before other, when ignoring the Week?"
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand that either.

But your isBefore and isAfter methods look horribly complicated to me.
Do you have an equals() method?
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should probably use one to make it simpler...
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to answer your question, no we did not have to make one.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, an equals() method would make writing after and before methods much easier. In fact if you have equals and after you don't need to work out a before method.
 
Dennis Von Valkenburgh
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the before method is part of the requirement in the assignment, I wont pass Junit without it.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In each method you can delete:
it is redundant
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if you have written after and equals before will almost write itself.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Dennis,

a way to greatly simplify your 'isAfter' method is to write the
year, month and day in this way:
yyyymmdd = 10000 * year + 100 * month + day.

You could put that into a private method.

Then all that's needed for 'isAfter' is:

return yyyymmdd() > other.yyyymmdd()

If needed, you could do the same for hours, minutes and seconds.

And maybe you could assume that two dates that differ no more than 7 days
are to be considered equal. It's a start...

Greetz,
Piet
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you aware of the ?: operator?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!