Create an Employee class with two fields: IDNum and hourlyWage . The constructor
ID: 3534858 • Letter: C
Question
Create an Employee class with two fields: IDNum and hourlyWage.
The constructor for Employee will accept two arguments for these two fields.
When the Employee class is instantiated you will throw an ArgumentException if the hourlyWage is less than 6.00 or more than 50.00.
Handle any thrown Exceptions in the Employee class by displaying an error message.
In Main instantiate an array of five (5) Employee objects.
Prompt the user for the values of the two fields in each Employee object.
Handle any exceptions (try-catch block) that are thrown by setting the Employee's ID number to 999 and the Employee's pay rate to the $6.00 minimum using theEmployee constructor in the catch so that the message can be displayed from the class object by way of the ArgumentException.
At the end of input display all the entered and possibly corrected records.
The following is an example of possible output:
Enter ID number x
Input string was not in a correct format.
Enter ID number 1
And hourly salary 8.00
Enter ID number 2
And hourly salary xefe
Input string was not in a correct format.
Enter ID number 3
And hourly salary 4.00
Value does not fall within the expected range.
Enter ID number 4
And hourly salary 60.00
Value does not fall within the expected range.
Employee # 999 pay rate $6.00
Employee # 1 pay rate $8.00
Employee # 999 pay rate $6.00
Employee # 999 pay rate $6.00
Employee # 999 pay rate $6.00
Press any key to continue . . .
Explanation / Answer
Please rate with 5 stars :)
public class WageCalculate
{
private String employeeName;
private int hours;
private double rate, pay;
public void setEmployeeName ( String name )
{
employeeName = name;
}
public String getEmployeeName()
{
return employeeName;
}
public double calculatePay( int hours, double rate )
{
if ( hours > 40 )
{
int extraHours = hours - 40;
pay = ( 40 * rate ) + ( extraHours * rate );
}
else pay = hours * rate;
return pay;
}
public void displayEmployee()
{
System.out.printf( "Employee's name: %s", getEmployeeName() );
System.out.printf( " Gross Salary: ", + pay );
}
}
The Employee.java
import java.util.Scanner;
public class Employee
{
public static void main(String[] args)
{
Scanner input = new Scanner( System.in);
WageCalcu employee = new WageCalcu();
System.out.print( "Enter Employee %s name: " );
String name = input.nextLine();
employee.setEmployeeName( name );
System.out.print( "Enter how many hours worked: " );
int hours = input.nextInt();
System.out.print( "Enter hourly rate: " );
double rate = input.nextInt();
employee.calculatePay( hours, rate );
employee.displayEmployee();
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.