954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Time Calculator

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 :)

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);
        
    }
}
detoxx
Light Poster
31 posts since Apr 2008
Reputation Points: 8
Solved Threads: 1
 

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...

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

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

System.out.println("Time in Hours: "+subtot);
tonief
Newbie Poster
19 posts since Sep 2008
Reputation Points: 8
Solved Threads: 1
 

Use % and / to do this.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 

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

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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 :D

detoxx
Light Poster
31 posts since Apr 2008
Reputation Points: 8
Solved Threads: 1
 

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

detoxx
Light Poster
31 posts since Apr 2008
Reputation Points: 8
Solved Threads: 1
 

How about some code

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

its the same as above ....

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

//variables
        final float KPH =85;
              float distance;
              int sec;
              float min;
              float hour;
              float subtot;


[img]http://img230.imageshack.us/my.php?image=javaza8.jpg[/img]

detoxx
Light Poster
31 posts since Apr 2008
Reputation Points: 8
Solved Threads: 1
 

... 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. ...

min = subtot*60;
               hour = min % 60;
               sec = min*60;
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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

http://img386.imageshack.us/my.php?image=questionfl0.jpg

this is the question i had to do to clear up anything.

I cant see how i can use If's and Else if to perform that tho ... a formula would do it for you :S

detoxx
Light Poster
31 posts since Apr 2008
Reputation Points: 8
Solved Threads: 1
 

thanks everyone, but i have solved it :)

public class Travel {

    public static void main(String[] argv) {

    // put your local declarations here
    final double SPEED = 85;
    double Distance = 0.0;
    long Seconds;
    long Minutes;
    long Hours;
        
    
    // Prompt and read the distance
    UserInput.prompt("Type the travel distance: ");

    Distance = UserInput.readDouble();
        
    
    // Compute and print
    Seconds = (long) ( Distance * 3600 / SPEED );

    System.out.println ( "To travel " + Distance + " takes " + Seconds
        + " seconds" );

    Minutes = Seconds / 60;
    Seconds %= 60;
    Hours = Minutes / 60;
    Minutes %= 60;

    System.out.println ( "To travel " + Distance + " takes " 
        + Hours + " hours " + Minutes + " minutes " + Seconds
        + " seconds" );

    } // end of main

} // end class


thank you again for all your help.

much appreciated.

detoxx
Light Poster
31 posts since Apr 2008
Reputation Points: 8
Solved Threads: 1
 

Nice work, you can mark the thread as solved so that people know too.

sillyboy
Practically a Master Poster
686 posts since Mar 2007
Reputation Points: 85
Solved Threads: 64
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You