ShiftSupervisor Class Design a class named Employee. this class should keep the
ID: 3836699 • Letter: S
Question
ShiftSupervisor Class
Design a class named Employee. this class should keep the following information in fields:
Employee name
Employee number in the format XXX-L, where each X is a digit within the range 0-9 and the L is a letter within the range A-M.
Hire date
Write one or more constructors and appropriate accessor and mutator methods for the class.
A shift supervisor is a salaried employee who supervises a shift. In addition to salary, the shift supervisor earns a yearly bonus whtn his or her shift meets the production goals. Design a ShiftSupervisor class that extends the Employee class you created above. The ShiftSupervisor class should have a field that holds the annual salary and a field that holds the annual production bonus that the shift supervisor has earned. Write one or more constructors and the appropriate accessor (getters) and mutator (setters) methods for the class.
Demonstrate the class by writing a program that uses a ShiftSupervisor object.
Sample Output:
Here's the first shift supervisor.
Name: John Smith
Employee Number: 123-A
Hire Date: 11-15-2005
Annual Salary: $48,000.00
Production Bonus: $6,500.00
Here's the second shift supervisor.
Name: Joan Jones
Employee Number: 222-L
Hire Date: 12-12-2005
Annual Salary: $55,000.00
Production Bonus: $8,000.00
Press any key to continue . . .
Explanation / Answer
Employee.java
public class Employee
{
//Declaring instance variables
private String name;
private String date;
private String empnumber;
//Zero argumented constructor
public Employee()
{
name = " ";
date = " ";
empnumber = " ";
}
//Parameterized constructor
public Employee(String name, String date, String empnumber)
{
this.name = name;
this.date = date;
setEmpnumber(empnumber);
}
//getters and setters
public String getName()
{
return name;
}
public String getDate()
{
return date;
}
public String getNumber()
{
return empnumber;
}
public void setEmpnumber(String empnumber) {
if (empnumber.length() != 5)
{
throw new IllegalArgumentException("InvalidEmployeeNumber");
}
else if ((!Character.isDigit(empnumber.charAt(0))) ||
(!Character.isDigit(empnumber.charAt(1))) ||
(!Character.isDigit(empnumber.charAt(2))) ||
(empnumber.charAt (3) != '-') ||
(Character.toUpperCase(empnumber.charAt(4)) < 'A') ||
(Character.toUpperCase(empnumber.charAt(4)) > 'M'))
{
throw new IllegalArgumentException("InvalidEmployeeNumber");
}
else
{
this.empnumber = empnumber;
}
}
//toString method is used to display the contents of an object inside it
public String toString()
{
return "Name: " + name + " Employee Number: " + empnumber + " Hire Date: " + date;
}
}
_________________________
ShiftSupervisor.java
public class ShiftSupervisor extends Employee {
//Declaring instance variables
private double ann_salary;
private double ann_production_bonus;
//Parameterized constructor
public ShiftSupervisor(String name, String date, String empnumber,
double ann_salary, double ann_production_bonus) {
super(name, date, empnumber);
this.ann_salary = ann_salary;
this.ann_production_bonus = ann_production_bonus;
}
//getters and setters
public double getAnn_salary() {
return ann_salary;
}
public void setAnn_salary(double ann_salary) {
this.ann_salary = ann_salary;
}
public double getAnn_production_bonus() {
return ann_production_bonus;
}
public void setAnn_production_bonus(double ann_production_bonus) {
this.ann_production_bonus = ann_production_bonus;
}
//toString method is used to display the contents of an object inside it
@Override
public String toString() {
return super.toString()+" Annual salary=$" + ann_salary
+ " Annual Production Bonus=$" + ann_production_bonus;
}
}
_____________________
Driver.java
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
//Declaring variables
String name;
String date;
String empnumber;
double annSal,annBonus;
//Creating the references of ShiftSupervisor class
ShiftSupervisor ss1=null,ss2=null;
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//This loop continues to execute until the user enters a valid employee number
while(true)
{
//Getting the inputs entered by the user
System.out.print(" Enter Employee no :");
empnumber=sc.next();
sc.nextLine();
System.out.print("Enter Employee Name :");
name=sc.nextLine();
System.out.print("Enter Joining Date :");
date=sc.next();
System.out.print("Enter Annual Salary :$");
annSal=sc.nextDouble();
System.out.print("Enter Annual Production Bonus:$");
annBonus=sc.nextDouble();
try {
//Creating an Object to ShiftSupervisor class by passing the user entered inputs
ss1=new ShiftSupervisor(name, date, empnumber,annSal,annBonus);
break;
}
catch(Exception e)
{
System.out.println("Exception :"+e);
continue;
}
}
//Displaying the info
System.out.println(" Here's is the first shift supervisor.");
System.out.println(ss1.toString());
//This loop continues to execute until the user enters a valid employee number
while(true)
{
//Getting the inputs entered by the user
System.out.print(" Enter Employee no :");
empnumber=sc.next();
sc.nextLine();
System.out.print("Enter Employee Name :");
name=sc.nextLine();
System.out.print("Enter Joining Date :");
date=sc.next();
System.out.print("Enter Annual Salary :");
annSal=sc.nextDouble();
System.out.print("Enter Annual Production Bonus:");
annBonus=sc.nextDouble();
try {
//Creating an Object to ShiftSupervisor class by passing the user entered inputs
ss2=new ShiftSupervisor(name, date, empnumber,annSal,annBonus);
break;
}
catch(Exception e)
{
System.out.println("Exception :"+e);
continue;
}
}
//Displaying the info
System.out.println(" Here's is the first shift supervisor.");
System.out.println(ss2.toString());
}
}
______________________
Output:
Enter Employee no :123Z
Enter Employee Name :Williams
Enter Joining Date :06/06/2006
Enter Annual Salary :$48000
Enter Annual Production Bonus:$6500
Exception :java.lang.IllegalArgumentException: InvalidEmployeeNumber
Enter Employee no :123-A
Enter Employee Name :Williams
Enter Joining Date :11-15-2005
Enter Annual Salary :$48000
Enter Annual Production Bonus:$6500
Here's is the first shift supervisor.
Name: Williams
Employee Number: 123-A
Hire Date: 11-15-2005
Annual salary=$48000.0
Annual Production Bonus=$6500.0
Enter Employee no :123A
Enter Employee Name :Joan Jones
Enter Joining Date :12-12-2005
Enter Annual Salary :55000
Enter Annual Production Bonus:8000
Exception :java.lang.IllegalArgumentException: InvalidEmployeeNumber
Enter Employee no :123-A
Enter Employee Name :Joan Jones
Enter Joining Date :12-12-2005
Enter Annual Salary :55000
Enter Annual Production Bonus:8000
Here's is the first shift supervisor.
Name: Joan Jones
Employee Number: 123-A
Hire Date: 12-12-2005
Annual salary=$55000.0
Annual Production Bonus=$8000.0
_______________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.