This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes class ClockTime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class ClockTime" Watch "class ClockTime" New topic
Author

class ClockTime

Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
I've been working on a class called ClockTime where methods are used to carry out various tasks on time. My code is as follows:



I'm having problems with method equals() and method before(). When I call method equals(), even if I plug values into each array that make them identical, it is returning the string that they are NOT equal. Same thing happens when I specify values that aren't equal.

When I call method before(), if I use values that make myTime occur before clock, it still returns false. Same thing happens vice versa.

I don't think I'm fully grasping the relationship between arrays and equality. Please help!

THANKS!
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
there should be a small correction made in the before method



it should be like

in your case it checks the hour first then the minutes and then the seconds. if hour is leeser and minute is greater or vice versa will affect the result. your code will work only if all the values are greater or all the values are less..

[ November 02, 2004: Message edited by: Karthikeyan Rajendraprasad ]
[ November 02, 2004: Message edited by: Karthikeyan Rajendraprasad ]

Karthikeyan<br />SCJP 1.4, SCWCD.
Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
So then I should get rid of the variable result and the return result statement outside of the for loop, right?

Thanks.
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
no you cant remove it, if the time is equal you have to return using that variable only. you can either set it as true or false as per your requirement.
Amy Lee
Greenhorn

Joined: Nov 02, 2004
Posts: 25
Your method works - THANKS! - but I want to understand. I feel like this should be apparent but...

1. boolean result is declared outside the for loop and is initialized to true

2. there are three scenarios in the if statement

3. say the myTime occurs AFTER clock; the if statement will return false - myTime does not occur before clock

4. nowhere in the if statement does it assign the boolean false to result outside the for loop. how does the method know to return result as false if it is never assigned false and it was initialized to true to begin with?

Thanks for your patience.
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
oops i missed the && result condition in the for loop.

i think you can remove it and set the result as false so the method should look like
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class ClockTime