/**
TimeCalcBetter.java
Craig Persiko
CS 111A Extra-Credit Solution to Programming Lab 1
Modified version of program taken from
Tony Gaddis' Starting Out with Java,
from Control Structures to Objects
Chapter 3
Programming Challenge 6: Time Calculator
*/
import java.util.Scanner;
public class TimeCalcBetter
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
double seconds; // The number of seconds
// Get the number of seconds.
System.out.println("Enter the number of seconds");
seconds = scan.nextDouble();
System.out.println("That equals:");
// Display the number of days, if any.
if (seconds >= 86400)
{
// cast seconds to an int so we get integer division: whole number of days
System.out.print(((int)seconds / 86400) + " days ");
// remove full days from seconds, so it represents a portion of a day
seconds %= 86400;
}
// Display the number of hours, if any.
if (seconds >= 3600)
{
System.out.print(((int)seconds / 3600) + " hours ");
seconds %= 3600;
}
// Display the number of minutes, if any.
if (seconds >= 60)
{
System.out.print(((int)seconds / 60) + " minutes ");
seconds %= 60;
}
System.out.println(seconds + " seconds.");
}
}
/* Sample Output:
-bash-3.2$ java TimeCalcBetter
Enter the number of seconds
30
That equals:
30.0 seconds.
-bash-3.2$ java TimeCalcBetter
Enter the number of seconds
90
That equals:
1 minutes 30.0 seconds.
-bash-3.2$ java TimeCalcBetter
Enter the number of seconds
4000
That equals:
1 hours 6 minutes 40.0 seconds.
-bash-3.2$ java TimeCalcBetter
Enter the number of seconds
90000
That equals:
1 days 1 hours 0.0 seconds.
-bash-3.2$ java TimeCalcBetter
Enter the number of seconds
95000
That equals:
1 days 2 hours 23 minutes 20.0 seconds.
-bash-3.2$
*/
syntax highlighted by Code2HTML, v. 0.9