In: Computer Science
Time Calculator – Intro To Programming - JAVA 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 .JAVA file from your program should be attached to this assignment.
Code for the Time calculator is as following. For the given java code class name is taken as "TimeCalculator" and file was saved with TimeCalculator.java, so change it as per your requirement (if changes required).
import java.util.Scanner;
class TimeCalculator {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter number of seconds:");
//take input from user - the number of seconds
long sec = sc.nextLong();
//if #seconds > 86,400, calcuate number of days and diplay them
if(sec>=86400){
System.out.println((sec/86400) + " day");
sec = sec%86400;
}
//if number of seconds left after calculating number of days, calculate hours
if(sec>=3600){
System.out.println((sec/3600) + " hour");
sec = sec%3600;
}
//calculate number of minutes
if(sec>=60){
System.out.println((sec/60)+" minute");
sec = sec%60;
}
// calculate number of seconds
if(sec>0){
System.out.println(sec+" seconds");
}
sc.close();
}
}
Screenshot of the above code for better readability of the code is given below:
Sample output for the aboce program is as following:
Sample output 1.
Sample output 2.
Sample output 3.
Sample output 4.