In: Computer Science
This assignment will use the Employee class that you developed for assignment 6. Design two subclasses of Employee…SalariedEmployee and HourlyEmployee. A salaried employee has an annual salary attribute. An hourly employee has an hourly pay rate attribute, an hours worked attribute, and an earnings attribute. An hourly employee that works more than 40 hours gets paid at 1.5 times their hourly pay rate. You will decide how to implement constructors, getters, setters, and any other methods that might be necessary.
Implement the classes, and write a test program that creates a salaried employee and two hourly employees. One of the hourly employees should have hours worked set to less than 40 and one should have hours worked set to more than 40. The test program should display all attributes for the three employees. To keep things simple, the employee classes don’t need to do any editing.
Previous Code:
EmployeeInfo.java
public class Employeeinfo {
public static void main(String[] args) {
int n_emp;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number of
Employee(s)");
n_emp = scanner.nextInt();
Employee array[]=new Employee[n_emp];
int i = 0;
while(i<n_emp)
{
String s1,s2,s3;
//* String for Input
System.out.println("What is Employees First Name?");
s1 =
scanner.next();
System.out.println("What is Employees Last Name?");
s2 =
scanner.next();
Name n = new
Name(s1, s2);
int
i1,i2,i3;
while(true)
{
System.out.println("Please Enter Year Hired:");
i1 =
scanner.nextInt();
if(i1
>= 1900 && i1 <= 2020)
{
break;
}
else
{
System.out.println("Sorry not within Range, Please enter a yearly
range between 1900 - 2020"); //* Error if not in range
}
}
while(true)
{
System.out.println("Please
Enter Month Hired:");
i2 = scanner.nextInt();
if(i2 >= 1 && i2
<= 12)
{
break;
}
else
{
System.out.println("Sorry not
within Range, Please enter a monthly range between 1 - 12");
}
}
while(true)
{
System.out.println("Please
Enter Day Hired:");
i3 = scanner.nextInt();
if(i3 >= 1 && i3
<= 31)
{
break;
}
else
{
System.out.println("Sorry not
within Range, Please enter a daily range between 1 - 31");
}
}
Date d = new Date(i3,i2,i1);
System.out.println("Please
Enter Street Name:");
s1 = scanner.next();
System.out.println("Please
Enter City");
s2 = scanner.next();
System.out.println("Please
Enter State");
s3 = scanner.next();
System.out.println("Please
Enter ZipCode");
i3 = scanner.nextInt();
Address a = new Address(s1, s2, s3, i3);
System.out.println("Please
enter a Employee Number");
i1 = scanner.nextInt();
Employee e =new
Employee(i1,n,d,a);
e.myname.fname = n.fname; //* Array to display
e.myname.lname = n.lname;
e.mydate.month = d.month;
e.mydate.day = d.day;
e.mydate.year = d.year;
e.myadress.street = a.street;
e.myadress.state = a.state;
e.myadress.city = a.city;
e.myadress.zipcode = a.zipcode;
array[i] = e;
i++;
}
System.out.println("List of
Employee(s): "); //Print List
for (i = 0 ; i< n_emp;
i++)
{
System.out.println(
array[i].number + " "+ array[i].myname.fname + " " +
array[i].myname.lname + " | " + array[i].mydate.month + "/" +
array[i].mydate.day + "/" + array[i].mydate.year + " | "+
array[i].myadress.street + " "+ array[i].myadress.state + " "+
array[i].myadress.city + ","+ array[i].myadress.zipcode);
}
}
}
Date.Java
public class Date {
public int month;
public int day;
public int year;
public Date(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
public Date() {
this.month = 0;
this.day = 0;
this.year = 0;
}
}
Employee.java
public class Employee {
public int number;
public Date mydate;
public Address myadress;
public Name myname;
public Employee(int number, Name myname, Date
mydate, Address myadress) {
this.number = number;
this.mydate = new Date();
this.myadress = new Address();
this.myname = new Name();
}
public Employee() {
this.number = number;
this.mydate = new Date();
this.myadress = new Address();
this.myname = new Name();
}
}
Name.Java
public class Name {
public String fname;
public String lname;
public Name(String fname, String lname) {
this.fname = fname;
this.lname = lname;
}
public Name() {
this.fname = "";
this.lname = "";
}
}
Address.Java
public class Address {
public String street ;
public String state ;
public String city;
public int zipcode;
public Address(String street, String state, String
city, int zipcode) {
this.street = street;
this.state = state;
this.city = city;
this.zipcode = zipcode;
}
public Address() {
this.street = "";
this.state = "";
this.city = "";
this.zipcode = 0;
}
}
// Date.java
public class Date {
public int month;
public int day;
public int year;
public Date(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
public Date() {
this.month = 0;
this.day = 0;
this.year = 0;
}
}
//end of Date.java
// Name.java
public class Name {
public String fname;
public String lname;
public Name(String fname, String lname) {
this.fname = fname;
this.lname = lname;
}
public Name() {
this.fname = "";
this.lname = "";
}
}
//end of Name.java
// Address.java
public class Address {
public String street ;
public String state ;
public String city;
public int zipcode;
public Address(String street, String state, String
city, int zipcode) {
this.street = street;
this.state = state;
this.city = city;
this.zipcode = zipcode;
}
public Address() {
this.street = "";
this.state = "";
this.city = "";
this.zipcode = 0;
}
}
//end of Address.java
// Employee.java
public class Employee {
public int number;
public Date mydate;
public Address myadress;
public Name myname;
public Employee(int number, Name myname, Date
mydate, Address myadress) {
this.number = number;
this.mydate = mydate;
this.myadress = myadress;
this.myname = myname;
}
public Employee() {
this.number = 0;
this.mydate = new Date();
this.myadress = new Address();
this.myname = new Name();
}
// method to display the details of the Employee
public void display()
{
System.out.println("Number:
"+number);
System.out.println("Name:
"+myname.fname+" "+myname.lname);
System.out.println("Data:
"+mydate.month+"/"+mydate.day+"/"+mydate.year);
System.out.println("Address:
"+myadress.street+" "+myadress.city+", "+myadress.state+",
"+myadress.zipcode);
}
}
// end of Employee.java
// SalariedEmployee.java
public class SalariedEmployee extends Employee
{
public double salary;
// parameterized constructor
public SalariedEmployee(int number, Name myname, Date
mydate, Address myadress, double salary)
{
super(number, myname, mydate,
myadress); // call Employee's constructor
this.salary = salary;
}
// default constructor
public SalariedEmployee()
{
super();
this.salary = 0;
}
// override Employee's display method to display the
additional details
public void display()
{
super.display();
System.out.printf("Salary:
$%,.2f\n",salary);
}
}
//end of SalariedEmployee.java
// HourlyEmployee.java
public class HourlyEmployee extends Employee
{
public double pay_rate;
public int hours_worked;
public double earnings;
// parameterized constructor
public HourlyEmployee(int number, Name myname, Date
mydate, Address myadress, double pay_rate, int hours_worked)
{
super(number, myname, mydate,
myadress);
this.pay_rate = pay_rate;
this.hours_worked =
hours_worked;
// calculate earnings
if(hours_worked <= 40) // no
overtime
earnings =
this.pay_rate*this.hours_worked;
else // overtime
earnings =
this.pay_rate*40 + (this.hours_worked-40)*1.5*this.pay_rate;
}
// default constructor
public HourlyEmployee()
{
super();
pay_rate = 0;
hours_worked = 0;
earnings = 0;
}
// override display method
public void display()
{
super.display();
System.out.printf("Pay rate:
$%,.2f\n",pay_rate);
System.out.println("Hours Worked:
"+hours_worked);
System.out.printf("Earnings:
$%,.2f\n",earnings);
}
}
//end of HourlyEmployee.java
// Employeeinfo.java
public class Employeeinfo {
public static void main(String[] args)
{
// create SalariedEmployee
SalariedEmployee s = new
SalariedEmployee(12, new Name("Shaun","Marsh"), new Date(11, 7,
1995), new Address("Street1","State1","City1",70081), 75000);
// create HourlyEmployee without
any overtime
HourlyEmployee h1 = new
HourlyEmployee(15, new Name("Harry","Doe"), new Date(7, 16, 2000),
new Address("Street2","State2","City2",60181), 45.75, 35);
// create HourlyEmployee with
overtime
HourlyEmployee h2 = new
HourlyEmployee(25, new Name("Jerry","Hope"), new Date(10, 16,
2007), new Address("Street3","State3","City3",80111), 45.75,
45);
// display the details
s.display();
System.out.println();
h1.display();
System.out.println();
h2.display();
}
}
//end of Employeeinfo.java
Output: