# StaticMethod...Help?!

Printable View

• 10-17-2010, 08:17 PM
daniellex0x
StaticMethod...Help?!
Okay, so I need to make Static method...but for some reason I keep getting this error:

operator - cannot be applied to Time,Time
I have a Time Class that contains int hours and int minutes.

I'm trying to minus two time from each other, like 10:10 - 1:10 would equel 9:00. The format would be:

System.out.println(Time.difference(new Time(1,10), new Time(10,10)));
So far, I thought that this would work:

public static int difference(Time t1,Time t2){
return t2 - t1 ;
}

But, alas - no go.

So I tried:

public static int difference(Time t1,Time t2){
return t2.hours - t1.hours ;

But it only gives me the hours (of course). and I don't know how to incorperated the minutes :S

Any help would be apperciated!

-D
• 10-17-2010, 08:46 PM
pbrockway2
Why make the method static? My rule of thumb is to trust what the library writers do: and, for instance, BigDecimal's subtract() isn't static. "Static" may not be harmful, but it's often considered rather disreputable.

What is the int that is returned by the method supposed to be? If it is hours as suggested by "t2.hours-t1.hours" then will it have to be double or something to represent time intervals with the granularity of minutes. If you returned an int representing minutes it migt be a little more straightfoward:

Code:

```            |-------- Subtract the number of minutes up             v        to t2 from the number up to t1 *      t1      t2 ......... ..................```

Each time represents a number of minutes from some starting point and you just have to subtract these values to figure out the number of minutes to return. Because you are performing subtraction the starting point is quite conventional.

If you want to return both hours and minutes you could return an instance of Time. (Note in English we use the word "time" ambiguously: it can be a moment in the passage of events or it can be the amount of such change between two events.) Once you have the result in minutes it should not be hard to represent this as a Time instance using div and mod.
• 10-18-2010, 12:30 AM
daniellex0x
It has to be a static method :S