【发布时间】:2014-06-03 12:03:08
【问题描述】:
所以这个程序应该做的是从键盘获取 id、小时数和工资的输入。然后它将计算加班费、正常工资和总工资。计算完成后,id、小时数、工资、加班费、正常工资和总工资应该被计算出来,必须打印到输出中。
还假设员工的属性和行为在员工类中。
但是当我想打印 toString 方法时,常规工资、加班工资和总工资显示为零。知道为什么吗?
主要方法:
public class FinalExam
{ // begin class
public static void main(String[] args)
{ // begin main
// ********** DECLARATION OF CONSTANTS **********
// ********** DECLARATION OF VARIABLES **********
String strout; // output string (toString)
int ID; // employee's id number
int Hours; // employee's hours worked
double Wage; // employee's wage per hour
// ********** CREATE OBJECTS **********
ArrayList<Employee> employeeInfo = new ArrayList(); // list of all employee's properties/objects
// ********** CREATE INPUT STREAMS **********
Scanner keyboard = new Scanner(System.in); // create a Scanner object for keyboard input.
// ********** GET INPUT **********
// get the employee's ID
System.out.println("\nEnter your employee ID.");
ID = keyboard.nextInt(); //get the input and set it to the local varaible ID
Employee employee = new Employee(ID); // pass your id
//System.out.println("Employee ID: " + ID);
// get the employee's hours worked
System.out.println("\nEnter the amount of hours you worked this week.");
Hours = keyboard.nextInt(); //get the input and set it to the local varaible HoursWorked
employee.setHours(Hours); // pass your hours
//System.out.println("Hours worked: " + Hours);
// get the employee's wage
System.out.println("\nEnter your wage.");
Wage = keyboard.nextDouble(); //get the input and set it to the local varaible Wage
employee.setWage(Wage); // pass your wage
//System.out.println("Employee wage: " + Wage);
employeeInfo.add(employee); // add it to the list of course
// ********** OUTPUT **********
System.out.println("\n\n" + employeeInfo.toString());
} // end main
} // end class
然后是 Employee 类:
public class Employee
{ // begin class
// *********** CLASS VARIABLES **********
// *********** CLASS CONSTANTS **********
private static int MAXHOURS = 40; // maximum hours before overime
private static double OTRATE = 1.5; // overtime is one and a half
// ********** INSTANCE VARIABLES **********
private int ID; // employee's id
private int Hours; // number of hours worked
private double Wage; // pay per hour
private double RegularPay; // regular pay
private int OverHours; // number of overtime hours worked
private double OverPay; // overtime pay
private double GrossPay; // gross pay
// ********** CREATE INPUT STREAMS **********
DecimalFormat df1 = new DecimalFormat ("#####.00"); // to get two decimal places at the end of the numbers
// ********** CONSTRUCTORS ***********
public Employee(int IDnumber)
{ // begin initialized constructor
ID = IDnumber; // set ID to ID number
} // end initialized constructor
// ********** ACCESSORS **********
public int getID()
{ // begin getID
return ID;
} // end getID
public void setWage(double HourlyWage)
{ // begin setWage
Wage = HourlyWage;
} // end setWage
public double getWage()
{ // begin getWage
return Wage;
} // end getWage
public void setHours(int hoursWorked)
{ // begin setHours
Hours = hoursWorked;
} // end setHours
public double getHours()
{ // begin getHours
return Hours;
} // end getHours
// ********** MUTATORS **********
public double getOverPay()
{ // begin getOverPay
if (Hours > MAXHOURS)
{ // begin if hours worked is bigger than MAXHOURS
OverHours = Hours - MAXHOURS;
OverPay = OverHours * Wage * OTRATE;
} // end if hours worked is bigger than MAXHOURS
else
OverPay = 0;
return OverPay;
} // end getOverPay
public double getRegularPay()
{ // begin getRegularPay
return MAXHOURS * Wage;
} // end getRegularPay
public double getGrossPay()
{ // begin getGrossPay
return RegularPay + OverPay;
} // end getGrossPay
public String toString() // overrides the toString method inherited from object
{ // begin toString
String strout = "\nId \t\t Hours \t\t Rate \t\t Regular Pay \t Overtime Pay \t Gross Pay\n";
strout += ID + "\t " + Hours + "\t\t\t $" + (df1.format(Wage)) + "\t\t $" + (df1.format(RegularPay)) + "\t\t\t $" + (df1.format(OverPay)) + "\t\t\t $" + (df1.format(GrossPay));
// df1.format(double) allows me two decimal places
return strout;
} // end toString
} // end class
【问题讨论】:
-
您期望什么值以及您在哪里设置它们?
-
我想从键盘获取值并从那里将它们返回给员工类,以便它可以获取 regPay、overPay 和 GrossPay。
标签: java class properties return behavior