Question

In: Computer Science

Write a program that request a time interval in seconds and display it in hours, minutes,...

Write a program that request a time interval in seconds and display it in hours, minutes, second format. (java)

Solutions

Expert Solution

Note: I have added a main method to show sample output and also uploaded the output image.Please comment if you need any clarifiaction.

Java code:

import java.util.Scanner;

public class Time {
    public static void main(String[] args) {
        System.out.println("Enter time in seconds");
        Scanner sc = new Scanner(System.in);
        System.out.println(secToTime(sc.nextInt()));
        System.out.println(secToTime(9));
        System.out.println(secToTime(120));
        System.out.println(secToTime(7240));
    }

    /**
     *
     * @param seconds Time in seconds
     * @return Time in hours:minutes:seconds format
     */
    static String secToTime(int seconds) {
        //Number of seconds after conversion
        int sec = seconds % 60;
        //Number of minutes after conversion
        int min = seconds / 60;
        //Number of hours after conversion
        int hr = 00;
        if (min >= 60) {
            hr = min / 60;
            min %= 60;
        }
        String second = String.format("%02d", sec);
        String minute = String.format("%02d", min);
        String hour = String.format("%02d", hr);

        return hour + ":" + minute + ":" + second;
    }
}

Output:


Related Solutions

Design a clock display to show the time in hours, minutes, and seconds. Assume that we...
Design a clock display to show the time in hours, minutes, and seconds. Assume that we have a clock of exactly 1 KHz (1000 clock pulses per second). It will use 6 seven-segment displays and operate either in military time (hours 00 to 23) or regular time (1 to 12, with AM and PM). An input line, x, differentiates between the two. A seventh display is used to show A or P in the latter case; it is blank otherwise....
Write a C++ code that can convert time into hours, minutes, seconds. It can accept (seconds),...
Write a C++ code that can convert time into hours, minutes, seconds. It can accept (seconds), (minutes, seconds), (hours, minutes, seconds) The input can be written in main It should produce the following output: (67.4, 14, 5) is 67 Hours, 38 Minutes, 5 Seconds (127.86) is 0 Hours, 2 Minutes, 8 Seconds (-3, 73, 2) is -1 Hours, -46 Minutes, -58 Seconds
Using Quartus to design a stopwatch that can display tenths of seconds, minutes, and hours, with...
Using Quartus to design a stopwatch that can display tenths of seconds, minutes, and hours, with the gaps between HEX displays splitting the time units (i.e. minutes on HEX[5..4], hours on HEX[7..6]). Have the stopwatch cycle through 3 states of a state machine with a pushbutton. The 3 states are counter-clear, counter-start, and counter-stop
Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
Write a program that accepts a number of minutes and converts it to days and hours....
Write a program that accepts a number of minutes and converts it to days and hours. For example, 6000 minutes represents 4 days and 4 hours. Be sure to provide proper exception handling for non-numeric values and for negative values. Save the file as  MinuteConversionWithExceptionHandling.java
Write the definition for a generic / Template class called time that has hours and minutes...
Write the definition for a generic / Template class called time that has hours and minutes as structure. The class has the following member functions: SetTime to set the specified value in object ShowTime to display time object Sum to sum two time object & return time Write the definitions for each of the above member functions. Write main function to create three time objects. Set the value in two objects and call sum() to calculate sum and assign it...
Time Remaining 2 hours 2 minutes 11 seconds 02:02:11 Item 6 Item 6 Time Remaining 2...
Time Remaining 2 hours 2 minutes 11 seconds 02:02:11 Item 6 Item 6 Time Remaining 2 hours 2 minutes 11 seconds 02:02:11 The Molson Company had budgeted production for the year as follows: Quarter 1 2 3 4 Production in units 11,700 13,700 19,600 15,700 Four pounds of raw materials are required for each unit produced. Raw materials on hand at the start of the year total 5,700 lbs. The raw materials inventory at the end of each quarter should...
Time Remaining 3 hours 5 minutes 26 seconds 03:05:26 eBook Check my work Check My Work...
Time Remaining 3 hours 5 minutes 26 seconds 03:05:26 eBook Check my work Check My Work button is now disabled Item 3 Item 3 Time Remaining 3 hours 5 minutes 26 seconds 03:05:26 Polaski Company manufactures and sells a single product called a Ret. Operating at capacity, the company can produce and sell 38,000 Rets per year. Costs associated with this level of production and sales are given below: Unit Total Direct materials $ 20 $ 760,000 Direct labor 10...
Write a C++ program to display toy name
Write a C++ program to display toy name
Time Remaining 5 minutes 19 seconds 00:05:19 Item 23 Item 23 Time Remaining 5 minutes 19...
Time Remaining 5 minutes 19 seconds 00:05:19 Item 23 Item 23 Time Remaining 5 minutes 19 seconds 00:05:19 On April 1st, Bob the Builder entered into a contract of one-month duration to build a barn for Nolan. Bob is guaranteed to receive a base fee of $5,800 for his services in addition to a bonus depending on when the project is completed. Nolan created incentives for Bob to finish the barn as soon as he can without jeopardizing the structural...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT