我有3个不同的类,其中2个继承自1个类。我为测试这些类而编写的程序给了我一些我不理解的错误。
这是第一堂课:
public class Employee
{
private String employeeName;
private double weeklySalary;
private String idNumber;
private String dateHired;
public Employee()
{
idNumber = "";
employeeName = "";
dateHired = "";
weeklySalary = 0;
}
public Employee(String name, String employeeID, String hiringDate)
{
idNumber = employeeID;
employeeName = name;
dateHired = hiringDate;
}
public double getWeeklySalary()
{ return weeklySalary; }
public String getEmployee()
{ return employeeName; }
public String getIDNumber()
{ return idNumber; }
public String getdateHired()
{ return dateHired; }
public void setEmployeeName(String name)
{ employeeName = name; }
public void setWeeklySalary(double weekPay)
{ weeklySalary = weekPay; }
public void setEmployeeID(String employeeID)
{ idNumber = employeeID; }
public void setDateHired(String hiringDate)
{ dateHired = hiringDate; }
public boolean equals(Object otherObject)
{
if(otherObject == this)
return true;
if(otherObject == null)
return false;
if(this.getClass() != otherObject.getClass())
return false;
Employee otherID = (Employee)otherObject;
return idNumber == otherID.idNumber;
}
public String toString()
{
String result = String.format("ID Number: %7d\nEmployee: %-20s\nDate Hired: %7d\n$%,11.2f\n",
idNumber, employeeName, dateHired, weeklySalary);
return result;
}
}
我编写的这两个类继承自Employee类:
public class HourlyEmployee extends Employee
{
private static double hourlyPayRate;
private static int hoursWorkedPerWeek;
public HourlyEmployee()
{
super();
hourlyPayRate = 0;
hoursWorkedPerWeek = 0;
}
public HourlyEmployee(String name, String employeeID, String hiringDate, double hourlyPay, int hrsPerWeek)
{
super(name, employeeID, hiringDate);
hourlyPayRate = hourlyPay;
hoursWorkedPerWeek = hrsPerWeek;
}
public static double getHourlyPayRate()
{ return hourlyPayRate; }
public static int getHoursWorkedPerWeek()
{ return hoursWorkedPerWeek; }
public static void setHourlyPayRate(double hourlyPay)
{ hourlyPayRate = hourlyPay; }
public static void setHoursWorkedPerWeek(int hrsPerWeek)
{ hoursWorkedPerWeek = hrsPerWeek; }
public static double getWeeklySalaryHE()
{
double weeklySalary = 0;
if(hoursWorkedPerWeek <= 35)
{
weeklySalary = hourlyPayRate * hoursWorkedPerWeek;
}
else if(hoursWorkedPerWeek > 35)
{
int overtimeHours = hoursWorkedPerWeek - 35;
double weeklyPay = hourlyPayRate * 35;
double overtimePay = (hourlyPayRate * 2) * overtimeHours;
weeklySalary = weeklyPay + overtimePay;
}
return weeklySalary;
}
public String toString()
{
String result = super.toString();
String payRate = String.format("Rate of Pay: %5d\n", hourlyPayRate);
String hoursWorked = String.format("Hours worked per week: %5d\n", hoursWorkedPerWeek);
return result + payRate + hoursWorked;
}
}
...
public class Manager extends Employee
{
private static double annualSalary;
public Manager()
{
super();
annualSalary = 0;
}
public Manager(String name, String employeeID, String hiringDate, double annualPay)
{
super(name, employeeID, hiringDate);
annualSalary = annualPay;
}
public static double getAnnualSalary()
{ return annualSalary; }
public static void setAnnualSalary(double annualPay)
{ annualSalary = annualPay; }
public static double getWeeklySalaryM()
{
double weeklySalary = annualSalary / 52;
return weeklySalary;
}
public String toString()
{
String result = super.toString();
String Salary = String.format("Annual Salary: %7d\n", annualSalary);
return result + Salary;
}
}
我为测试这些课程编写的程序在这里:
public class EmployeeTest
{
public static void main(String[] args)
{
Employee billsEmployee = new Employee("Bill", "112543", "2014" );
HourlyEmployee marysHourlyEmployee = new HourlyEmployee("Mary", "154232", "2016", 11.30, 32);
Manager joesManager = new Manager("Joe", "225231", "2015", 45000);
HourlyEmployee brocksHourlyEmployee = new HourlyEmployee("Brock", "552721", "2014", 15.50, 40);
billsEmployee.getWeeklySalary();
System.out.println(billsEmployee);
marysHourlyEmployee.getWeeklySalaryHE();
System.out.println();
System.out.println(marysHourlyEmployee);
joesManager.getWeeklySalaryM();
System.out.println();
System.out.println(joesManager);
brocksHourlyEmployee.getWeeklySalaryHE();
System.out.println();
System.out.println(brocksHourlyEmployee);
Employee[] employees = new Employee[4];
employees[0] = billsEmployee;
employees[1] = marysHourlyEmployee;
employees[2] = joesManager;
employees[3] = brocksHourlyEmployee;
System.out.print("\n\n\nPrint all accounts\n\n");
for(int i = 0; i < employees.length; i++)
{
System.out.println(employees[i].toString());
System.out.print("\n\n");
}
if(joesManager instanceof Manager)
System.out.println("Joe is a manager.");
}
}
当我运行测试程序时,我收到了一系列错误:
Exception in thread "main" java.util.IllegalFormatConversionException: d != java.lang.String
at java.util.Formatter$FormatSpecifier.failConversion(Formatter.java:4302)
at java.util.Formatter$FormatSpecifier.printInteger(Formatter.java:2793)
at java.util.Formatter$FormatSpecifier.print(Formatter.java:2747)
at java.util.Formatter.format(Formatter.java:2520)
at java.util.Formatter.format(Formatter.java:2455)
at java.lang.String.format(String.java:2940)
at Employee.toString(Employee.java:61)
at java.lang.String.valueOf(String.java:2994)
at java.io.PrintStream.println(PrintStream.java:821)
at EmployeeTest.main(EmployeeTest.java:11)
有人可以帮我弄清楚我为了让我的程序正常运行而犯了哪些错误吗?