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)
{
System.out.print ("Enter the number of hours worked:");
hoursWorked = keyboard.nextDouble();
System.out.print ("Enter the number of dependents:");
dependents = keyboard.nextDouble();
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);
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.
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.
Re: Help needed! Calculating weekly pay with if else
Originally Posted by henryswanson
... 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 [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
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?
Re: Help needed! Calculating weekly pay with if else
Originally Posted by henryswanson
... 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 [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Bookmarks