Help me in C# please, this is what I have done so far. I'm stuck at if user input just Enter key, or 12:20 (Input is not the right format, or "Blahhaha" (string letter) will print out "Enter valid input" as instructions. And when the user input the wrong format for the first time, it has to catch an error and stop the program immediately. class Program     {         public static int militaryTime (string s1, string s2)         {             string[] arr1 = s1.Split(":", 3);             string[] arr2 = s2.Split(":", 3);             if (Convert.ToInt32(arr1[0]) > 23 || Convert.ToInt32(arr1[0]) < 0 || Convert.ToInt32(arr2[0]) > 23 || Convert.ToInt32(arr2[0]) < 0)                 throw new InvalidTimeException("Hour must be below 24");             if (Convert.ToInt32(arr1[1]) > 59 || Convert.ToInt32(arr1[0]) < 0 || Convert.ToInt32(arr2[1]) > 59 || Convert.ToInt32(arr2[1]) < 0)                 throw new InvalidTimeException("Minutes must be less than 60");             if (Convert.ToInt32(arr1[2]) > 23 || Convert.ToInt32(arr1[0]) < 0 || Convert.ToInt32(arr2[2]) > 59 || Convert.ToInt32(arr2[2]) < 0)                 throw new InvalidTimeException("Seconds must be less than 60");             if (Convert.ToBoolean (arr1.Length != 3))                 throw new InvalidTimeException("Seconds must be less than 60");             //if (arr1.Length != 3 || arr2.Length != 3)             //{             //    throw new InvalidTimeException("Enter a valid time");             //}               else             {                 int hour = Convert.ToInt32(arr1[0]) - Convert.ToInt32(arr2[0]);                 int min = Convert.ToInt32(arr1[1]) - Convert.ToInt32(arr2[1]);                 int sec = Convert.ToInt32(arr1[2]) - Convert.ToInt32(arr2[2]);                 int result = (hour * 3600) + (min * 60) + sec;                 if (result < 0)                 {                     result *= -1;                 }                 return result;             }         }         static void Main(string[] args)         {             Console.WriteLine("Enter time 1 in 24hr format as follows (HH:MM:SS) ");             string input1 = Console.ReadLine();             Console.WriteLine("Enter time 2 in 24hr format as follows (HH:MM:SS) ");             string input2 = Console.ReadLine();             try             {                 Console.WriteLine("Difference in seconds: " + militaryTime(input1, input2));             }             catch (InvalidTimeException e)             {

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Help me in C# please, this is what I have done so far. I'm stuck at if user input just Enter key, or 12:20 (Input is not the right format, or "Blahhaha" (string letter) will print out "Enter valid input" as instructions. And when the user input the wrong format for the first time, it has to catch an error and stop the program immediately.

class Program
    {
        public static int militaryTime (string s1, string s2)
        {
            string[] arr1 = s1.Split(":", 3);
            string[] arr2 = s2.Split(":", 3);

            if (Convert.ToInt32(arr1[0]) > 23 || Convert.ToInt32(arr1[0]) < 0 || Convert.ToInt32(arr2[0]) > 23 || Convert.ToInt32(arr2[0]) < 0)
                throw new InvalidTimeException("Hour must be below 24");

            if (Convert.ToInt32(arr1[1]) > 59 || Convert.ToInt32(arr1[0]) < 0 || Convert.ToInt32(arr2[1]) > 59 || Convert.ToInt32(arr2[1]) < 0)
                throw new InvalidTimeException("Minutes must be less than 60");

            if (Convert.ToInt32(arr1[2]) > 23 || Convert.ToInt32(arr1[0]) < 0 || Convert.ToInt32(arr2[2]) > 59 || Convert.ToInt32(arr2[2]) < 0)
                throw new InvalidTimeException("Seconds must be less than 60");

            if (Convert.ToBoolean (arr1.Length != 3))
                throw new InvalidTimeException("Seconds must be less than 60");

            //if (arr1.Length != 3 || arr2.Length != 3)
            //{
            //    throw new InvalidTimeException("Enter a valid time");
            //}

 

            else
            {
                int hour = Convert.ToInt32(arr1[0]) - Convert.ToInt32(arr2[0]);
                int min = Convert.ToInt32(arr1[1]) - Convert.ToInt32(arr2[1]);
                int sec = Convert.ToInt32(arr1[2]) - Convert.ToInt32(arr2[2]);

                int result = (hour * 3600) + (min * 60) + sec;

                if (result < 0)
                {
                    result *= -1;
                }

                return result;
            }


        }

        static void Main(string[] args)
        {
            Console.WriteLine("Enter time 1 in 24hr format as follows (HH:MM:SS) ");
            string input1 = Console.ReadLine();

            Console.WriteLine("Enter time 2 in 24hr format as follows (HH:MM:SS) ");
            string input2 = Console.ReadLine();

            try
            {
                Console.WriteLine("Difference in seconds: " + militaryTime(input1, input2));
            }

            catch (InvalidTimeException e)
            {
                Console.WriteLine(e.Message);
            }
            
        }

        public class InvalidTimeException : Exception
        {
            public InvalidTimeException() { }

            public InvalidTimeException(string message) : base(message) { }

            public InvalidTimeException(string message, Exception inner) : base(message, inner) { }
        }
    }

Objectives:
In this lab you’ll allow the user to enter in 2 times in 24hr (military) format. You'll calculate how many
seconds are between those times.
For example, the user may enter 12:00:00 and 14:00:00. In this case your method would return 7200
since there are 60 seconds per minute, 60 minutes per hour, and this is 2 hours of a difference = 60*60*2
= 7200.
This week's lesson is on exception handling, so you’ll need to handle all the exceptions that could happen
here. Think about what the user could do wrong. Here are a few examples:
25:20:20 - not a valid hour
0:0:89 - not a valid second
14:-2:09 - not a valid minute
12:20 - not valid because they didn't specify seconds
"B" - not a valid time
"In" - ie, they just hit enter, not valid at all
%3D
You'll need to handle all these, but your error should be able to produce at least 4 different errors:
1) Hour not valid
2) Minute not valid
3) Second not valid
4) Time format not valid (Covers "12:20", “B" and “n")
Tasks
Write a method which takes in a string, and returns an integer. The string should be in the format
HH:MM:SS for example 12:20:30
Your method should break the string apart into 3 pieces. In java you might find string.split(“:",3) useful,
in C# string.Split(“:",3)
Convert each number to an int, and validate the sanity of each number. Hours should be 0-23, minutes
and seconds should be 0-59. Anything outside those ranges should throw an exception of type
InvalidTimeException with an appropriate message indicating the issue.
Assuming the given time is valid, calculate the number of seconds since midnight of that time. The
formula is (Hours*60*60) + (Minutes*60) + Seconds. Return that result.
InvalidTimeException should be a class that you define that extends Exception. It should have a
constructor which takes in a string, and calls its parent's constructor with that string.
Finally write a driver program which prompts the user to enter 2 time strings, and calls above method
with the string, gets a number seconds back, and subtracts the second from the first. The main method
Transcribed Image Text:Objectives: In this lab you’ll allow the user to enter in 2 times in 24hr (military) format. You'll calculate how many seconds are between those times. For example, the user may enter 12:00:00 and 14:00:00. In this case your method would return 7200 since there are 60 seconds per minute, 60 minutes per hour, and this is 2 hours of a difference = 60*60*2 = 7200. This week's lesson is on exception handling, so you’ll need to handle all the exceptions that could happen here. Think about what the user could do wrong. Here are a few examples: 25:20:20 - not a valid hour 0:0:89 - not a valid second 14:-2:09 - not a valid minute 12:20 - not valid because they didn't specify seconds "B" - not a valid time "In" - ie, they just hit enter, not valid at all %3D You'll need to handle all these, but your error should be able to produce at least 4 different errors: 1) Hour not valid 2) Minute not valid 3) Second not valid 4) Time format not valid (Covers "12:20", “B" and “n") Tasks Write a method which takes in a string, and returns an integer. The string should be in the format HH:MM:SS for example 12:20:30 Your method should break the string apart into 3 pieces. In java you might find string.split(“:",3) useful, in C# string.Split(“:",3) Convert each number to an int, and validate the sanity of each number. Hours should be 0-23, minutes and seconds should be 0-59. Anything outside those ranges should throw an exception of type InvalidTimeException with an appropriate message indicating the issue. Assuming the given time is valid, calculate the number of seconds since midnight of that time. The formula is (Hours*60*60) + (Minutes*60) + Seconds. Return that result. InvalidTimeException should be a class that you define that extends Exception. It should have a constructor which takes in a string, and calls its parent's constructor with that string. Finally write a driver program which prompts the user to enter 2 time strings, and calls above method with the string, gets a number seconds back, and subtracts the second from the first. The main method
should deal with all exceptions thrown. See sample run below
Sample Output:
Enter time 1 in 24hr format as follows (HH:MM:SS)
12:00:00
Enter time 2 in 24hr format as follows (HH:MM: SS)
16:30:01
Difference in seconds: 16201
****Separate Run****
Enter time 1 in 24hr format as follows (HH:MM:SS)
26:00:00
Hour must be below 24
****Separate Run****
Enter time 1 in 24hr format as follows (HH:MM:SS)
12:20
Enter a valid time
****Separate Run****
Enter time 1 in 24hr format as follows (HH:MM:SS)
12:76:01
Minutes must be less than 60
****Separate Run****
Enter time 1 in 24hr format as follows (HH:MM:SS)
2:15:00
Enter time 2 in 24hr format as follows (HH:MM: SS)
4:20:60
Seconds must be less than 60
****Separate Run****
Enter time 1 in 24hr format as follows (HH:MM:SS)
Enter a valid time
//In this last run, I just hit enter when asked for a time.
****Separate Run****
Enter time 1 in 24hr format as follows (HH:MM:SS)
16:00:00
Enter time 2 in 24hr format as follows (HH:MM: SS)
17:00
Transcribed Image Text:should deal with all exceptions thrown. See sample run below Sample Output: Enter time 1 in 24hr format as follows (HH:MM:SS) 12:00:00 Enter time 2 in 24hr format as follows (HH:MM: SS) 16:30:01 Difference in seconds: 16201 ****Separate Run**** Enter time 1 in 24hr format as follows (HH:MM:SS) 26:00:00 Hour must be below 24 ****Separate Run**** Enter time 1 in 24hr format as follows (HH:MM:SS) 12:20 Enter a valid time ****Separate Run**** Enter time 1 in 24hr format as follows (HH:MM:SS) 12:76:01 Minutes must be less than 60 ****Separate Run**** Enter time 1 in 24hr format as follows (HH:MM:SS) 2:15:00 Enter time 2 in 24hr format as follows (HH:MM: SS) 4:20:60 Seconds must be less than 60 ****Separate Run**** Enter time 1 in 24hr format as follows (HH:MM:SS) Enter a valid time //In this last run, I just hit enter when asked for a time. ****Separate Run**** Enter time 1 in 24hr format as follows (HH:MM:SS) 16:00:00 Enter time 2 in 24hr format as follows (HH:MM: SS) 17:00
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY