CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2011
    Posts
    0

    Help needed! Calculating weekly pay with if else

    Hi, I am really new to programming and am trying to create a program that calculates an employee's weekly pay.

    I have worked out the calculations and tested them outside of Java and everything is fine but once I put it all in I ended up with various errors - which I managed to fix (or so I thought) and compiled the program but came back with the wrong figures for "Weekly Income Tax" and the "Take Home Pay".

    Weekly Income Tax is based solely upon a calculation for Yearly Income Tax which is meant to be calculated using if statements (and/or boolean expression).

    I may be totally wrong ... but if anyone can help it would be appreciated!

    Also I need to correct the decimals to 2 decimal places but have no idea how to implement this nor do I know how to add extra lines between the outputs.

    Thanks in advance!

    PHP Code:
    // This program is designed to calculate an employee's pay

    import java.util.Scanner;

    public class 
    PayCalc
    {
        public static 
    void main (String [] args)
        {

        
    Scanner keyboard = new Scanner (System.in);

        
    Double hoursWorked;
        
    Double dependents;
        
    Double grossPay;
        
    Double adjGross;
        
    Double mediLevy;
        
    Double projectedAnnual;
            
    Double yearTax 0.00;
        
    Double weekTax;
        
    Double weekAssess;
        
    Double weekTHome;
            
    Double employSuper;
            
    Double employeeSuper 100.00;
        
    Double childSupport 50.00;

        
    System.out.print ("Enter the number of hours worked:");
        
    hoursWorked keyboard.nextDouble();
        
    System.out.print ("Enter the number of dependents:");
        
    dependents keyboard.nextDouble();

        
    grossPay = (38*15.50)+((hoursWorked-38)*(15.50*1.5));
        
    adjGross grossPay-employeeSuper;
        
    mediLevy grossPay*0.015;
        
    projectedAnnual adjGross*52;
        
    boolean condition true;
        if (
    projectedAnnual <= 6000) {
            
    yearTax 0.00;
        } else if (
    projectedAnnual >= 6000)  {
            
    yearTax = ((projectedAnnual-6000)*0.15);
        } else if (
    projectedAnnual >= 25000)  {
            
    yearTax = (25000-6000)*0.15+((projectedAnnual-25000)*0.30);
        } else if (
    projectedAnnual >= 75000)  {
            
    yearTax = (25000-6000)*0.15+(75000-25000)*0.30+((projectedAnnual-75000)*0.40);
            } else if (
    projectedAnnual >= 150000)  {
            
    yearTax = (25000-6000)*0.15+(75000-25000)*0.30+(150000-75000)*0.40+((projectedAnnual-150000)*0.45);
        }
          
    weekTax yearTax/52;
          
    weekAssess grossPay-employeeSuper;
          
    weekTHome weekAssess-weekTax-mediLevy-childSupport;
          
    childSupport dependents*50.00;
          
    employeeSuper 100.00;
          
    employSuper grossPay*0.09;

        
    System.out.println("Worker's projected yearly gross salary          :"  +projectedAnnual);

        
    System.out.println("Superannuation Contribution");
        
    System.out.println("Employer contribution                           :"  +employSuper);
        
    System.out.println("Employee contribution                           :"  +employeeSuper);

        
    System.out.println("Worker's weekly assessable income               :"  +weekAssess);
        
    System.out.println("Weekly income tax                               :"  +weekTax);
        
    System.out.println("Medicare levy                                   :"  +mediLevy);
        
    System.out.println("Child Support contribution                      :"  +childSupport);

        
    System.out.println("Take home pay                                   :"  +weekTHome);

        
        }
        


  2. #2
    Join Date
    Oct 2010
    Posts
    60

    Re: Help needed! Calculating weekly pay with if else

    Is the Yearly Income Tax correct? I think there is an issue in the if-else statements. You have 5 ranges, 0-6000, 6000-25000, 25000-75000, and 75000-150000. For one, you need a 150000+ range. But more importantly, a number like 99999999 will fall into the projectedAnnual >= 6000 range. Do you see why?

    Also, you don't need to use Double. It's better to use double. Double is an object containing the primitive type 'double'. In addition, you have several places where you have something like (75000-25000). Why not put in 50000? It will make the code easier to read.

  3. #3
    Join Date
    Oct 2010
    Posts
    60

    Re: Help needed! Calculating weekly pay with if else

    For the formatting, use System.out.printf("Here is some text with a value %.2f and some more text.\n", value). The % means that a value will be inserted into the text. The .2 means to format two decimal places. The f means the variable is a float-point type variable. The \n is the way to specify a new line.

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help needed! Calculating weekly pay with if else

    Quote Originally Posted by henryswanson View Post
    ... The \n is the way to specify a new line.
    Not necessarily. The line separator or newline is platform dependent. You can obtain the system line separator by calling System.getProperties("line.separator"). For applications, this would normally be obtained once and stored in a static variable.

    Experience is a poor teacher: it gives its tests before it teaches its lessons...
    Anon.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Oct 2010
    Posts
    60

    Re: Help needed! Calculating weekly pay with if else

    Whoops, sorry about that. Too bad there's no printfln(). Also, could he call println() once after each printf()? Or is that slower/less readable than getting the newline separator?

  6. #6
    Join Date
    Mar 2011
    Posts
    3

    Re: Help needed! Calculating weekly pay with if else

    To print out the double value to two decimal places:

    First:
    import java.util.DecimalFormat;

    Then create a new format object:
    DecimalFormat frmt = new DecimalFormat("0.00");

    Then to print out:
    //doubleValue is the double you wanted to be formatted
    System.out.println("Salary: " + frmt.format(doubleValue));

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Help needed! Calculating weekly pay with if else

    Quote Originally Posted by henryswanson View Post
    ... could he call println() once after each printf()? Or is that slower/less readable than getting the newline separator?
    Yup, calling println() is fine, just as readable AFAICS, and probably easier in a short program. Speed shouldn't be an issue with that kind of I/O

    In theory, there is no difference between theory and practice, but not in practice...
    Anon.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured