Hello, I wrote all my class done and seem looks good. But I had problem on my te
ID: 3730173 • Letter: H
Question
Hello, I wrote all my class done and seem looks good. But I had problem on my test class. Could you help me take a look?
public class Name{
private String firstName;
private String lastName;
/**
* @param firstName
* @param lastName
*/
public Name(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return firstName+" "+lastName;
}
}
public class Address{
private String street;
private String city;
private String state;
private String zip;
/**
* @param street
* @param city
* @param state
* @param zip
*/
public Address(String street, String city, String state, String zip) {
super();
this.street = street;
this.city = city;
this.state = state;
this.zip = zip;
}
/**
* @return the street
*/
public String getStreet() {
return street;
}
/**
* @return the city
*/
public String getCity() {
return city;
}
/**
* @return the state
*/
public String getState() {
return state;
}
/**
* @return the zip
*/
public String getZip() {
return zip;
}
/**
* @param street the street to set
*/
public void setStreet(String street) {
this.street = street;
}
/**
* @param city the city to set
*/
public void setCity(String city) {
this.city = city;
}
/**
* @param state the state to set
*/
public void setState(String state) {
this.state = state;
}
/**
* @param zip the zip to set
*/
public void setZip(String zip) {
this.zip = zip;
}
@Override
public String toString() {
return street+", "+city+", "+state+"-"+zip;
}
}
public class Date{
private int day;
private int month;
private int year;
/**
* @param day
* @param month
* @param year
*/
public Date(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
/**
* @return the day
*/
public int getDay() {
return day;
}
/**
* @return the month
*/
public int getMonth() {
return month;
}
/**
* @return the year
*/
public int getYear() {
return year;
}
/**
* @param day the day to set
*/
public void setDay(int day) {
this.day = day;
}
/**
* @param month the month to set
*/
public void setMonth(int month) {
this.month = month;
}
/**
* @param year the year to set
*/
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return month+"/"+day+"/"+year;
}
}
public class Employee2 {
private int empId;
private Name name;
private Address address;
private Date hireDate;
/**
* @param empId
* @param name
* @param address
* @param hireDate
*/
public Employee2(int empId, Name name, Address address, Date hireDate) {
super();
this.empId = empId;
this.name = name;
this.address = address;
this.hireDate = hireDate;
}
/**
* @return the empId
*/
public int getEmpId() {
return empId;
}
/**
* @return the name
*/
public Name getName() {
return name;
}
/**
* @return the address
*/
public Address getAddress() {
return address;
}
/**
* @return the hireDate
*/
public Date getHireDate() {
return hireDate;
}
/**
* @param empId the empId to set
*/
public void setEmpId(int empId) {
this.empId = empId;
}
/**
* @param name the name to set
*/
public void setName(Name name) {
this.name = name;
}
/**
* @param address the address to set
*/
public void setAddress(Address address) {
this.address = address;
}
/**
* @param hireDate the hireDate to set
*/
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public void display(){
System.out.println("Emp No.: "+empId);
System.out.println("Name: "+name.toString());
System.out.println("Address:");
System.out.println(" "+address.toString());
System.out.println("Hire Date: "+hireDate.toString());
}
}
public class SalariedEmployee extends Employee2 {
private double annualSalary;
public SalariedEmployee(int empID, Name name, Address address, Date hireDate,double annualSalary) {
super(empID, name, address, hireDate);
this.setAnnualSalary(annualSalary);
}
public double getAnnualSalary() {
return annualSalary;
}
public void setAnnualSalary(double annualSalary) {
this.annualSalary = annualSalary;
}
@Override
public String toString() {
return super.toString()+" Annual Salary="+annualSalary;
}
}
public class HourlyEmployee extends Employee2 {
private float hourlyPayRate;
private float hoursWorked;
private double earning;
public HourlyEmployee(int empId, Name name, Address address, Date hireDate, float hourlyPayRate,float hoursWorked) {
super(empId, name, address, hireDate);
this.setHourlyPayRate(hourlyPayRate);
this.setHoursWorked(hoursWorked);
if(hoursWorked>40){
this.earning = hoursWorked*hourlyPayRate*1.5;
}
else{
this.earning = hoursWorked*hourlyPayRate;
}
}
public float getHourlyPayRate() {
return hourlyPayRate;
}
public void setHourlyPayRate(float hourlyPayRate) {
this.hourlyPayRate = hourlyPayRate;
}
public float getHoursWorked() {
return hoursWorked;
}
public void setHoursWorked(float hoursWorked) {
this.hoursWorked = hoursWorked;
}
public double getEarning() {
return earning;
}
public void setEarning(double earning) {
this.earning = earning;
}
@Override
public String toString() {
return super.toString()+" Hours Worked="+hoursWorked+" Hourly Pay Rate="+hourlyPayRate+" Earnings="+earning;
}
}
public class Employee2Test {
public static void main(String[] args) {
SalariedEmployee salEmp = new SalariedEmployee(1234, "John", "Autumn Spell", "Jan 4,2018", 500000.0);
HourlyEmployee hrEmp1 = new HourlyEmployee(5678, "Smith", "Cedar Ln", "Sep 16, 2007", 4.0f, 30);
HourlyEmployee hrEmp2 = new HourlyEmployee(1111, "Danna", "Hibiscus Ct", "Feb 6, 2015", 4.0f, 50);
System.out.println("Salaried Employee: "+salEmp+" ");
System.out.println("Hourly Employee: "+hrEmp1+" ");
System.out.println("Hourly Employee: "+hrEmp2+" ");
}
}
Explanation / Answer
Hello, I’ve figured out the mistake in your code. You were passing wrong values to the constructors of each Employee2 objects (Salaried and Hourly). The Employee2 class is defined to have Name datatype for name field, and Date datatype for date field, and Address datatype for address field. You were just passing String values to the constructor which was causing the error. So I’ve defined one Name object, one Address object and one Date object, used to set the values of each employee, passed these objects in constructors instead of String values and executed the program.
There was one more error, the toString() method was not defined for Employee2 class. Only display() method was created, so I have replaced the display method with toString() method to return a String containing properly formatted fields and values. After this, the program worked perfectly fine.
I’m attaching only the modified classes Employee2Test.java and Employee2.java as Chegg is showing character limit exceeded error while pasting whole code. You can use all other classes without any modifications. Thanks
// Employee2Test.java
public class Employee2Test {
public static void main(String[] args) {
/**
* Defining Name, Address, and Date objects to be used while creating
* Employees
*/
Name name;
Address address;
Date date;
/**
* initializing name for the first employee
*/
name = new Name("John", "Jo");
/**
* initializing address for first employee
*/
address = new Address("Autumn Spell", "XYZ", "Washington", "123456");
/**
* initializing date for first employee
*/
date = new Date(4, 1, 2018);
/**
* Defining a Salariedemployee using the above name, address and date objects
*/
SalariedEmployee salEmp = new SalariedEmployee(1234, name, address,
date, 500000.0);
/**
* Reusing the above name, address, date objects to store details of second employee
*/
name = new Name("Smith", "S");
address = new Address("Cedar Ln", "XYZ", "Washington", "123456");
date = new Date(16, 9, 2007);
/**
* Defining a HourlyEmployee using the above name, address and date objects
*/
HourlyEmployee hrEmp1 = new HourlyEmployee(5678, name, address, date,
4.0f, 30);
/**
* Reusing the above name, address, date objects to store details of third employee
*/
name = new Name("Danna", "D");
address = new Address("Hibiscus Ct", "ABCDE", "Alaska", "112233");
date = new Date(6, 2, 2015);
/**
* Defining another HourlyEmployee using the above name, address and date objects
*/
HourlyEmployee hrEmp2 = new HourlyEmployee(1111, name, address, date,
4.0f, 50);
System.out.println("Salaried Employee: " + salEmp + " ");
System.out.println("Hourly Employee: " + hrEmp1 + " ");
System.out.println("Hourly Employee: " + hrEmp2 + " ");
}
}
// Employee2.java
public class Employee2 {
private int empId;
private Name name;
private Address address;
private Date hireDate;
/**
* @param empId
* @param name
* @param address
* @param hireDate
*/
public Employee2(int empId, Name name, Address address, Date hireDate) {
super();
this.empId = empId;
this.name = name;
this.address = address;
this.hireDate = hireDate;
}
/**
* @return the empId
*/
public int getEmpId() {
return empId;
}
/**
* @return the name
*/
public Name getName() {
return name;
}
/**
* @return the address
*/
public Address getAddress() {
return address;
}
/**
* @return the hireDate
*/
public Date getHireDate() {
return hireDate;
}
/**
* @param empId
* the empId to set
*/
public void setEmpId(int empId) {
this.empId = empId;
}
/**
* @param name
* the name to set
*/
public void setName(Name name) {
this.name = name;
}
/**
* @param address
* the address to set
*/
public void setAddress(Address address) {
this.address = address;
}
/**
* @param hireDate
* the hireDate to set
*/
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
@Override
public String toString() {
String data = "Emp No.: " + empId + " ";
data += "Address: " + address + " ";
data += "Hire Date: " + hireDate;
return data;
}
}
/*OUTPUT*/
Salaried Employee:
Emp No.: 1234
Address:
Autumn Spell, XYZ, Washington-123456
Hire Date: 1/4/2018
Annual Salary=500000.0
Hourly Employee:
Emp No.: 5678
Address:
Cedar Ln, XYZ, Washington-123456
Hire Date: 9/16/2007
Hours Worked=30.0
Hourly Pay Rate=4.0
Earnings=120.0
Hourly Employee:
Emp No.: 1111
Address:
Hibiscus Ct, ABCDE, Alaska-112233
Hire Date: 2/6/2015
Hours Worked=50.0
Hourly Pay Rate=4.0
Earnings=300.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.