| | |
Time Calculator
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 11
Reputation:
Solved Threads: 0
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
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)
public class Travel{ public static void main (String [] argv){ //variables final float kph =85; float distance; float sec; float min; float hour; float subtot; // float total; // float grandtot; // prints out user prompt System.out.println("Please Enter the Distance: "); distance = UserInput.readFloat (); // formula to work out length of travel subtot = distance/kph; // hour = subtot%10; // total = distance/kph*60; // min = total%10; // grandtot = distance/kph*60*60; // sec = grandtot%10; min = subtot*60; hour = min % 60; sec = min*60; //total= subtotal*60; // total = distance/kph; //timeInMin = System.out.println("Time in Hours: "+hour); System.out.println("Time in Minutes: "+min); System.out.println("Time in seconds: "+sec); System.out.println("The Total Time is: "+hour+":"+sec+":"+min); } }
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...
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...
•
•
Join Date: Sep 2008
Posts: 19
Reputation:
Solved Threads: 1
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)
System.out.println("Time in Hours: "+subtot);
Last edited by tonief; Nov 12th, 2008 at 7:47 pm.
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
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
•
•
Join Date: Apr 2008
Posts: 11
Reputation:
Solved Threads: 0
its the same as above ....
only thing is i changed the floats (hour, min, sec) to int.
http://img230.imageshack.us/my.php?image=javaza8.jpg
only thing is i changed the floats (hour, min, sec) to int.
java Syntax (Toggle Plain Text)
//variables final float KPH =85; float distance; int sec; float min; float hour; 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
•
•
•
•
... 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)
min = subtot*60; hour = min % 60; sec = min*60;
Java Syntax (Toggle Plain Text)
min = subtot * 60; if ( min >= 60){ hour = min%60; } else{ hour = 0; } if (hour != null){ min -= (hour*60); }
that should fix that. once you've taken them as hours, subtract the right amount of minutes from min to get the correct answer
![]() |
Similar Threads
- Calculator Missing - help please (Windows NT / 2000 / XP)
- Calculator Logic (VB.NET)
- age calculator (Computer Science)
- Project calculator (Python)
- Writing Applet ActionListener Mortgage Calculator (Java)
- Command line calculator (C)
- How to re-install software, I have lost my Calculator!! (OS X)
- The Calculator (C++)
Other Threads in the Java Forum
- Previous Thread: Simple Java Help
- Next Thread: Passing Array element to a list
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






