Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,423 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,342 people online right now. Registration is fast and FREE... Join Now!




Java HW Help- loops

 
Reply to this topicStart new topic

Java HW Help- loops

RayJKraft
14 Oct, 2008 - 02:43 PM
Post #1

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 5

I have to create a program that will generate net and gross pay for both hourly and salary employees. I have been struggling with it for a few days, I'm really stumped. I know its a wreck I'm working on it right now. When I enter annual salary, it returns the same number as the net salary and 0 for Tax. Also I need an error message when the user inputs an hourly rate less than 5.15 or greater than 25. My loop for that is wrong also. Eventually I need to add overtime calculation for after 40 hours but I haven't started that since I'm having so many problems. Please Help, Thank You In Advance.

Test Class

CODE

import java.util.*;
public class TestNetWeeklyPay
{
    public static void main(String[] args)
    {
        double hoursWorked;
        double hourlyPay;
        double weeklySalary;
        double netWeeklyPay = 0;
        double tax = 0;
        double annualSalary = 0;
        double annualPayroll = 0;
        double netAnnualPay = 0;
        int payType;
        String name;
    
        
        Scanner scan = new Scanner(System.in);
        NetWeeklyPay calc = new NetWeeklyPay();
        System.out.print("Enter employee name: ");
        name = scan.nextLine();
            do {
            System.out.print("Enter employee pay type (1 for hourly, 2 for salary): ");
            payType = scan.nextInt();

          if (payType == 1)
            {
                System.out.print("Enter hours worked during the week: ");
                hoursWorked = scan.nextDouble();
                System.out.print("Enter hourly rate: ");
                hourlyPay = scan.nextDouble();    
                weeklySalary = calc.weeklyGrossHourly(hoursWorked, hourlyPay);
                System.out.print("Weekly payroll information for " + name + "\n");
                System.out.print("Gross Pay\t" + weeklySalary +"\n");
                System.out.print("Federal Tax\t" + tax +"\n");
                System.out.print("Net Pay\t" + netWeeklyPay +"\n");
                    while((hourlyPay >= 5.15) || (hourlyPay <= 25))
                    {
                        System.out.print("error");
                    }
                break;
            }
            else if (payType == 2)
                 System.out.print("Enter annual salary: ");
                annualSalary = scan.nextDouble();
                //annualPayroll = calc.annualPayroll(annualSalary);
                System.out.print("Weekly payroll information for " + name + "\n");
                System.out.print("Gross Pay\t" + annualSalary +"\n");
                tax = calc.tax(annualSalary);
                System.out.print("Federal Tax\t" + tax +"\n");
                netAnnualPay = calc.annualNet(annualSalary);
                System.out.print("Net Pay\t" + netAnnualPay +"\n");
            }
                while((payType==1) || (payType==2));
                    
    }
    
}




Functional Class

CODE

public class NetWeeklyPay
{
    double weeklyGrossHourly;
    double annual;
    double annualNet;
    double tax;
    
    public double weeklyGrossHourly(double hoursWorked, double hourlyPay)
    {
        weeklyGrossHourly = hoursWorked * hourlyPay;
        return weeklyGrossHourly;
    }
    public double netWeeklyPay()
    {    
        double annual = weeklyGrossHourly * 52;
                if((annual < 12000) && (annual >= 18000))    {
                annual = (annual - 12000) * 0.14;
                return annual;
        }    else if ((annual < 18000) && (annual >= 29000))    {
                annual = 840 + (annual - 18000) * 0.16;
                return annual;
        }    else if (annual < 29000)    {
                annual = 2600 + (annual - 29000) * 0.19;
                return annual;
        }    else    {
                return annual;
        }
    //    return annual;
    }
    public double tax(double annualSalary)
    {
        if((annualSalary < 12000) && (annualSalary >= 18000))    {
                double tax = (annualSalary - 12000) * 0.14;
                return tax;
        }    else if ((annualSalary < 18000) && (annualSalary >= 29000))    {
                double tax = 840 + (annualSalary - 18000) * 0.16;
                return tax;
        }    else if (annualSalary < 29000)    {
                double tax = 2600 + (annualSalary - 29000) * 0.19;
                return tax;
        }    else    {
                tax = 0;
                return tax;
        }
    }
    public double annualNet(double annualSalary)
    {
        annualNet = annualSalary - tax;
        return annualNet;
    }
}

User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Java HW Help- Loops
14 Oct, 2008 - 03:02 PM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,993



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
are you getting any errors?
what specifically is the problem?
User is online!Profile CardPM
+Quote Post

RayJKraft
RE: Java HW Help- Loops
14 Oct, 2008 - 03:20 PM
Post #3

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 5

No compile errors icon_up.gif . Just logical problems. Something wrong with

CODE

  do {
            System.out.print("Enter employee pay type (1 for hourly, 2 for salary): ");
            payType = scan.nextInt();

          if (payType == 1)
            {
                System.out.print("Enter hours worked during the week: ");
                hoursWorked = scan.nextDouble();
                System.out.print("Enter hourly rate: ");
                hourlyPay = scan.nextDouble();    
                weeklySalary = calc.weeklyGrossHourly(hoursWorked, hourlyPay);
                System.out.print("Weekly payroll information for " + name + "\n");
                System.out.print("Gross Pay\t" + weeklySalary +"\n");
                System.out.print("Federal Tax\t" + tax +"\n");
                System.out.print("Net Pay\t" + netWeeklyPay +"\n");
                  [color=#FF0000]  while((hourlyPay >= 5.15) || (hourlyPay <= 25))
                    {
                        System.out.print("error");
                    }[/color]
                break;
            }
            else if (payType == 2) ........


And my methods in the functional class (I think) . I am not getting the correct values (For example 0 for Tax and the same net pay as the gross pay user entered.)

User is offlineProfile CardPM
+Quote Post

RayJKraft
RE: Java HW Help- Loops
14 Oct, 2008 - 03:25 PM
Post #4

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 5

No compile errors icon_up.gif . Just logical problems. Something wrong with the error message in the while loop. It is supposed to print "error" if the hourly rate is not between 5.15 and 25. Instead it calculates the weekly salary regardless of what value is entered, then prints error repeatedly (errorerrorerror)

CODE

  do {
            System.out.print("Enter employee pay type (1 for hourly, 2 for salary): ");
            payType = scan.nextInt();

          if (payType == 1)
            {
                System.out.print("Enter hours worked during the week: ");
                hoursWorked = scan.nextDouble();
                System.out.print("Enter hourly rate: ");
                hourlyPay = scan.nextDouble();    
                weeklySalary = calc.weeklyGrossHourly(hoursWorked, hourlyPay);
                System.out.print("Weekly payroll information for " + name + "\n");
                System.out.print("Gross Pay\t" + weeklySalary +"\n");
                System.out.print("Federal Tax\t" + tax +"\n");
                System.out.print("Net Pay\t" + netWeeklyPay +"\n");
                while((hourlyPay >= 5.15) || (hourlyPay <= 25))
                    {
                        System.out.print("error");
                    }
                break;
            }
            else if (payType == 2) ........


And my methods in the functional class (I think) . I am not getting the correct values (For example 0 for Tax and the same net pay as the gross pay user entered.)

User is offlineProfile CardPM
+Quote Post

mse12
RE: Java HW Help- Loops
14 Oct, 2008 - 03:29 PM
Post #5

D.I.C Head
**

Joined: 21 Sep, 2008
Posts: 55

here is the problem for your tax equaling 0:

else {
tax = 0;
return tax;
}


As far as the same net pay as the gross pay user entered, I cannot seem to find a problem. Let me have another look, but everything seems solid there. Odd...
User is offlineProfile CardPM
+Quote Post

RayJKraft
RE: Java HW Help- Loops
14 Oct, 2008 - 03:45 PM
Post #6

New D.I.C Head
*

Joined: 14 Oct, 2008
Posts: 5

QUOTE(mse12 @ 14 Oct, 2008 - 04:29 PM) *

here is the problem for your tax equaling 0:

else {
tax = 0;
return tax;
}


As far as the same net pay as the gross pay user entered, I cannot seem to find a problem. Let me have another look, but everything seems solid there. Odd...


Thank You, any help's appreciated.

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 01:15PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month