Create two classes - \'FullTimeEmployee\' and \'PartTimeEmployee\', which inheri
ID: 3801472 • Letter: C
Question
Create two classes - 'FullTimeEmployee' and 'PartTimeEmployee', which inherit from a superclass 'Employee'. Write a test program to test the methods you write for the two classes.
In FullTimeEmployee, include the following variables and methods, other than all instance variables and methods inherited from class Employee:
Private instance variable weeklySalary;
A constructor takes four inputs (firstname, lastname, SSN, and salary);
One additional getter method to return the instance variable (accessor);
One setter method to set the instance variable (mutator);
One overriding method 'earnings()' that returns the weekly earnings (same as the getSalary() method);
A method toString that converts a fulltime employee's information into string form (including firstname, lastname, ssn, earning, and class name).Override superclass toString() method.
In the second file 'PartTimeEmployee.java', include the following additional instance variables and methods (other than all instance variables and methods inherited from class Employee):
Private instance variables 'wage' (wage per hour) and 'hours' (hours worked for week);
A constructor takes five inputs: 'firstname', 'lastname', 'SSN', 'hours', and 'wage';
Two additional getter methods to return the instance variables (accessor);
Two setter methods to set the instance variables (mutator);
One overriding method 'earnings()' that returns the earnings based on hours and wage. For hours in excess of 40 hours, the pay will be time-and-a-half.
A method 'toString' that converts a parttime employee's information into string form (including firstname, lastname, ssn, earnings, and class name). Override superclass toString() method.
In the third file 'EmployeeTester.java', do the following:
Create an array of Employee type, with the array size of 10. Use this array to store the references for subclass objects (FullTimeEmployee, PartTimeEmployee).
Initialize each object of the array using the values from the file of "data.txt" (Read the values in from the file using the Scanner object, do not initialize them manually).
The first value in each line of the file could be 1 (FullTimeEmployee) or 2 (PartTimeEmployee), which will be used to determine which kind of object you will initialize. The reamining values of the lines will be used to initialize each instance variable of that particular type of the object.
Print out the contents of each element (i.e., object) in the array using toString() method.
This program needs to be in Java.
Explanation / Answer
public class FullTimeEmployee extends Employee{
private double weeklySalary;
FullTimeEmployee(String firstname,String lastname, String ssn,double salary){
this.firstName = firstname;
this.lastName = lastname;
this.ssn = ssn;
this.weeklySalary = salary;
}
public double getWeeklySalary() {
return weeklySalary;
}
public void setWeeklySalary(double weeklySalary) {
this.weeklySalary = weeklySalary;
}
public String toString(){
return this.getFirstName()+" "+this.getLastName()+" "+this.getWeeklySalary()+" "+this.getSsn();
}
}
public class PartTimeEmployee extends Employee{
private double wage;
private int hours;
public double getWage() {
return wage;
}
public void setWage(double wage) {
this.wage = wage;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public PartTimeEmployee(String firstname, String lastname,String ssn, double wage,int hours){
this.firstName = firstname;
this.lastName = lastname;
this.ssn = ssn;
this.wage = wage;
this.hours = hours;
}
public String toString(){
return this.getFirstName()+" "+this.getLastName()+" "+this.getSsn()+" "+(double)(this.getWage() * this.getHours());
}
}
public class Employee {
String firstName;
String lastName;
String ssn;
double salary;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSsn() {
return ssn;
}
public void setSsn(String ssn) {
this.ssn = ssn;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
import java.io.BufferedReader;
import java.io.FileReader;
public class EmployeeTester {
private static final String FILENAME = "E:\test\filename.txt"; // path of the file to read
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee emp[] = new Employee[10];
BufferedReader br = null;
int count =0;
FileReader fr = null;
try {
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
br = new BufferedReader(new FileReader(FILENAME));
while ((sCurrentLine = br.readLine()) != null) {
//assuming the content of the file is like below
//FullTimeEmployee first_name last_name ssn salary
// or
//PartTimeEmployee first_name last_name ssn wage hours
String str[] = sCurrentLine.split(" ");
if(str[0].equals("FullTimeEmployee")){
emp[count] = new FullTimeEmployee(str[1], str[2], str[3], Double.parseDouble(str[4]));
count++;
}
else if(str[0].equals("PartTimeEmployee")){
emp[count] = new PartTimeEmployee(str[1], str[2], str[3], Double.parseDouble(str[4]), Integer.parseInt(str[5]));
count++;
}
else{
}
}
for(int i=0;i<emp.length;i++){
System.out.println(emp[i].toString());
System.out.println();
}
}
catch(Exception e){
System.out.println(e);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.