Results 1 to 6 of 6
  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Minutes to return

    Exercise 4.16* Write a Time method public int timeInMinutes(): The executor tells the total number of minutes that have passed since midnight.

    Java Code:
    public class Time extends Object {
        private int itsHour;
        private int itsMin;
        private int timeMin;
        /** Create an object for the given hour and minute. If min
           * is negative, adjust the values to make 0 <= min < 60. */
           public Time (int hour, int min) {
               super();
               itsHour = hour;
               for (itsMin = min; itsMin < 0; itsMin = itsMin + 60){
               itsHour--;
              }
            } //=======================
            /** Return the time expressed in military time. */
            public String toString(){ 
                if (itsHour < 10) {
                return ("0" + itsHour) + itsMin;
            }else{
                return ("" + itsHour) + itsMin;
            }
        } //=======================
        /** Return the result of adding this Time to that Time. */
            public Time add (Time that){
                return that;
            } // left as an exercise
            
            public int timeInMinutes() {
                
            }
    }
    I don't know what to even start with.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,303
    Blog Entries
    7
    Rep Power
    20

    Default Re: Minutes to return

    Suppose it's 15:20 (3:20pm); 15*60 + 20 minutes have passed since midnight ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Re: Minutes to return

    I finished that but now I've run into this one and I can't finish it.

    Exercise 4.17* Write a Time method public Time subtract (Time that): The executor returns a new Time object that is itself minus the parameter, e.g., 0720 subtract 1430 is 1650. If the difference is negative, add an extra 24 hours.

    Java Code:
     public Time subtract (Time that) {
                Time sub = sub - that;
                if (sub < 0){
                    
                }
                
        }

  4. #4
    Sierra is offline AN21XX
    Join Date
    Mar 2012
    Location
    Munich
    Posts
    297
    Rep Power
    3

    Default Re: Minutes to return

    "sub" ist not "yourself" (that is your "this" object in that method)... first you need to create a NEW object.
    Then copy your own object information to the new object.
    Then you subtract the time and check for its validity. All right so far?
    I like likes!

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,303
    Blog Entries
    7
    Rep Power
    20

    Default Re: Minutes to return

    Go for the hh:mm notation again: 7:20 - 14:30 == -7:-10; the minutes are negative, so correct it: -8:50; the hours are negative, so correct it: 16:50. You can't just so sub-that where one (or both) of them is/are of type Time.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

Similar Threads

  1. Turn seconds to hours/minutes/sec
    By Scruss in forum New To Java
    Replies: 6
    Last Post: 09-06-2011, 06:48 PM
  2. Call a method every 5 minutes.
    By PhQ in forum New To Java
    Replies: 2
    Last Post: 03-31-2010, 03:31 AM
  3. getting hours and minutes
    By silversurfer2in in forum New To Java
    Replies: 5
    Last Post: 02-17-2010, 07:35 AM
  4. How to beep every 5 minutes in Java
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:40 PM

Posting Permissions

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