Java Help: Question 1- In Question1 folder, the EmployeeDemo.java class creates
ID: 3664241 • Letter: J
Question
Java Help:
Question 1- In Question1 folder, the EmployeeDemo.java class creates instances of the PartTimeEmployee.java and FullTimeEmployee.java classes and prints them. Try the code to see that it runs fine; however, the PartTimeEmployee.java and FullTimeEmployee.java classes have a lot of duplicate code.
A: Use Inheritance to reduce code duplication.
1- For this purpose, create a third class called Employee.java, which will contain the code that is in common between PartTimeEmployee.java and FullTimeEmployee.java classes.
2- Modify PartTimeEmployee.java and FullTimeEmployee.java classes, accordingly.
3- Use the concept of abstract classes where it can be applied.
4- Make sure that the Empolyee.java class also has a toString() method and reuse it in the modified PartTimeEmployee.java and FullTimeEmployee.java classes.
5- You should not change the EmployeeDemo.java class to run your modified code.
B: Write a new main method in a class called EmployeeDemo2.java as follows:
1- Create two Employee variables called empPT and empFT. Assign empPT an instance of a PartTimeEmployee and assign empFT to an instance of a FullTimeEmployee objects that you created in part A.
2- Print the both empPT and empFT variables..
3- Specify whether the toString() method of the Employee class is called or the toString() methods of the PartTimeEmployee and FullTimeEmployee classes are called.
4- Explain why the above has happened and in Object Oriented paradigm, what is it called?
5- Call empPT.getHours() in EmployeeDemo2.java.
a. What is the result? Explain why.
b. If there is an error, how can you change empPT to fix the error?
//EmployeeDemo.java
public class EmployeeDemo{
public static void main(String[] args){
PartTimeEmployee pe1 = new PartTimeEmployee("John", "Driver", 25.0);
FullTimeEmployee fe1 = new FullTimeEmployee("Maggie", "Lab Manager", 8000.0);
pe1.setHours(40);
System.out.println(pe1);
System.out.println(fe1);
}
}
//FullTImeEmployee
public class FullTimeEmployee{
private String name;
private String title;
private double salary;
public FullTimeEmployee(String n, String t, double s){
name = n;
title = t;
salary = s;
}
public String getName(){
return name;
}
public String getTitle(){
return title;
}
public double getSalary(){
return salary;
}
public String toString(){
return " Full-time Employee: " + "Name: " + name +
" Title: " + title +
" Salary: " + salary;
}
}
//PartTimeEmployee
public class PartTimeEmployee{
private String name;
private String title;
private double rate;
private int hours;
public PartTimeEmployee(String n, String t, double r){
name = n;
title = t;
rate = r;
hours = 0;
}
public String getName(){
return name;
}
public String getTitle(){
return title;
}
public void setHours(int h){
hours = h;
}
public int getHours(){
return hours;
}
public double getSalary(){
return hours * rate;
}
public String toString(){
return " Part-time Employee: " + "Name: " + name +
" Title: " + title +
" Salary: " + getSalary();
}
}
Explanation / Answer
public class PartTimeEmployee{
private String name;
private String title;
private double rate;
private int hours;
public PartTimeEmployee(String n, String t, double r){
name = n;
title = t;
rate = r;
hours = 0;
}
public String toString(){
return " Part-time Employee: " + "Name: " + name +
" Title: " + title +
" Salary: " + getSalary();
}
private String getSalary() {
// TODO Auto-generated method stub
return null;
}
public void setHours(int i) {
// TODO Auto-generated method stub
}
}
//FullTImeEmployee
public class FullTimeEmployee{
private String name;
private String title;
private double salary;
public FullTimeEmployee(String n, String t, double s){
name = n;
title = t;
salary = s;
}
public String toString(){
return " Full-time Employee: " + "Name: " + name +
" Title: " + title +
" Salary: " + salary;
}
}
public class EmployeeDemo{
public static void main(String[] args){
PartTimeEmployee pe1 = new PartTimeEmployee("John", "Driver", 25.0);
FullTimeEmployee fe1 = new FullTimeEmployee("Maggie", "Lab Manager", 8000.0);
pe1.setHours(40);
System.out.println(pe1);
System.out.println(fe1);
}
}
public class Employee {
public String getName(){
return getName();
}
public String getTitle(){
return getTitle();
}
public double getSalary(){
return getSalary();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.