由于某些原因未计算方法grossPay()fedTax()


如何返回grossPay()结果grosspay
如何返回fedTax()结果taxtotal
我在哪里计算totalpay = grosspay - taxtotal
由于无法返回taxtotal,所以无法确定它是否正常工作,但是我是否正确调用了grosspaydependents来计算taxtotal


谢谢!

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);
}
}

最佳答案

让我们从您的领域开始。您只需要几个。

// 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: " + ____);


...但是我将其保留为读者的明确练习。

09-30 23:27