In: Computer Science
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”
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 : )