[英]Java: Trouble calling another class
I am just beginning in Java and I am having a peculiar problem that I just cant seem to get to the root of. 我刚刚开始使用Java,我遇到了一个特殊的问题,我似乎无法找到它的根源。 I have 2 programs and one is taking data from a text file and then calling it into a class to do some calculations and finally putting the output into another text document. 我有2个程序,一个是从文本文件中获取数据,然后将其调用到一个类中进行一些计算,最后将输出放到另一个文本文档中。
Everything works except this part here: 除此部分外,一切正常:
public class Paycheck
{
//Constants for the private class
private final String EMPLOYEE_NAME; //Employee name
private final String SOC_SEC_NUM; //Employee Social Security Number
private final double WAGE_RATE; //Employee wage
private final double TAX_RATE; //Employee tax withheld
private final double HOURS_WORKED; //Employee's hours of work
//Variables for the private class
private double grossPay; //Employee Gross Pay
private double taxWithheld; //Employee Tax Withheld
private double netPay; //Employee Net Pay
//This is the constructor. It is called whenever an instance of the class is created
public Paycheck (String name, String ssn, double wage, double tax, double hours)
{
EMPLOYEE_NAME = name; //Instance employee name
SOC_SEC_NUM = ssn; //Instance employee SSN
WAGE_RATE = wage; //Instance employee wage rate
TAX_RATE = tax; //Instance employee tax rate
HOURS_WORKED = hours; //Instance employee hours worked
}
//This calculates the variables in the paycheck class
public void calcWages()
{
grossPay = WAGE_RATE * HOURS_WORKED; //Calculates Gross Pay
taxWithheld = grossPay * TAX_RATE; //Calculates Taxes Withheld
netPay = grossPay - taxWithheld; //Calculates net pay
}
//Returns the Paycheck objects Employee Name
public String getEmployeeName()
{
return EMPLOYEE_NAME;
}
//Returns the employee SSN of the Paycheck object
public String getSocSecNum()
{
return SOC_SEC_NUM;
}
//Reeturns a Paycheck object's employee Wage Rate
public double getWageRate()
{
return WAGE_RATE;
}
//Returns a Paycheck object's employee tax rate
public double getTaxRate()
{
return TAX_RATE;
}
//Returns an Paycheck object's employee hours worked
public double getHoursWorked()
{
return HOURS_WORKED;
}
//Returns a Paycheck object's gross pay
public double getGrossPay()
{
return grossPay;
}
//Returns a Paycheck object's Taxes Withheld
public double getTaxWithheld()
{
return taxWithheld;
}
//Returns a paycheck object't net pay
public double getNetPay()
{
return netPay;
}
The calcWages() does the necessary calculations and below this are a series of get statements to call them. calcWages()进行必要的计算,下面是一系列调用它们的get语句。 However, my output does not return any values for the calcWages() arguments. 但是,我的输出不返回calcWages()参数的任何值。
I added the getters here and my other program is grabbing them. 我在这里添加了吸气剂,我的其他程序正在抓住它们。 However the final output on my other program is coming up as 0. 但是我的其他程序的最终输出结果为0。
Where am I going wrong here? 我在哪里错了?
This is the part of the main method that is calling them 这是调用它们的主要方法的一部分
public static void main(String [] args) throws IOException //Throws clause
{
//Declare constants
final String INPUT_FILE = "Employee.txt"; //Input text file containing Employee information
final String OUTPUT_FILE= "PayrollHistory.txt"; //Output text file that will receive the data
//Declare Variables
String payPeriodDate; //Ending date of the pay period
String employeeName; //Employee Name in text file
String employeeSocSecNum; //Employee SSN in text file
double employeeHours; //Employee hours worked
double employeeTax; //Employee Tax rate
double employeeWage; //Employee Wage rate
double totalGrossPay; //Total employee Gross for pay period
double totalTaxWithheld; //Total Tax Withheld for pay period
double totalNetPay; //Total Net Payroll for pay period
String input; //String input for double conversion in JoptionPane
DecimalFormat money = new DecimalFormat ("#0.00"); // Decimal Format to put money in the right format(USD)
//This ensures that the input file actually exists in the program folder
//And exits the program if it does not, along with the prompt.
File file = new File(INPUT_FILE);
if (!file.exists())
{
JOptionPane.showMessageDialog(null, "The " + INPUT_FILE + " file cannot be found." +
"Program terminated.");
System.exit(0);
}
// Create Scanner object to enable reading data from input file
Scanner inputFile = new Scanner(file);
// Create FileWriter and PrintWriter objects to enable
// writing (appending not overwriting) data to text file
FileWriter fwriter = new FileWriter(OUTPUT_FILE, true);
PrintWriter outputFile = new PrintWriter(fwriter);
//Initialize accumulator values
totalGrossPay = 0.0;
totalTaxWithheld = 0.0;
totalNetPay = 0.0;
//Get the pay period for the employee
payPeriodDate = JOptionPane.showInputDialog("Enter pay period ending date (mm/dd/yyyy):");
outputFile.println("PAY PERIOD ENDING DATE: " + payPeriodDate); //Inputs pay period date into the text file
outputFile.println(); // Blank line
outputFile.println(); // Blank line
while (inputFile.hasNext()) // This will look through the input file and get the necessary variable input
{
// Read employee Name from Input File
employeeName = inputFile.nextLine();
// Read employee SSN from input file
employeeSocSecNum = inputFile.nextLine();
// Read employee Wage Rate from input file
//Parses it into a double type
input = inputFile.nextLine();
employeeWage = Double.parseDouble(input);
//Read employee tax rate from input file
//Parses it into a double type
input = inputFile.nextLine();
employeeTax = Double.parseDouble(input);
//Get number of hours worked
input = JOptionPane.showInputDialog("Employee Name: " + employeeName +
"\nEnter number of hours worked:");
employeeHours = Double.parseDouble(input);
//This call the paycheck class to create a new Paycheck Object
Paycheck employee = new Paycheck (employeeName, employeeSocSecNum, employeeWage, employeeTax, employeeHours);
// Call Paycheck class methods into the output file
outputFile.println("Employee Name: " + employeeName); //Employee Name
outputFile.println("SSN: " + employeeSocSecNum); //Employee SSN
outputFile.println("Hours Worked: " + employeeHours); //Employee Hours Worked
outputFile.println("Wage Rate: " + money.format(employeeWage)); //Employee Wage Rate
outputFile.println("Gross Pay: " + money.format(employee.getGrossPay())); //Employee Gross Pay
outputFile.println("Tax Rate: " + money.format(employeeTax)); //Employee Tax Rate
outputFile.println("Tax Withheld: " + money.format(employee.getTaxWithheld())); //Employee Tax Withheld
outputFile.println("Net Pay: " + employee.getNetPay()); //Employee Net Pay
outputFile.println(); // Blank line
You don't appear to be actually calling calcWages()
before calling your getters, so grossPay, taxWithheld, and netPay
are still going to be 0, as that's Java's default value for uninitialized numbers. 在调用getter之前,您似乎没有实际调用 calcWages()
,因此grossPay, taxWithheld, and netPay
仍将为0,因为这是Java未初始化数字的默认值。
You need to call employee.calcWages()
before referencing those values for them to change. 您需要在引用这些值之前调用employee.calcWages()
以进行更改。
It is because calcWages()
is declared void ( public void calcWages()
), meaning it is not supposed to return any value but just complete a series of steps (calc payroll particulars in this example). 这是因为calcWages()
被声明为void ( public void calcWages()
),这意味着它不应该返回任何值,而只是完成一系列步骤(在此示例中为计算工资单详细信息)。 After calling it, just proceed to reference the instance variables it processed. 调用它之后,继续引用它处理的实例变量。
您已将变量声明为final
,这意味着它们只能分配一次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.