Question

PLEASE I NEED C# Objectives Learn the basics of exception handling. Background Exceptions are essentially unexpected...

PLEASE I NEED C#

Objectives

  • Learn the basics of exception handling.

Background

Exceptions are essentially unexpected situations. It is difficult to write a program that can handle all possible situations, as we have found out through the many programs we have written. For example, should your program accept accidental input? Does it use the metric system or the empirical system? Do users of your program know which system it uses? In order to deal with all these possibilities, exceptions were created to allow programs to recover from these awkward situations. After this lab, your program will never look silly at a party again!

Tasks

This lab has two parts:

  1. Create a TimeException class.
  2. Create a program that continually asks the user for a 24-hour time and uses the TimeException if the time is too large or small.

Task 1 – TimeException Class

Create a TimeException class by creating a new class that extends from the built-in exception class. The only additional step is to create a parameterized constructor that takes in a string which will be the error message given back to the user.

Task 2 – Main Program

Create a Tester Class, with a Main method. In the Main method, create a loop that will continue to execute until the user enters “n”, otherwise the loop should continue. Each iteration of the loop will begin by asking the user to enter a time in the 24-hour format, including the colon “:”. That means that numbers given from zero (0) to twenty-three (23) will be appropriate for the hours place. The minutes will still be in the same range, going from zero (0) to fifty-nine (59).

The next step in this loop will be to convert the given time from a string (which includes “:”) to a number format (which does not include “:”). At this point, please use the already created FormatException (in C#) to handle cases where the user did not enter a number. If an exception does occur, please print the relevant error message using the Exception class’s StackTrace (in C#), as well continuing the loop to the next iteration and starting the process over again by asking the user for another 24-hour time.

After that the following step is reduce the number of hours if they are over twelve (12) so that we can see the equivalent 12-hour time. There is also a special case for the hour zero (0), which is our twelve (12) or midnight hour. Here is where we should throw and catch our TimeException if the number of hours is twenty-four (24) or over, or if it is under zero (0). We should also use this TimeException if the number of minutes is over fifty-nine (59) or under zero (0). If an exception does occur, print the relevant error message using the same method above, after which please continue the loop to the next iteration and start the process over again as well.

Finally, we print out the converted hours along with “AM” or “PM”, and then ask the user if they wish to continue and start the loop over again.

∃ Some Sample Output:

Enter a time in 24-hour format: 14:15

Same time in 12-hour format: 2:15 PM

Continue? y

Enter a time in 24-hour format: 20:50

Same time in 12-hour format: 8:50 PM

Continue? y

Enter a time in 24-hour format: 25:50

The number of hours was not valid, try again.

Enter a time in 24-hour format: 2:80

The number of minutes was not valid, try again.

Enter a time in 24-hour format: 2:30

Same time in 12-hour format: 2:30 AM

Continue? Y

Enter a time in 24-hour format: 0:05

Same time in 12-hour format: 12:05 AM

Continue? N

Program has finished!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

TimeException class

class TimeException : Exception
{
public TimeException(String message)
: base(message)
{

}
}

Main class

class Program
{
static void Main(string[] args)
{
String userChoice,time;
int hour, min;

do
{
Console.Write("Enter a time in 24-hour format:");
time = Console.ReadLine();
try
{

hour = Convert.ToInt32(time.Substring(0, time.IndexOf(":")));
min = Convert.ToInt32(time.Substring(time.IndexOf(":") + 1));
try
{
converTime(hour, min);
}
catch (TimeException e)
{
Console.WriteLine(e.Message);
}
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
Console.Write("Continue? ");
userChoice = Console.ReadLine();
} while (userChoice == "y");
Console.WriteLine("Program has finished!");
}

private static void converTime(int hour, int min)
{
string ampm;
if (hour < 0 || hour > 23)
throw new TimeException("The number of hours was not valid, try again.");
if (min <= 0 || min > 59)
throw new TimeException("The number of minutes was not valid, try again.");

if (hour <= 12)
ampm = "AM";
else
ampm = "PM";

if (hour == 0)
hour = 12;
else if (hour > 12)
hour -= 12;

Console.WriteLine("Same time in 12-hour format:{0}:{1:D2} {2}", hour, min,ampm);
}
}

output

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
PLEASE I NEED C# Objectives Learn the basics of exception handling. Background Exceptions are essentially unexpected...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling...

    Assignment 11 – Exceptions, Text Input/Output - Random Numbers Revised 9/2019 Chapter 12 discusses Exception Handling and Input/Output. Using try/catch/finally statement to handle exceptions, or declare/throw an exception as needed, create the following program: Create an array of 25 random numbers between 0 & 250 formatted to a field width of 10 & 4 decimal places (%10.4f). Use the formula Math.random() * 250. Display the array of random numbers on the console and also write to a file. Prompt the...

  • Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to...

    Lesson is about Input validation, throwing exceptions, overriding toString Here is what I am supposed to do in the JAVA Language: Model your code from the Time Class Case Study in Chapter 8 of Java How to Program, Late Objects (11th Edition) by Dietel (except the displayTime method and the toUniversalString method). Use a default constructor. The set method will validate that the hourly employee’s rate of pay is not less than $15.00/hour and not greater than $30.00 and validate...

  • 12.5 Program: Divide by Zero (C++) This program will test exception handling during division. The user...

    12.5 Program: Divide by Zero (C++) This program will test exception handling during division. The user will enter 2 numbers and attempt to divide (in a try block) the 1st number by the 2nd number. The program will provide for 2 different catch statements: Division by zero - denominator is zero Division results in a negative number The user should be prompted with Enter 2 numbers: For input of 7 and 0 the output should be Exception: Division by zero...

  • Chapter 9 Lab Text Processing and Wrapper Classes Lab Objectives ? Use methods of the Character...

    Chapter 9 Lab Text Processing and Wrapper Classes Lab Objectives ? Use methods of the Character class and String class to process text ? Be able to use the StringTokenizer and StringBuffer classes Introduction In this lab we ask the user to enter a time in military time (24 hours). The program will convert and display the equivalent conventional time (12 hour with AM or PM) for each entry if it is a valid military time. An error message will...

  • Need some assistance of reorganizing this whole program. I have the right code for everything I...

    Need some assistance of reorganizing this whole program. I have the right code for everything I just need help on putting all the codes in the right spot so it can come out to the correct output. output is supposed to look like this: 1 \\ user inputs choice to convert 12 to 24 8 \\ user inputs 8 for hours 30 \\ user inputs 30 for minutes 20 \\ user inputs 20 for seconds AM \\ user inputs AM...

  • Java Store Time Clock Objects

    1. Create a Time class which is designed to contain objects representing times in 12-hour clock format. Eg: 6:58am, 11:13pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples.Provide a default constructor, a set method which is given a String (eg: “9:42am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night” appropriate...

  • Write a program that reads a string from the keyboard and tests whether it contains a...

    Write a program that reads a string from the keyboard and tests whether it contains a valid time. Display the time as described below if it is valid, otherwise display a message as described below. The input date should have the format hh:mm:ss (where hh = hour, mm = minutes and ss = seconds in a 24 hour clock, for example 23:47:55). Here are the input errors (exceptions) that your program should detect and deal with: Receive the input from...

  • 1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock...

    1. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a boolean isAm method. Also provide a method for returning a string “morning”, “afternoon”, “evening” or “night”...

  • What to submit: your answers to exercises 1, and 2 and separate the codes to each...

    What to submit: your answers to exercises 1, and 2 and separate the codes to each question. Create a MyTime class which is designed to contain objects representing times in 12-hour clock format. Eg: 7:53am, 10:20pm, 12:00am (= midnight), 12:00pm (= noon). You can assume the user will enter times in the format given by the previous examples. Provide a default constructor, a set method which is given a String (eg: “7:53am”), a getHours method, a getMinutes method and a...

  • C++ Please! Given this definition of a Time class: class Time { private: int hour, min,...

    C++ Please! Given this definition of a Time class: class Time { private: int hour, min, sec; public: Time() { hour = 0; min = 0; sec = 0; } Time(int h, int m, int s) {      hour = h; min = m; sec = s; } int getHour() const {     return hour; } int getMin() const {     return minute; } int getSec() const {     return sec; } }; Derive a class MilTime from the Time...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT