对Java来说很新,这是用于赋值的。基本上我想要做的是用户输入他们工作的小时数,他们的小时费率,他们的直线时间小时以及程序输出的净薪水。从子类的超类调用方法返回值为0
我可以计算毛支付就好了,但分配要求我通过从超类调用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.
只提供需要的东西。不要转储所有的代码。 [MCVE](http://stackoverflow.com/help/mcve)。 – YoungHobbit
您的Payroll类完全错误 - 您不应该创建Pay对象,而是使用超类的方法。这个班有什么要求?总体要求是什么?你正在做出我认为不好的假设。 –