In: Computer Science
C#
1. Create an Employee class with two fields: idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, thrown an ArgumentException if the hourlyWage is less than 7.50 or more than 50.00. Write a program that establishes, one at a time, at least three Employees with hourlyWages that are above, below, and within the allowed range. Immediately after each instantiation attempt, handle any thrown Exceptions by displaying an error message. Save the file as EmployeeExceptionDemo.cs.
2. Write an application that creates an array of five Employees. Prompt the user for values for each field for each Employee. If the user enters improper or invalid data, handle any exceptions that are thrown by setting the Employee’s ID number to 999 and the Employee’s pay rate to the $7.50. At the end of the program, display all the entered, and possible corrected, records. Save the file as EmployeeExceptionDemo2.cs.
C# Program:
(1)
using System;
//User defined class
class Employee
{
public int idNum;
public double hourlyWage;
//Constructor
public Employee(int id, double wage)
{
//Storing id
idNum = id;
//Checking wage
if(wage < 7.50 || wage > 50.00)
{
throw new ArgumentException("Wage out of range",
idNum.ToString());
}
else
{
wage = hourlyWage;
}
}
}
class Program
{
static void Main()
{
try
{
//Creating Employee class object
Employee e1 = new Employee(101, 60.0);
Employee e2 = new Employee(102, 40.0);
Employee e3 = new Employee(103, 5.0);
}
catch(ArgumentException ex)
{
Console.Write(ex);
}
}
}
Sample Run:

_________________________________________________________________________________________________
(2)
using System;
//User defined class
class Employee
{
public int idNum;
public double hourlyWage;
//Constructor
public Employee(int id, double wage)
{
//Storing id
idNum = id;
//Checking wage
if(wage < 7.50 || wage > 50.00)
{
throw new ArgumentException("Wage out of range",
idNum.ToString());
}
else
{
hourlyWage = wage;
}
}
}
class HelloWorld
{
static void Main()
{
//Employee Array
Employee[] employees = new Employee[5];
//Iterating over each Employee
for(int i=0; i<5; i++)
{
//Reading values
Console.Write("\nEnter Employee #" + (i+1) + " ID Number: ");
int id = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Employee #" + (i+1) + " Wage: ");
double wage = Convert.ToDouble(Console.ReadLine());
//Handling exceptions
try
{
//Creating an Employee
employees[i] = new Employee(id, wage);
}
catch(ArgumentException ex)
{
employees[i] = new Employee(999, 7.50);
}
}
//Printing records
//Iterating over each Employee
for(int i=0; i<5; i++)
{
//Reading values
Console.Write("\nEmployee #" + (i+1) + " ID Number: " +
employees[i].idNum + ", Wage: $" + employees[i].hourlyWage);
}
Console.Write("\n");
}
}
Sample Run:
