英文:
Code shows no errors but won't send results
问题
我一直在努力寻找缺失的链接,员工的展示,但薪水无法计算。我需要洞察力,因为我似乎没有注意到会引起这种情况的原因。我相信问题在于TestEmployee.java。
Employee.java
package salary;
public abstract class Employee {
public String fullName;
public Employee() {
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public abstract double getBiWeeklyPay();
public void printgetBiWeeklyPay() {
System.out.println("员工 " + this.fullName + " 的" + "双周薪水是:");
}
}
HourlyEmployee.java
package salary;
public class HourlyEmployee extends Employee {
public double hourlyRate;
public double hoursPerWeek;
public HourlyEmployee() {
}
public double getBiWeeklyPay() {
return hourlyRate * (hoursPerWeek * 2);
}
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public double getHoursPerWeek() {
return hoursPerWeek;
}
public void setHoursPerWeek(double hoursPerWeek) {
this.hoursPerWeek = hoursPerWeek;
}
}
SalaryEmployee.java
package salary;
public class SalaryEmployee extends Employee {
public SalaryEmployee() {
}
public double salary;
public double getBiWeeklyPay() {
return salary / (52 * 2);
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
TestEmployee.java
package salary;
import java.util.*;
public class TestEmployee {
public static void main(String[] args){
java.util.ArrayList<Employee> employees = new java.util.ArrayList<Employee>();
SalaryEmployee john = new SalaryEmployee();
john.setFullName("John Doe");
john.setSalary(50000.00);
employees.add(john);
HourlyEmployee bob = new HourlyEmployee();
bob.setFullName("Bob Smith");
bob.setHourlyRate(15.00);
bob.setHoursPerWeek(20.00);
employees.add(bob);
HourlyEmployee Joe = new HourlyEmployee();
Joe.setFullName("Joe Moe");
Joe.setHourlyRate(27.00);
Joe.setHoursPerWeek(45.00);
employees.add(Joe);
HourlyEmployee Doe = new HourlyEmployee();
Doe.setFullName("Doe Joe");
Doe.setHourlyRate(20.00);
Doe.setHoursPerWeek(25.00);
employees.add(Doe);
print(employees);
}
public static void print(ArrayList<Employee> employees){
for(Employee e: employees){
e.printgetBiWeeklyPay();
}
}
}
员工、薪水、按小时计算的薪水都是预先制作的,以及TestEmployee,我按照指示进行操作。我阅读了教材和老师的讲座,一切似乎都是有序的。希望有人能给我一个提示,我会有更好的理解。
英文:
I have been stuck trying to find the missing link, the employee's show but the pay won't calculate. I could use the insight as i can't seem to notice what would cause this. I do belive the problem lies in TestEmployee.java.
Employee.java
package salary;
public abstract class Employee {
public String fullName;
public Employee() {
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public abstract double getBiWeeklyPay();
public void printgetBiWeeklyPay() {
System.out.println("Employee "+this.fullName+ "'s"+ "Bi-weekly pay is:");
}
}
HourlyEmployee .java
package salary;
public class HourlyEmployee extends Employee {
public double hourlyRate;
public double hoursPerWeek;
public HourlyEmployee() {
}
public double getBiWeeklyPay() {
return hourlyRate * (hoursPerWeek *2);
}
public double getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(double hourlyRate) {
this.hourlyRate = hourlyRate;
}
public double getHoursPerWeek() {
return hoursPerWeek;
}
public void setHoursPerWeek(double hoursPerWeek) {
this.hoursPerWeek = hoursPerWeek;
}
}
SalaryEmployee.java
package salary;
public class SalaryEmployee extends Employee {
public SalaryEmployee() {
}
public double salary;
public double getBiWeeklyPay() {
return salary/(52*2) ;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
TestEmployee.java
package salary;
import java.util.*;
public class TestEmployee {
public static void main(String[] args){
java.util.ArrayList<Employee> employees = new java.util.ArrayList<Employee>();
SalaryEmployee john = new SalaryEmployee();
john.setFullName("John Doe");
john.setSalary(50000.00);
employees.add(john);
HourlyEmployee bob = new HourlyEmployee();
bob.setFullName("Bob Smith");
bob.setHourlyRate(15.00);
bob.setHoursPerWeek(20.00);
employees.add(bob);
HourlyEmployee Joe = new HourlyEmployee();
Joe.setFullName("Joe Moe");
Joe.setHourlyRate(27.00);
Joe.setHoursPerWeek(45.00);
employees.add(Joe);
HourlyEmployee Doe = new HourlyEmployee();
Doe.setFullName("Doe Joe");
Doe.setHourlyRate(20.00);
Doe.setHoursPerWeek(25.00);
employees.add(Doe);
print(employees);
}
public static void print(ArrayList<Employee> employees){
for(Employee e: employees){
e.printgetBiWeeklyPay();
}
}
}
Employee, salary, hourly were mostly premade along with TestEmployee i was instructed. I read through my textbook and my teachers lectures, and everything seems to be in order. Hopefully someone could give me a hint and i will have a better understanding.
答案1
得分: 1
我建议您通过实际调用预期方法来改进printgetBiWeeklyPay()的实现。
英文:
I suggest you improve the implementation of printgetBiWeeklyPay() by actually invoking the intended method.
答案2
得分: 0
public void printgetBiWeeklyPay() {
System.out.print("Employee " + this.fullName + "'s" + "Bi-weekly pay is: " + String.valueOf(this.getBiWeeklyPay()));
}
英文:
public void printgetBiWeeklyPay() {
System.out.println("Employee "+this.fullName+ "'s"+ "Bi-weekly pay is:");
}
Referring to your code above, your method printgetBiWeeklyPay() just did what you have mentioned, printing the employee name and bi-weekly pay but without the number... Can you see that you are missing the crucial variable in the print statement? Where is your variable for biweekly pay? You never call it, hence no output of the biweekly pay is produced.
Perhaps:
public void printgetBiWeeklyPay() {
System.out.print("Employee "+this.fullName+ "'s"+ "Bi-weekly pay is:" + String.valueOf(this.getBiWeeklyPay());
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论