【发布时间】:2015-10-22 11:22:43
【问题描述】:
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.
【问题讨论】:
-
只提供需要的东西。不要转储所有代码。 MCVE.
-
你的 Payroll 类是完全错误的——你不应该让它创建一个 Pay 对象,而应该使用超类的方法。这个班的要求是什么?总体要求是什么?我认为你做出了错误的假设。
标签: java inheritance