In Java, create a program and Flowchart, Write a method that converts milliseconds to hours, minutes, and seconds using the following header: public static String convertMillis(long millis) The method returns a string as hours:minutes:seconds. For example, convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Write a test program that prompts the user to enter a long integer for milliseconds and displays a string in the format of hours:minutes:seconds.
Given below is the code for question. Please do rate the answer if it helped. Thank you.
Converter.java
-------
import java.util.Scanner;
public class Converter {
public static String convertMillis(long millis)
{
int MILLIS_PER_SEC = 1000;
int SECS_PER_MIN = 60;
int MINS_PER_HOUR = 60;
int hours = 0, mins = 0, secs =
0;
secs = (int)( millis /
MILLIS_PER_SEC);
mins = secs / SECS_PER_MIN;
secs = secs % SECS_PER_MIN;
hours = mins / MINS_PER_HOUR;
mins = mins % MINS_PER_HOUR;
return hours + ":" + mins + ":" +
secs;
}
public static void main(String[] args) {
Scanner scnr = new
Scanner(System.in);
long millis;
boolean done = false;
while(!done) {
System.out.print("Enter milliseconds (type 0 to quit): ");
millis =
scnr.nextLong();
if(millis <=
0)
done = true;
else {
System.out.println(millis + " is " +
convertMillis(millis));
}
}
}
}
Get Answers For Free
Most questions answered within 1 hours.