Results 1 to 6 of 6
Thread: Minutes to return
- 01-04-2013, 07:25 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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.
I don't know what to even start with.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() { } }
- 01-04-2013, 07:38 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Minutes to return
Suppose it's 15:20 (3:20pm); 15*60 + 20 minutes have passed since midnight ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-04-2013, 08:09 PM #3
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
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){ } }
- 01-04-2013, 08:15 PM #4
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
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!.gif)
- 01-04-2013, 08:19 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 01-04-2013, 08:42 PM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Similar Threads
-
Turn seconds to hours/minutes/sec
By Scruss in forum New To JavaReplies: 6Last Post: 09-06-2011, 06:48 PM -
Call a method every 5 minutes.
By PhQ in forum New To JavaReplies: 2Last Post: 03-31-2010, 03:31 AM -
getting hours and minutes
By silversurfer2in in forum New To JavaReplies: 5Last Post: 02-17-2010, 07:35 AM -
How to beep every 5 minutes in Java
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:40 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks