Java新手,这是一项任务。基本上我要做的是用户输入他们工作的小时数,他们的小时费率,以及他们的直接工作时间和程序输出他们的净工资。
我可以很好地计算总薪水,但是这项任务要求我通过调用超类中的calc_payroll和tax方法来计算子类中的净工资,但它会保持返回零值。我想也许我的税收方法有问题所以我尝试从子类中返回总薪酬,但它仍然返回零。
我真的难过这里,有人可以帮忙吗?
超类:
class Pay
{
private float hoursWorked;
private float rate;
private int straightTimeHours;
public double calc_payroll()
{
double straightTimePay = rate * straightTimeHours;
double excessPay = (rate * 1.33) * (hoursWorked - straightTimeHours);
double grossPay = straightTimePay + excessPay;
return grossPay;
}
public double tax(double a)
{
double taxRate;
double netPay;
if(a <= 399.99)
taxRate = 0.08;
else if(a > 399.99 && a <= 899.99)
taxRate = 0.12;
else
taxRate = 0.16;
netPay = a - (a * taxRate);
return netPay;
}
public void setHours(float a)
{
hoursWorked = a;
}
public float getHours()
{
return hoursWorked;
}
public void setRate(float a)
{
rate = a;
}
public float getRate()
{
return rate;
}
public void setHrsStr(int a)
{
straightTimeHours = a;
}
public int getHrsStr()
{
return straightTimeHours;
}
}
子类:
class Payroll extends Pay
{
public double calc_payroll()
{
Pay getVariables = new Pay();
double getGrossPay = getVariables.calc_payroll();
double finalNetPay = getVariables.tax(getGrossPay);
return finalNetPay; //This returns a value of zero
//return getGrossPay; This also returns a value of zero
}
}
主要方法:
import java.util.*;
class Assign2A
{
public static void main(String args[])
{
float userHours;
float userRate;
int userStraight;
Scanner userInput = new Scanner(System.in);
System.out.println("I will help you calculate your gross and net pay!");
System.out.println("Please enter the number of hours you have worked: ");
userHours = Float.valueOf(userInput.nextLine());
System.out.println("Please enter your hourly pay rate: ");
userRate = Float.valueOf(userInput.nextLine());
System.out.println("Please enter the number of straight hours required: ");
userStraight = Integer.parseInt(userInput.nextLine());
Pay object = new Pay();
object.setHours(userHours);
object.setRate(userRate);
object.setHrsStr(userStraight);
Payroll objectTwo = new Payroll();
System.out.println("========================================");
System.out.println("Your gross pay is: ");
System.out.println("$" + object.calc_payroll());
System.out.println("Your net pay is: ");
System.out.println("$" + objectTwo.calc_payroll());
System.out.println("Thank you, come again!");
}
}
典型输出:
----jGRASP exec: java Assign2A
I will help you calculate your gross and net pay!
Please enter the number of hours you have worked:
500
Please enter your hourly pay rate:
25
Please enter the number of straight hours required:
100
========================================
Your gross pay is:
$15800.0
Your net pay is:
$0.0
Thank you, come again!
----jGRASP: operation complete.
答案 0 :(得分:1)
几个问题。首先,您要创建一个Payroll对象:
Payroll objectTwo = new Payroll();
//.....
System.out.println("$" + objectTwo.calc_payroll());
不给它任何值,然后当它的值为0时会感到惊讶。
您应该在这里使用一个单独的对象,一个Payroll对象,而不是Pay对象,用有效数据填充它,并在这个单个对象上调用这两个方法。
其次,您的Payroll类完全错误。你有:
class Payroll extends Pay {
public double calc_payroll() {
Pay getVariables = new Pay();
double getGrossPay = getVariables.calc_payroll();
double finalNetPay = getVariables.tax(getGrossPay);
return finalNetPay; // This returns a value of zero
// return getGrossPay; This also returns a value of zero
}
}
但它不应该创建Pay对象,而是根据需要使用super方法。为了更好地帮助您,您必须告诉我们您的完整作业要求,因为您对作业做出了错误的假设,我相信。