当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Error in the test class: cannot find symbol

问题描述:

I cannot get rid of the "Cannot find symbol" error where it says "newEmployee.calcWTax(grossPay);" in the test class.

I have looked at many threads and I know it has something to do with the fact that grossPay isn't declared but I can't figure out where.

Also, as a side note, anything in a comment in a parameter or method is because I'm not sure if I'm going to use it or not yet.

/*

Test class to test Employee.java

*/

import javax.swing.JOptionPane;

public class EmployeeTest

{

public static void main(String[] args)

{

//Scanner input = newScanner(System.in);

//gets ID number of employee

Employee newEmployee = new Employee();

String input = JOptionPane.showInputDialog("What is the ID number of the employee?");

int id = Integer.parseInt(input);

newEmployee.setID(id);

//gets wage of employee

input = JOptionPane.showInputDialog("What is the wage of this employee?");

double wage = Double.parseDouble(input);

newEmployee.setWage(wage);

//gets hours worked of employee

input = JOptionPane.showInputDialog("How many hours did this employee work?");

double hours = Double.parseDouble(input);

newEmployee.setHours(hours);

//calculates and displays gross pay

newEmployee.calcGrossPay(hours, wage);

System.out.println(newEmployee.displayPay());

//calculates and displays withholding tax

newEmployee.calcWTax(grossPay);

System.out.println(newEmployee.displayTax());

}

}

And this is the Employee.java:

public class Employee

{

//private String name;

private double wage;

private int id;

private double hours;

private double grossPay;

private double tax;

private double netPay;

public Employee(/*String aName*/)

{

//aName = name;

wage = 0;

id = 0;

hours = 0;

}

public Employee(/*String aName,*/double aWage, int aID, double aHours)

{

//aName = name;

aWage = wage;

aID = id;

aHours = hours;

}

//mutators

public void setID(int aID)

{

int newID = aID + id;

id = newID;

}

/*public void setName(String aName)

{

String newName = aName;

name = newName;

}*/

public void setWage(double aWage)

{

double newWage = aWage + wage;

newWage = wage;

}

public void setHours(double aHours)

{

double newHours = aHours + hours;

newHours = hours;

}

public void setGross(double aGross)

{

double newGross = aGross + grossPay;

newGross = grossPay;

}

//Accessors

public int getID()

{

return id;

}

/*public String getName()

{

return name;

}*/

public double getWage()

{

return wage;

}

public double getHours()

{

return hours;

}

//Computes gross pay

public double calcGrossPay(double hours, double wage)

{

if ( hours > 40 )

{

double extraHours = hours - 40;

grossPay = ( 40 * wage ) + ( extraHours * (wage * 1.5) );

}

else grossPay = hours * wage;

return grossPay;

}

//Displays gross pay

public double displayPay()

{

return grossPay;

}

//calculates Withholding tax

public double calcWTax(double gross)

{

gross = grossPay;

if (grossPay >= 0)

{

if (grossPay >= 0 && grossPay <= 300)

{

double wTax = grossPay * .1;

wTax = tax;

}

else if (grossPay >= 300.01 && grossPay <= 400)

{

double wTax = grossPay * .12;

wTax = tax;

}

else if (grossPay >=400.01 && grossPay <= 500)

{

double wTax = grossPay * .15;

wTax = tax;

}

else if (grossPay >=500.01)

{

double wTax = grossPay * .2;

wTax = tax;

}

else

System.out.println("Error: Gross Pay is below 0");

}

else

System.out.println("You haven't earned any money, get to work!");

return tax;

}

//displays withholding tax

public double displayTax()

{

return tax;

}

//calculates Net Pay

public double calcNetPay()

{

double newNet = grossPay - tax;

newNet = netPay;

return netPay;

}

//displays Net Pay

public double displayNet()

{

return netPay;

}

}

网友答案:

You need to receive the return value from calcGrossPay method and pass it to the calcWTax method.

Declare the return value of calcGrossPay in a variable named grossPay and use it.

double grossPay = newEmployee.calcGrossPay(hours, wage);
System.out.println(newEmployee.displayPay());

//calculates and displays withholding tax
newEmployee.calcWTax(grossPay);

  public double calcWTax(double gross)
  {
    gross = grossPay;

This should be like this grossPay = gross. The input parameter you might be want to set to the class member not the other way round.

  public double calcWTax(double gross) {
  grossPay = gross
网友答案:

Your method calcGrossPay() returns the gross pay. You can store it for use a few lines later by assigning it to a variable I'll conveniently name grossPay.

double grossPay = newEmployee.calcGrossPay(hours, wage);
System.out.println(newEmployee.displayPay());

//calculates and displays withholding tax
newEmployee.calcWTax(grossPay);
网友答案:

You are referencing a variable grossPay which does not exist.

网友答案:

grossPay is a variable of Employee Class not a variable of EmployeeTest class. so Use newEmployee.calcGrossPay(hours,wage) instead of grossPay

分享给朋友:
您可能感兴趣的文章:
随机阅读: