Write exception classes for the following error conditions: 1) An empty string i
ID: 3538177 • Letter: W
Question
Write exception classes for the following error conditions:
1) An empty string is given for the employee name
2) An invalid value is given for the employees ID number. A negative number or zero would be invalid.
3) An invalid number is given for the number of hours worked. This would be a negative number or a number greater than 84.
4) An invalid number is given for the hourly pay rate. This would be a negative number or a number greater than 25.
Demonstrate the classes in a program
The following is a payroll class (without the exceptions):
public class PayrollClass
{
private int NumEmployees= 7;
int [] EmployeeID = {5658845, 4520125 ,7895122, 8777541, 8451277,1302850,7580489};
private int [] hours = new int[NumEmployees];
private double [] PayRate = new double[NumEmployees];
private double [] wage = new double[NumEmployees];
//set
//employee id
public void setEmployeeID(int index, int value)
{
EmployeeID[index] = value;
}
//hours
public void setHours(int index, int value)
{
hours[index] = value;
}
//pay rate
public void setPayRate(int index, double value)
{
PayRate[index] = value;
}
//wages
public void setWage(int index, double value)
{
wage[index] = value;
}
//get
//employee id
public int getEmployeeID (int index)
{
return EmployeeID[index];
}
//hours
public int getHours (int index)
{
return hours[index];
}
//payrate
public double getPayRate (int index)
{
return PayRate[index];
}
//wages
public double getWage (int index)
{
return wage[index];
}
//calculate employee wage
public double CalculateWage (int index)
{
double TotalWage = 0;
TotalWage =getHours (index) * getPayRate (index);
return TotalWage;
}
}
The following is a program used (without the exceptions):
import java.util.Scanner ;
public class PayrollProgram
{
public static void main (String[] args)
{
// variables
int i;
//allows for user input
Scanner keyboard = new Scanner (System.in);
PayrollClass employee = new PayrollClass();
for (i = 1; i ;>
{
System.out.println ("Employee #" + employee.EmployeeID[i]);
//asks for hours
System.out.print("Enter employee's hours:");
employee.setHours (i,keyboard.nextInt() );
//asks for pay
System.out.print("Enter employee's pay:");
employee.setPayRate (i,keyboard.nextDouble() );
}
for( i=1; i < employee.EmployeeID.length; i++)
{
System.out.println(" Employee # " + employee.EmployeeID[i] + " gross wages is:$" + employee.CalculateWage(i));
}
Explanation / Answer
import javax.swing.JOptionPane;
class Payroll Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.