943,822 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 3229
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 12th, 2008
0

Time Calculator

Expand Post »
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


java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 8
Solved Threads: 1
Light Poster
detoxx is offline Offline
31 posts
since Apr 2008
Nov 12th, 2008
0

Re: Time Calculator

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...
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Nov 12th, 2008
0

Re: Time Calculator

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
Java Syntax (Toggle Plain Text)
  1. System.out.println("Time in Hours: "+subtot);
Last edited by tonief; Nov 12th, 2008 at 7:47 pm.
Reputation Points: 8
Solved Threads: 1
Newbie Poster
tonief is offline Offline
19 posts
since Sep 2008
Nov 12th, 2008
0

Re: Time Calculator

Use % and / to do this.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Nov 13th, 2008
0

Re: Time Calculator

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
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Nov 13th, 2008
0

Re: Time Calculator

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
Reputation Points: 8
Solved Threads: 1
Light Poster
detoxx is offline Offline
31 posts
since Apr 2008
Nov 18th, 2008
0

Re: Time Calculator

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
Reputation Points: 8
Solved Threads: 1
Light Poster
detoxx is offline Offline
31 posts
since Apr 2008
Nov 18th, 2008
0

Re: Time Calculator

How about some code
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Nov 18th, 2008
0

Re: Time Calculator

its the same as above ....

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

java Syntax (Toggle Plain Text)
  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
Reputation Points: 8
Solved Threads: 1
Light Poster
detoxx is offline Offline
31 posts
since Apr 2008
Nov 18th, 2008
0

Re: Time Calculator

Click to Expand / Collapse  Quote originally posted by detoxx ...
... 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. ...

java Syntax (Toggle Plain Text)
  1.  
  2. min = subtot*60;
  3. hour = min % 60;
  4. sec = min*60;
Java Syntax (Toggle Plain Text)
  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
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is online now Online
2,496 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Simple Java Help
Next Thread in Java Forum Timeline: Passing Array element to a list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC