Time Calculator

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2008
Posts: 11
Reputation: detoxx is an unknown quantity at this point 
Solved Threads: 0
detoxx detoxx is offline Offline
Newbie Poster

Time Calculator

 
0
  #1
Nov 12th, 2008
Can anyone help me please? i need to create a program in which you enter a distance and the program will tell you how long it will take to get there in hours minutes and seconds.

My trouble is that they all display independently and not seperately ... for example ... i want it to display "1 hour 20mins 4 seconds" instead of "1.20 hours 80 mins" and how ever many in seconds.


thank you in advance for your help


  1. public class Travel{
  2.  
  3. public static void main (String [] argv){
  4.  
  5. //variables
  6. final float kph =85;
  7. float distance;
  8. float sec;
  9. float min;
  10. float hour;
  11. float subtot;
  12. // float total;
  13. // float grandtot;
  14.  
  15. // prints out user prompt
  16. System.out.println("Please Enter the Distance: ");
  17. distance = UserInput.readFloat ();
  18.  
  19. // formula to work out length of travel
  20. subtot = distance/kph;
  21. // hour = subtot%10;
  22.  
  23. // total = distance/kph*60;
  24. // min = total%10;
  25.  
  26. // grandtot = distance/kph*60*60;
  27. // sec = grandtot%10;
  28.  
  29. min = subtot*60;
  30. hour = min % 60;
  31. sec = min*60;
  32. //total= subtotal*60;
  33.  
  34. // total = distance/kph;
  35.  
  36. //timeInMin =
  37.  
  38. System.out.println("Time in Hours: "+hour);
  39. System.out.println("Time in Minutes: "+min);
  40. System.out.println("Time in seconds: "+sec);
  41. System.out.println("The Total Time is: "+hour+":"+sec+":"+min);
  42.  
  43. }
  44. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Time Calculator

 
0
  #2
Nov 12th, 2008
For staters, you don't need to be using floats for hour, sec, min and this is where a lot of your issues will be starting.

If you avoid decimal multiplication / division, you will probably get results closer to what you want. e.g. 1.20 = 1 in int (I am referring to your hours).

After you calculate hours, you need to take this into account when calculating the minutes. After you get the minutes you need to take this & hours into account when you calculate the seconds.

Hope this helps...
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 19
Reputation: tonief is an unknown quantity at this point 
Solved Threads: 1
tonief tonief is offline Offline
Newbie Poster

Re: Time Calculator

 
0
  #3
Nov 12th, 2008
I think that this if kph means speed than distance/kph means time in hours you don't need variable hours. you can simlply write
  1. System.out.println("Time in Hours: "+subtot);
Last edited by tonief; Nov 12th, 2008 at 7:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Time Calculator

 
0
  #4
Nov 12th, 2008
Use % and / to do this.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Time Calculator

 
0
  #5
Nov 13th, 2008
In Physics everything is measured in meters and seconds.
So after you get the speed and distance, Convert both to meters/sec and meters. After you do the division you will have a clean value in seconds which can be easily converted to minutes and hours.
If the final value has decimal points: 65.33 seconds then keep them at the seconds value or round them or have another variable for miliseconds:
1 minute and 5.33 seconds or
1 minute and 5 seconds or
1 minute and 5 seconds and 330 milliseconds
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: detoxx is an unknown quantity at this point 
Solved Threads: 0
detoxx detoxx is offline Offline
Newbie Poster

Re: Time Calculator

 
0
  #6
Nov 13th, 2008
Thank you for your posts!! it is really appreciated

i will give it a go and let you know how it turns out.

thanks again
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: detoxx is an unknown quantity at this point 
Solved Threads: 0
detoxx detoxx is offline Offline
Newbie Poster

Re: Time Calculator

 
0
  #7
Nov 18th, 2008
I tried using the modulo and i still get decimals.

I also tried changing my some of my floats to integers, but my compiler will not let it compile as it says "Possible Loss of Precision"

is there anyway around this?

p.s I changed the hour, min and sec to int
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,678
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Time Calculator

 
0
  #8
Nov 18th, 2008
How about some code
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 11
Reputation: detoxx is an unknown quantity at this point 
Solved Threads: 0
detoxx detoxx is offline Offline
Newbie Poster

Re: Time Calculator

 
0
  #9
Nov 18th, 2008
its the same as above ....

only thing is i changed the floats (hour, min, sec) to int.

  1. //variables
  2. final float KPH =85;
  3. float distance;
  4. int sec;
  5. float min;
  6. float hour;
  7. float subtot;

http://img230.imageshack.us/my.php?image=javaza8.jpg
Last edited by detoxx; Nov 18th, 2008 at 9:36 am. Reason: added image of compiler
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Time Calculator

 
0
  #10
Nov 18th, 2008
Originally Posted by detoxx View Post
... for example ... i want it to display "1 hour 20mins 4 seconds" instead of "1.20 hours 80 mins" and how ever many in seconds. ...

  1.  
  2. min = subtot*60;
  3. hour = min % 60;
  4. sec = min*60;
  1. min = subtot * 60;
  2. if ( min >= 60){
  3. hour = min%60;
  4. }
  5. else{
  6. hour = 0;
  7. }
  8.  
  9. if (hour != null){
  10. min -= (hour*60);
  11. }

that should fix that. once you've taken them as hours, subtract the right amount of minutes from min to get the correct answer
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC