could you please provide the java code for this. I really need help please. The
ID: 3813251 • Letter: C
Question
could you please provide the java code for this. I really need help please. The skeleton is provided in the bottom.
The following tasks are to done in the indicated classes:
Task #0 – (in the SalariedEmployee class and the HourlyEmployee class)
Fix the error due to extending the Employee superclass
Note: the method for HourlyEmployee needs to get hours to compute weekly pay.
Create the toString() method
Task #1 - Define an arrayList, myEmployees, of Employee type (in the EmployeeDemo class)
Task #2: (in the EmployeeDemo class)
Add a try-catch block to handle FileNotFound & any general Exception
Add a finally clause, but check that the file was created before closing it
Task #3: (in the EmployeeDemo class)
Open the input file, stored in the project's directory
Use a try-catch-finally block to handle the fileNotFound condition
Within the try-catch block:
read the salaryEmp.txt file until the end of the file
(firstName, lastName, annualSalary)
for each record read, create a SalariedEmployee object, and put inside arrayList of myEmployees
read the hourlyEmp.txt file until the end of the file
for each record read, create an HourlyEmployee object, and put inside myEmployees arrayList
(firstName, lastName, wage)
Task #4: (in the EmployeeDemo class)
Write a loop that, for each employee, will polymorphically display the employee’s data, ask the user for any missing data (hours), and then polymorphically calculate weeklyPay and display the pay.
package employeedemo;
public abstract class Employee {
private String firstName;
private String lastName;
// private double hoursWorked;
public Employee()
{
firstName = null;
lastName = null;
}
public Employee(String aFirstName, String aLastName)
{
firstName = aFirstName;
lastName = aLastName;
// hoursWorked = anHoursWorked;
}
public void setFirstName(String aFirstName)
{
firstName = aFirstName;
}
public void setLastName(String aLastName)
{
lastName = aLastName;
}
/* public void setHoursWorked(double anHours)
{
hoursWorked = anHours;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
/* public double getHoursWorked()
{
return hoursWorked;
}
*/
//Task 0:
//Define .toString()
public abstract double weeklyPay(int hours);
}
public class EmployeeDemo {
//Define an arrayList of Employee type.
ArrayList<Employee> myEmps = new ArrayList<Employee>();
public static void main(String[] args)
{
Scanner myKeyboard = new Scanner(System.in);
int hoursWorked;
EmployeeDemo myDemo = new EmployeeDemo();
myDemo.createEmployees();
myDemo.computeSalary();
}
public void createEmployees()
{
// Please write code below, and place all objects inside of an arrayList
String lastName, firstName;
double salary
SalariedEmployee salEmp;
Scanner inFile = null;
File myfile;
boolean fileOpened = false;
//read the file1 that contains only SalariedEmployees
//create SalariedEmployees subclasses, and add them to the Employee arrayList
//read the file2 that contains only HourlyEmployees
//create HourlyEmployees subclass, and add them to the Employee arrayList
}
public void computeSalary()
{
// loop through the arrayList, and call the polymorphic method found in the superclass
// and all of its subclasses. Print the results of the method's calculations
Scanner keyboard = new Scanner(System.in);
int hours;
double pay;
}
}
package employeedemo;
public class HourlyEmployee extends Employee {
private double wage;
public HourlyEmployee(String firstName, String lastName, double wage)
{
super(firstName, lastName);
this.wage = wage;
}
public void setWage(double aWage)
{
wage = aWage;
}
public double getWage()
{
return wage;
}
//Task 1:
//Create the missing method, using the logic below:
// double pay = wage * hoursWorked;
// if (hoursWorked > 40)
// {
// int otHours = hoursWorked - 40;
// double otPay = otHours * wage * .5;
// pay = pay + otPay;
//
// }
// return pay;
//Task 2:
//Override .toString() by invoking the super class .toString and
//then concatenating the HourlyEmployee -specific fields.
}
package employeedemo;
public class Manager extends SalariedEmployee {
private double weeklyBonus;
public Manager(String aFirstName, String aLastName, double anAnnualSalary, double aWeeklyBonus)
{
super(aFirstName, aLastName, anAnnualSalary);
weeklyBonus = aWeeklyBonus;
}
public void setWeeklyBonus(double aBonus)
{
weeklyBonus = aBonus;
}
public double getWeeklyBonus()
{
return weeklyBonus;
}
//Define the missing method using the logic below:
// double pay = super.weeklyPay(hours) + weeklyBonus;
// return pay;
//Override .toString() to invoke the superclass .toString() and concatenate all the remaining fields
}
package employeedemo;
public class SalariedEmployee extends Employee{
private double annualSalary;
public SalariedEmployee(String aFirstName, String aLastName, double anAnnualSalary)
{
super(aFirstName, aLastName);
annualSalary = anAnnualSalary;
}
public void setAnnualSalary(double anAnnualSalary)
{
annualSalary = anAnnualSalary;
}
public double getAnnualSalary()
{
return annualSalary;
}
//Define the missing method using the logic below:
//
// final int WEEKS_PER_YEAR = 52;
// double pay = annualSalary/ WEEKS_PER_YEAR;
// return pay;
//Override the .toString() method, by invoking the superclass .toString() and
//concatenating the remaining fields in this subclass
}
Explanation / Answer
package employeedemo;
public abstract class Employee {
private String firstName;
private String lastName;
// private double hoursWorked;
public Employee()
{
firstName = null;
lastName = null;
}
public Employee(String aFirstName, String aLastName)
{
firstName = aFirstName;
lastName = aLastName;
// hoursWorked = anHoursWorked;
}
public void setFirstName(String aFirstName)
{
firstName = aFirstName;
}
public void setLastName(String aLastName)
{
lastName = aLastName;
}
/* public void setHoursWorked(double anHours)
{
hoursWorked = anHours;
}
public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
/* public double getHoursWorked()
{
return hoursWorked;
}
*/
public String toString(){
return super.toString();
}
public abstract double weeklyPay(int hours);
}
package employeedemo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class EmployeeDemo {
//Define an arrayList of Employee type.
ArrayList<Employee> myEmps = new ArrayList<Employee>();
public static void main(String[] args)
{
Scanner myKeyboard = new Scanner(System.in);
int hoursWorked;
EmployeeDemo myDemo = new EmployeeDemo();
myDemo.createEmployees();
myDemo.computeSalary();
}
public void createEmployees()
{
// Please write code below, and place all objects inside of an arrayList
String lastName, firstName;
double salary;
SalariedEmployee salEmp;
Scanner inFile = null;
File myfile;
boolean fileOpened = false;
//read the file1 that contains only SalariedEmployees
//create SalariedEmployees subclasses, and add them to the Employee arrayList
myfile = new File("Salaried.txt");
FileReader fileReader;
try {
fileReader = new FileReader(myfile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
String[] s1 = line.split(" ");
salary = Double.parseDouble(s1[2]);
SalariedEmployee se = new SalariedEmployee(s1[0], s1[1], salary);
myEmps.add(se);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//read the file2 that contains only HourlyEmployees
//create HourlyEmployees subclass, and add them to the Employee arrayList
try {
fileReader = new FileReader(myfile);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
try {
while ((line = bufferedReader.readLine()) != null) {
String[] s1 = line.split(" ");
salary = Double.parseDouble(s1[2]);
HourlyEmployee he = new HourlyEmployee(s1[0], s1[1], salary);
myEmps.add(he);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void computeSalary()
{
// loop through the arrayList, and call the polymorphic method found in the superclass
// and all of its subclasses. Print the results of the method's calculations
Scanner keyboard = new Scanner(System.in);
int hours;
double pay;
for(Employee e:myEmps){
hours = keyboard.nextInt();
pay = e.weeklyPay(hours);
System.out.println(pay+" ");
}
}
}
package employeedemo;
public class HourlyEmployee extends Employee {
private double wage;
public HourlyEmployee(String firstName, String lastName, double wage)
{
super(firstName, lastName);
this.wage = wage;
}
public void setWage(double aWage)
{
wage = aWage;
}
public double getWage()
{
return wage;
}
@Override
public double weeklyPay(int hoursWorked) {
// TODO Auto-generated method stub
double pay = wage * hoursWorked;
if (hoursWorked > 40)
{
int otHours = hoursWorked - 40;
double otPay = otHours * wage * .5;
pay = pay + otPay;
}
return pay;
}
//Task 1:
//Create the missing method, using the logic below:
// double pay = wage * hoursWorked;
// if (hoursWorked > 40)
// {
// int otHours = hoursWorked - 40;
// double otPay = otHours * wage * .5;
// pay = pay + otPay;
//
// }
// return pay;
//Task 2:
//Override .toString() by invoking the super class .toString and
//then concatenating the HourlyEmployee -specific fields.
@Override
public String toString(){
return super.toString()+wage;
}
}
package employeedemo;
public class Manager extends SalariedEmployee {
private double weeklyBonus;
public Manager(String aFirstName, String aLastName, double anAnnualSalary, double aWeeklyBonus)
{
super(aFirstName, aLastName, anAnnualSalary);
weeklyBonus = aWeeklyBonus;
}
public void setWeeklyBonus(double aBonus)
{
weeklyBonus = aBonus;
}
public double getWeeklyBonus()
{
return weeklyBonus;
}
//Define the missing method using the logic below:
// double pay = super.weeklyPay(hours) + weeklyBonus;
// return pay;
@Override
public double weeklyPay(int hours) {
double pay = super.weeklyPay(hours) + weeklyBonus;
return pay;
}
//Override .toString() to invoke the superclass .toString() and concatenate all the remaining fields
@Override
public String toString(){
return super.toString()+weeklyBonus;
}
}
package employeedemo;
public class SalariedEmployee extends Employee{
private double annualSalary;
public SalariedEmployee(String aFirstName, String aLastName, double anAnnualSalary)
{
super(aFirstName, aLastName);
annualSalary = anAnnualSalary;
}
public void setAnnualSalary(double anAnnualSalary)
{
annualSalary = anAnnualSalary;
}
public double getAnnualSalary()
{
return annualSalary;
}
//Define the missing method using the logic below:
//
// final int WEEKS_PER_YEAR = 52;
// double pay = annualSalary/ WEEKS_PER_YEAR;
// return pay;
//Override the .toString() method, by invoking the superclass .toString() and
//concatenating the remaining fields in this subclass
@Override
public String toString(){
return super.toString()+annualSalary;
}
@Override
public double weeklyPay(int hours) {
// TODO Auto-generated method stub
final int WEEKS_PER_YEAR = 52;
double pay = annualSalary/ WEEKS_PER_YEAR;
return pay;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.