Question

In: Computer Science

3. Problem 3: How much time has elapsed? Unix time is commonly used to track the...

3. Problem 3: How much time has elapsed? Unix time is commonly used to track the amount of time that has passed in computer systems. The Unix time is stored as the number of seconds that have passed since 00:00:00 UTC January 1, 1970. However, the number of seconds by itself is not directly meaningful to most users, but luckily for us the number of seconds elapsed provides sufficient information to infer what we may need to display to an end user. Write a method, convertSeconds, which takes as input a non-negative number of seconds and returns a string of the form: ’h hours, m minutes, s seconds’ but where if ’h’, ’m’, or ’s’ is 1 for the number of hours, minutes, or seconds, then the singular form of the word should be used (hour, minute, or second). Note that English uses the plural when talking about 0 items, so it should be ”0 minutes”. As always if a constraint is broken print an error message and in this case return the empty string (””). .java = TimeConversion.java Examples (a) convertSeconds(7325) returns ”2 hours, 2 minutes, 5 seconds” (b) convertSeconds(1) returns ”0 hours, 0 minutes, 1 second”

Solutions

Expert Solution

Thanks for the question.

Below is the code you will be needing  Let me know if you have any doubts or if you need anything to change.

Thank You !!

===========================================================================

public class Timer {


    public static String convertSeconds(int seconds) {

        int hours = seconds / 3660;
        seconds = seconds % 3600;
        int minutes = seconds / 60;
        seconds = seconds % 60;

        StringBuilder time = new StringBuilder();
        if (hours != 1) {
            time.append(hours).append(" hours, ");
        } else {
            time.append(hours).append(" hour, ");
        }

        if (minutes != 1) {
            time.append(minutes).append(" minutes, ");
        } else {
            time.append(minutes).append(" minute, ");
        }

        if (seconds != 1) {
            time.append(seconds).append(" seconds");
        } else {
            time.append(seconds).append(" second");
        }

        return time.toString();
    }


    public static void main(String[] args) {

        System.out.println(convertSeconds(7325));
        System.out.println(convertSeconds(3661));
        System.out.println(convertSeconds(1));
    }
}

===========================================================================


Thank you so much!

Please do appreciate with an upvote : )


Related Solutions

In accounting, balance sheets are used to keep of track how much money a person has...
In accounting, balance sheets are used to keep of track how much money a person has in an account.  Write a Python program to create a simple three column balance sheet, showing credits in one column, debits in the next, and a running balance in the third.  If the balance dips into the negative, you must show the value in parentheses, the standard method in accounting. Input will be from a text file. The first value will be the opening balance in...
3. Assume that the time elapsed between customers entering a retail shop is exponentially decreasing by...
3. Assume that the time elapsed between customers entering a retail shop is exponentially decreasing by 15%. How long will the average customer continue to come before the shop runs out of business. 4. .The weight of the fabled Cobrafish is normally distributed with a mean of 45 kg. and a standard deviation of 6 kg. Approximately what percentage of Cobrafish (rounded to two decimal places) weigh between 30 kg. and 49 kg? Note: You must find the corresponding z-scores...
Some pharmaceuticals were much more commonly prescribed at one time but today are prescribed much less...
Some pharmaceuticals were much more commonly prescribed at one time but today are prescribed much less frequently. Pick two such substances and explain the dramatic decline in psychiatric use. Be as specific and as detailed as you can. use Goode's book and tell page number
How much time does an algorithm take to solve a problem of size n if this...
How much time does an algorithm take to solve a problem of size n if this algorithm uses 2n2 + 2n operations, each requiring 10-8 seconds, with these values of n? a) 10: b) 20: c) 50: d) 100
How to proof that the 2-partition problem can be transformed to 3-partition problem and the time...
How to proof that the 2-partition problem can be transformed to 3-partition problem and the time complexity of the transformation (i.e. the 2-partition problem can be solved by using an algorithm that solves the 3-partition problem)
3. Suppose you are in the market for a used treadmill and find a Nordic Track...
3. Suppose you are in the market for a used treadmill and find a Nordic Track Commercial 2150 on sale for $1000. If you knew the motor was in good condition, you would be willing to pay $1,400. Meanwhile, if you knew the motor was only in fair condition, you would only be willing to pay $400 for the treadmill. Explain the circumstances under which you would be willing to purchase the treadmill for $900. a) Please supply an example...
How much customers buy is a direct result of how much time they spend in the...
How much customers buy is a direct result of how much time they spend in the store. A study of average shopping times in a large national houseware store gave the following information (Source: Why We Buy: The Science of Shopping by P. Underhill). Women with female companion: 8.3 min. Women with male companion: 4.5 min. Suppose you want to set up a statistical test to challenge the claim that a woman with a female friend spends an average of...
How much customers buy is a direct result of how much time they spend in the...
How much customers buy is a direct result of how much time they spend in the store. A study of average shopping times in a large national houseware store gave the following information (Source: Why We Buy: The Science of Shopping by P. Underhill). Women with female companion: 8.3 min. Women with male companion: 4.5 min. Suppose you want to set up a statistical test to challenge the claim that a woman with a female friend spends an average of...
How much customers buy is a direct result of how much time they spend in the...
How much customers buy is a direct result of how much time they spend in the store. A study of average shopping times in a large national houseware store gave the following information (Source: Why We Buy: The Science of Shopping by P. Underhill). Women with female companion: 8.3 min. Women with male companion: 4.5 min. Suppose you want to set up a statistical test to challenge the claim that a woman with a female friend spends an average of...
Explain the 3 primary ingredients of Just in Time and how it can be used in...
Explain the 3 primary ingredients of Just in Time and how it can be used in a transportation company.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT