Question

In: Computer Science

ØWrite a method that takes a string representation of time in hours:minutes:seconds and returns the total...

ØWrite a method that takes a string representation of time in hours:minutes:seconds and returns the total number of seconds

For example, if the input is “0:02:20” it would return 140

ØIf the input string is missing the hours component it should still work

For example, input of “10:45” would return 645

ØWrite an overloaded form of the method that takes in only hours and minutes and returns the total number of seconds

It should leverage the first method you wrote

For example, an input of “2:05” should return 7205

ØLastly, write a method that takes seconds and returns a well-formatted time string, e.g., with two digits in each place

For example, an input of 3666 would return “01:01:06”

Solutions

Expert Solution

import java.util.*;
public class Main
{   
public static int TTS(int hour,int minute,int second){ //to solve normal time format
int temp;
temp = second + (60 * minute) + (3600 * hour);
return temp;
}
public static int TTS(int hour,int second){ //to overload to perform with special time cases
int temp;
temp = second + (3600 * hour);
return temp;
}
public static String sbr(int second1){ //to convert seconds to time format
int p1 = second1 % 60;
       int p2 = second1 / 60;
       int p3 = p2 % 60;

       p2 = p2 / 60;
String a="HH:MM:SS - " +p2 + ":" + p3 + ":" + p1;
       return a;
}
   public static void main(String[] args) {
   Scanner sc=new Scanner(System.in);
       String h="";
   String[] h1;
       System.out.println("Enter your choice:\n1. Time in HH:MM:SS to Seconds\n2.Time in HH:SS to Seconds\n3.Seconds to Time");
int d=sc.nextInt();
sc.nextLine();
switch(d){
case 1:
h=sc.nextLine();
h1=h.split(":");
int hour=0,minute=0,second=0;
if(h1.length==3){
hour=Integer.parseInt(h1[0]); //converting input string to integers
minute=Integer.parseInt(h1[1]);//converting input string to integers
second=Integer.parseInt(h1[2]);}//converting input string to integers
else{
hour=00;
minute=Integer.parseInt(h1[0]);
second=Integer.parseInt(h1[1]);
}
int temp=TTS(hour,minute,second);
System.out.println("secondss :"+temp);
break;
case 2:
h=sc.nextLine();
h1=h.split(":");
hour=Integer.parseInt(h1[0]); //converting input string to integers
second=Integer.parseInt(h1[1]); //converting input string to integers
temp=TTS(hour,second);
System.out.println("secondss :"+temp);
break;
case 3:
int second1=sc.nextInt();
String c=sbr(second1);
System.out.println(c);
break;
default:
System.out.println("error");
break;
}
}
}

If you found this answer helpful please give a thumbs up.


Related Solutions

Time String to Seconds Write a method that takes a string representation of time in hours:minutes:seconds...
Time String to Seconds Write a method that takes a string representation of time in hours:minutes:seconds and returns the total number of seconds For example, if the input is “0:02:20” it would return 140 If the input string is missing the hours component it should still work For example, input of “10:45” would return 645 Write an overloaded form of the method that takes in only hours and minutes and returns the total number of seconds It should leverage the...
Create a function called time2Greeting. It takes a time (in military time) and returns a string...
Create a function called time2Greeting. It takes a time (in military time) and returns a string with the right greeting. Good Morning 4AM to before noon. Good Afternoon Noon to before 5PM Good Evening from 5PM to 11PM What are you doing up at this hour? between 11 and 4AM For illegal values, say: That is not a valid time. Example: What is your name?   John What time is it? 1315 Good afternoon, John. C++ programming
Write a java method that takes a string and returns an array of int that contains...
Write a java method that takes a string and returns an array of int that contains the corresponding alphabetic order of each letter in the received string: An illustration: the method takes: "Sara" the method returns: {4,1,3,2} another illustration: the method takes: "hey" the method returns: {2,1,3}
Write a recursive method using Java that takes a string s as input and returns a...
Write a recursive method using Java that takes a string s as input and returns a list that contains all the anagrams of the string s. An anagram is a word formed by rearranging the letters of a different word. For instance, the word ‘cat’ is an anagram of ‘act’. Notice that the output list cannot contain duplicates.
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting...
Java RECURSIVE methods: => intersection(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in both s1 and s2. => union(String s1, String s2): takes two strings and returns the string consisting of all letters that appear in either s1 or s2. =>difference(String s1, String s2): takes two strings and returns the string consisting of all letters that appear only in s1.
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a static method called "evaluate" that takes a string as a parameter
In Java language  Write a static method called "evaluate" that takes a string as a parameter. The string will contain a postfix expression, consisting only of integer operands and the arithmetic operators +, -, *, and / (representing addition, subtraction, multiplication, and division respectively). All operations should be performed as integer operations. You may assume that the input string contains a properly-formed postfix expression. The method should return the integer that the expression evaluates to. The method MUST use a stack...
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT