In: Computer Science
JAVA PROGRAMMING.
In this assignment, you are to create a class named Payroll.
In the class, you are to have the following data members:
name: String (5 pts)
id: String (5 pts)
hours: int (5 pts)
rate: double (5 pts)
private members (5 pts)
You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts)
The class definition should also handle the following exceptions:
An employee name should not be empty, otherwise an exception should be thrown. (10 pts)
An employee id should have the form LLNNNN. If that form is not received, an exception should be thrown. (10 pts)
An employee's hours should neither be negative nor greater than 84. An exception should be thrown otherwise. (10 pts)
An employee's pay rate should neither be negative nor greater than 25.00. An exception should be thrown otherwise. (10 pts)
Demonstrate this class in a program (separate class or in the same class). (5 pts)
The exception messages should be appropriate to the specific exception that has occurred. (5 pts)
Create a package payroll for the project. (5 pts)
Sample Output:
CODE TO COPY:
File: Payroll.java
// Payroll class implementation
package payroll;
public class Payroll
{
// private members
private String name;
private String id;
private int hours;
private double rate;
// no-arg constructor implementation
public Payroll()
{
name = "";
id = "";
hours = 0;
rate = 0.0;
}
// parameterized constructor implementation
public Payroll(String aname, String aid, int ahours,
double arate)
{
setName(aname);
setId(aid);
setHours(ahours);
setRate(arate);
}
// setters
// setName method implementation
public void setName(String aname)
{
if(aname.equals(""))
{
throw new
IllegalArgumentException("Employee's name should not be
empty!");
}
name = aname;
}
// setId method implementation
public void setId(String aid)
{
if(aid.length() != 6 ||
!Character.isLetter(aid.charAt(0)) ||
!Character.isLetter(aid.charAt(1))
|| !Character.isDigit(aid.charAt(2)) ||
!Character.isDigit(aid.charAt(3))
|| !Character.isDigit(aid.charAt(4)) ||
!Character.isDigit(aid.charAt(5)))
{
throw new
IllegalArgumentException("Employee's id should have the form
LLNNNN!");
}
id = aid;
}
// setHours method implementation
public void setHours(int ahours)
{
if(ahours < 0 || ahours >
84)
{
throw new
IllegalArgumentException("Employee's hours should neither be
negative nor greater than 84!");
}
hours = ahours;
}
// setRate method implementation
public void setRate(double arate)
{
if(arate < 0 || arate >
25.00)
{
throw new
IllegalArgumentException("Employee's pay rate should neither be
negative nor greater than 25.00!");
}
rate = arate;
}
// getters
// getName method implementation
public String getName()
{
return name;
}
// getId method implementation
public String getId()
{
return id;
}
// getHours method implementation
public int getHours()
{
return hours;
}
// getRate method implementation
public double getRate()
{
return rate;
}
} // end of Payroll class
File: PayrollDemo.java
// PayrollDemo class implementation
package payroll;
public class PayrollDemo
{
// start main method
public static void main(String[] args)
{
// create an object for the Payroll
class
Payroll pr = new Payroll();
// test the setter method
pr.setName("Joseph Li");
pr.setId("JL3699");
pr.setHours(36);
pr.setRate(18.36);
// test the getter method
System.out.println("Detailes of
Payroll...");
System.out.println("Name: " +
pr.getName());
System.out.println("ID: " +
pr.getId());
System.out.println("Hours: " +
pr.getHours());
System.out.println("Pay rate: " +
pr.getRate());
} // end of main method
} // end of PayrollDemo class