Results 1 to 3 of 3
  1. #1
    daniellex0x is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Default 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

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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:

    Java 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.

  3. #3
    daniellex0x is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •