由于某些原因,未计算方法grossPay()
和fedTax()
。
grossPay()
结果grosspay
?fedTax()
结果taxtotal
?totalpay = grosspay - taxtotal
?taxtotal
我无法判断它是否有效,但是我是否正确调用grosspay
和dependents
来计算taxtotal
?< / LI>
醇>
谢谢!
import java.util.Scanner;
public class WorkPay {
public static void main(String args[]) {
WorkPay wagecalc = new WorkPay(); // 1. Instantiate the object WorkPay
WorkPay input = new WorkPay(); // 2. Reference the method to prompt for inputs
input.prompt4data();
input.display(); // 3. Reference the method to display the results
}
public void prompt4data() {
Scanner console = new Scanner(System.in);
System.out.println("How many hours have you worked?");
hours = console.nextDouble();
System.out.println("What is your wage rate?");
wage_rate = console.nextDouble();
System.out.println("How many dependents do you have?");
dependents = console.nextInt();
}
// private instance variables
private double hours;
private double wage_rate;
private int dependents;
private double grosspay;
private double totalpay;
private double tax;
private double dependenttax;
private double taxtotal;
public double grossPay() {
double total1 = 0;
double total2 = 0;
double total3 = 0;
if (hours <= 40) {
total1 = wage_rate * hours;
grosspay = total1;
}
else if (hours > 40 && hours <= 60) {
total2 = total1 + (wage_rate * 1.5) * (hours - 40);
grosspay = total2;
}
else {
total3 = total2 + (wage_rate * 2) * (hours - 60);
grosspay = total3;
}
return grosspay;
}
public void fedTax() {
tax = (0.10 * grosspay);
dependenttax = (25 * dependents);
taxtotal = tax + dependenttax;
if (tax < 0)
System.out.println("Taxt withheld can't be less than 0.");
}
public void display() {
System.out.println("The hours worked is: " + hours);
System.out.println("The hoursly rate is: " + wage_rate);
System.out.println("The number of dependents is: " + dependents);
System.out.println("The gross income is: " + grosspay);
System.out.println("The federal tax withheld is: " + taxtotal);
System.out.println("The take home pay is: " + totalpay);
}
}
答案 0 :(得分:1)
调用它们,或者使用getter(更好),getters,aka accessors,是一种命名约定,其中方法以get
为前缀(另请参阅Java - Using Accessor and Mutator methods):
public double getGrossPay() {
double grosspay = 0; //local now (remove field)
double total1 = 0;
double total2 = 0;
double total3 = 0;
if (hours <= 40) {
total1 = wage_rate * hours;
grosspay = total1;
}
else if (hours > 40 && hours <= 60) {
total2 = total1 + (wage_rate * 1.5) * (hours - 40);
grosspay = total2;
}
else {
total3 = total2 + (wage_rate * 2) * (hours - 60);
grosspay = total3;
}
return grosspay;
}
public void display() {
...
System.out.println("The gross income is: " + getGrossPay());
...
}
这样总薪酬总是正确的,没有任何字段可以过时。
getGrossPay
您还有其他一些问题:各种小计并非都计算完毕,但我会让您解决这个问题,或者您可以看看这对您是否有意义:
public double getGrossPay() {
double grosspay = wage_rate * hours;
if (hours > 40) grosspay += wage_rate * (hours - 40) * 0.5;
if (hours > 60) grosspay += wage_rate * (hours - 60) * 0.5;
return grosspay;
}
答案 1 :(得分:0)
由于您使用实例变量进行所有计算,因此无需从方法返回。您可以继续更新变量并在最后打印它们。
在这里,我对您的计划进行了一些更改。
import java.util.Scanner;
public class WorkPay {
// private instance variables
private double hours;
private double wage_rate;
private int dependents;
private double grosspay;
private double totalpay;
private double tax;
private double dependenttax;
private double taxtotal;
public static void main(String args[]) {
WorkPay input = new WorkPay(); // 2. Reference the method to prompt for
// inputs
input.prompt4data();
input.grossPay();
input.fedTax();
input.display(); // 3. Reference the method to display the results
}
public void prompt4data() {
Scanner console = new Scanner(System.in);
System.out.println("How many hours have you worked?");
hours = console.nextDouble();
System.out.println("What is your wage rate?");
wage_rate = console.nextDouble();
System.out.println("How many dependents do you have?");
dependents = console.nextInt();
}
public void grossPay() {
double total1 = 0;
double total2 = 0;
double total3 = 0;
if (hours <= 40) {
total1 = wage_rate * hours;
grosspay = total1;
} else if (hours > 40 && hours <= 60) {
total2 = total1 + (wage_rate * 1.5) * (hours - 40);
grosspay = total2;
} else {
total3 = total2 + (wage_rate * 2) * (hours - 60);
grosspay = total3;
}
}
public void fedTax() {
tax = (0.10 * grosspay);
dependenttax = (25 * dependents);
taxtotal = tax + dependenttax;
if (tax < 0)
System.out.println("Taxt withheld can't be less than 0.");
}
public void display() {
System.out.println("The hours worked is: " + hours);
System.out.println("The hoursly rate is: " + wage_rate);
System.out.println("The number of dependents is: " + dependents);
System.out.println("The gross income is: " + grosspay);
System.out.println("The federal tax withheld is: " + taxtotal);
System.out.println("The take home pay is: " + totalpay);
}
}
答案 2 :(得分:0)
让我们从你的领域开始吧。你只需要少数几个。
// private instance variables
private double hours;
private double wage_rate;
private int dependents;
这三个字段(以及wage_rate
应该wageRate
符合惯例)是您需要的唯一字段。应计算所有其他值。
接下来,由于您计算的值彼此依赖(doubleTime
包括您的基本费率和时间的一半),您应该预先计算这些值。这不应该有任何风险,因为例如,如果你只有51个小时的工作时,你不在乎doubleTime
是否不准确。
public double grossPay() {
double baseRate = wage_rate * hours;
double timeAndAHalf = baseRate + (wage_rate * 1.5) * (hours - 40);
double doubleTime = timeAndAHalf + (wage_rate * 2) * (hours - 60);
if (hours <= 40) {
return baseRate;
} else if (hours > 40 && hours <= 60) {
return timeAndAHalf;
} else {
return doubleTime;
}
return grosspay;
}
第三,让我们仔细研究fedTax
方法。好消息是你不能在其他任何地方使用dependenttax
(真的应该是dependentTax
)或tax
,所以你需要做的只是明确的返回您计算的值。不要忘记实际调用 grossPay
方法,因为您需要将其作为计算的一部分。
public double fedTax() {
tax = (0.10 * grossPay());
dependenttax = (25 * dependents);
return tax + dependenttax;
}
最后,剩下的就是将您必须计算的值打印到输入字段的最后部分...
// Method call to...something that does things with gross pay
System.out.println("The gross income is: " + _____);
// Method call to...something that does things with tax...
System.out.println("The federal tax withheld is: " + _____);
// What's the net pay again?
System.out.println("The take home pay is: " + ____);
...但我将 作为读者的明确练习。