(*Construct a Java Program*) Time Calculator Write a program that asks the user
ID: 668956 • Letter: #
Question
(*Construct a Java Program*)
Time Calculator
Write a program that asks the user to enter a number of seconds.
* There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds.
* There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to 3,600, the program should display the number of hours in that many seconds.
* There are 86,400 seconds in a day. If the number of seconds entered by the user is greater than or equal to 86,400, the program should display the number of days in that many seconds.
The output should look like this (it is a good idea to echo back the input):
You entered 500,000 seconds, which is 5 days, 18 hours, 53 minutes and 20 seconds. (5 days 18:53:20 hours)
If the result is 0 days, then don't display the days, if the hours is 0 and the days are 0, then don't display the hours, if the minutes are 0, the hours are 0 and the days are 0, then only display the seconds
Explanation / Answer
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TimeCalculator {
public static void main(String args[]){
long input=0;
long sec=0;
long minute=0;
long hours=0;
long days=0;
System.out.println("Enter seconds : ");
try{
BufferedReader bufferRead = new BufferedReader(new InputStreamReader(System.in));
String s = bufferRead.readLine();
input=Long.valueOf(s);
}
catch(IOException e)
{
e.printStackTrace();
}
if(input>=60){
minute=input/60;
sec=input- (minute*60);
}
if(minute>=60){
hours=minute/60;
minute=minute%60;
}
if(hours>=24){
days=hours/24;
hours=hours%24;
}
if(days>0 )
System.out.print(days +" Days");
if(hours>0 && hours<25 )
System.out.print(hours +" Hours");
if(minute>0 && hours<61 )
System.out.print(minute +" Minutes");
if(sec>0 && sec<61 )
System.out.print(sec +" Seconds");
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.