In: Computer Science
(in C# please.) EXCEPTION HANDLING Concept Summary:
1. Use of try… catch in a program
2. Define an exception class and call it from the driver program.
For this lab you will complete two parts.
Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java.
Write a program that implements an ArrayIndexOutOfBounds error.
Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation.
Assume the user will enter the time as a 4-digit number with no colon.
Submission Guidelines: You will turn in 3 program files. Part (a) has one program file Part (b) requires two program files. (Note: C# Students can turn in one program file)
Specifications: Part (a): Write a program in Java or C# that declares an array of 5 elements and displays the contents of the array. Your program should attempt to access the 6th element in the array (which does not exist) and using try.. catch your program should prevent the run-time error and display your error message to the user. Sample output including error message is provided below. SAMPLE OUTPUT: Part (a) Printing an element out of bounds 5 7 11 3 0 You went out of limits in the array
Part (b): Define an exception class called InvalidTimeFormatException. If the user enters an invalid time like 1065 or 2515, the program should throw and handle an InvalidTimeFormatException. NOTE: Assume the user will enter the time as a 4-digit number with no colon.
SAMPLE OUTPUT:
Part (b) Enter time in 24-hour notation: 1614
That is the same as 4:14 PM Again? (y/n) y
Enter time in 24-hour notation: 0245
That is the same as 2:45 AM Again? (y/n) y
Enter time in 24-hour notation: 1215
That is the same as 12:15 PM Again? (y/n) y
Enter time in 24-hour notation: 2612 Error: Hour can only be between 0 and 23 Again? (y/n) y
Enter time in 24-hour notation: 1562 Error: Minutes can only be between 0 and 59 Again? (y/n) n
Below is the solution:
using System;
namespace ExceptionHandling
{
class Program
{
static void
Main(string[] args)
{
int[] num = new int[5] {5,7,11,3,0};
try
{
for(int i=0; i<=num.Length; i++) //loop runs 6 times
{
Console.Write(num[i]+ " ");
}
}
catch (IndexOutOfRangeException ex) //print an error
{
Console.WriteLine("\nYou went out of limits in the array");
}
int hour, minute, number; //declare variable
String AmPm = "";
char option = 'y';
try
{
do
{ //loop util user user enter n
//read number
Console.Write("\nEnter time in 24 - hour notation:");
number = Convert.ToInt32(Console.ReadLine());
hour = number / 100;
//check for the 24 hour
if (hour > 24 || hour < 0)
{
Console.Write("Error: Hour can only be between 0 and 23 Again?
(y/n) ");
option = Console.ReadLine()[0];
}
else
{
if (hour > 0 || hour <= 12)
AmPm = "AM";
else
AmPm = "PM";
}
minute = number % 100;
//check for the 60 minute
if (minute > 60 || minute < 0)
{
Console.Write("Error: Minutes can only be between 0 and 59 Again?
(y/n) ");
option = Console.ReadLine()[0];
}
else
{
//print
Console.Write("That is the same as " + hour + ":" + minute + " "
+AmPm+" Again? (y/n) ");
option = Console.ReadLine()[0];
}
} while (option == 'y');
}
catch (InvalidTimeZoneException ex) //print an error
{
Console.WriteLine(ex.ToString());
}
Console.ReadKey();
}
}
}
sample output:
5 7 11 3 0
You went out of limits in the array
Enter time in 24 - hour notation:1614
That is the same as 16:14 PM Again? (y/n) y
Enter time in 24 - hour notation:0245
That is the same as 2:45 AM Again? (y/n) y
Enter time in 24 - hour notation:1215
That is the same as 12:15 PM Again? (y/n) y
Enter time in 24 - hour notation:2612
Error: Hour can only be between 0 and 23 Again? (y/n) y
Enter time in 24 - hour notation:1562
Error: Minutes can only be between 0 and 59 Again? (y/n) n