May I please get help on what I need to do for this homework? I want to
Question:
May I please get help on what I need to do for this homework? I want to write a code and want an explanation of what I need to do.
The Assignment:
Write a program that converts a number of seconds to the equivalent number of hours, minutes, and seconds.
It should do the following:
- Prompt the user for input
- Read an integer from the keyboard
- Calculate the result
- Useprintf to display the output
A sample run with input 5000 must look like:
Enter the number of seconds: 5000
5000 seconds = 1 hours, 23 minutes, and 20 seconds
5000 seconds = 01h:23m:20s
A sample run with input 3754 must look like:
Enter the number of seconds: 3754
3754 seconds = 1 hours, 2 minutes, and 34 seconds
3754 seconds = 01h:02m:34s
Hint1: Use integer division
Hint2:Use the modulus operator
Please make sure to end each line of output with anewline.
Please note that your class should be namedSecondsConverter.
What I wrote:
import java.util.*; public class SecondsConverter { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter the number of seconds: "); int totalSeconds = in.nextInt(); int numSeconds = totalSeconds % 60; int numHours = totalSeconds / 60; int numMinutes = numHours % 60; numHours = numHours / 60; System.out.print( totalSeconds+" Seconds ="+numHours + " hours," + numMinutes + " minutes,and " + numSeconds + " seconds"); System.out.println(""); System.out.print( totalSeconds+" Seconds ="+numHours + "h:" + numMinutes + "m:" + numSeconds + "s"); System.out.print(" "); } }
Thank you