0

The methods grossPay() and fedTax() are not being computed for some reason.

  1. How do I return the grossPay() result, grosspay?
  2. How do I return the fedTax() result, taxtotal?
  3. Where would I compute totalpay = grosspay - taxtotal?
  4. Since I cannot return taxtotal I can't tell if it is working, but did I correctly call grosspay and dependents to compute the taxtotal?

Thank you!

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);
}
}
Hcorg
  • 11,598
  • 3
  • 31
  • 36
Jesus T.
  • 25
  • 5
  • Where is `wagecalc` used? No where. Why is it there? Use an IDE and pay attention to warnings. – bcsb1001 Oct 15 '15 at 15:49
  • 3
    Well, you never call those methods. You should just call them, so they can set the values. Also, you should chose different names, as now the method names are almost identical to the variable names. How about "calculateCrossPay" or similar? – tobias_k Oct 15 '15 at 15:50

3 Answers3

1

Either call them, or use getters (better), getters, aka accessors, are a naming convention where the method is prefixed with get (see also 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());
    ...
}

That way the gross pay will always be correct, there's no field to get out of date.

You have some other problems with getGrossPay the various sub totals are not all calculated, but I'll let you work that out or you can see if this makes sense to you:

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;
}
Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203
0

Since you are doing all your calculation using your instance variables, you do not have to return from your methods. You can just keep updating the variables and print them at the end.

Here, I have a made a few changes to your program.

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);
        }
    }
yogidilip
  • 790
  • 6
  • 21
0

Let's start with your fields. You only need a handful of them around.

// private instance variables
private double hours;
private double wage_rate;
private int dependents;

These three fields (and wage_rate should be wageRate to fall in convention) are the only ones you require. All of the other values should be computed.

Next, since the values you're computing depend on one another (with doubleTime including both your base rate and the time-and-a-half rate), you should compute those up front. There shouldn't be any risk to this, since for instance, you don't care if doubleTime is inaccurate if you only have 51 hours of work.

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

Third, let's take a hard look at that fedTax method. The good news is that you don't use dependenttax (which really should be dependentTax) or tax anywhere else, so all you need to do is explicitly return the value you compute. Don't forget to actually call the grossPay method, since you require that as part of your computation.

public double fedTax() {
    tax = (0.10 * grossPay());
    dependenttax = (25 * dependents);
    return tax + dependenttax;
}

Lastly, all that's left is printing the values that you have to compute towards the last parts of your input field...

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

...but I leave that as an explicit exercise for the reader.

Makoto
  • 104,088
  • 27
  • 192
  • 230