Problem: Suppose we need a program to calculate someone s net pay. For input the program requires a name, gross pay, deductions and a tax rate. The output comprises the name, gross pay, deductions, taxes paid, and net pay. The program determines the taxes to be paid and the net pay (i.e. the processing) as follows: taxes paid is (gross pay deductions) * tax rate net pay is gross pay deductions taxes paid Variable names in Java cannot have spaces in them and so the usual names given for the above concepts are: taxespaid, grosspay, deductions, taxrate and netpay. The naming convention for variables is to start with lower case and then capitalize the first letter of subsequent words. The above calculations are expressed in Java using assignment statements: taxespaid = (grosspay deductions) * taxrate netpay = grosspay deductions taxespaid Java considers = to be the assignment operator. Whatever the right-hand-side evaluates to is assigned to the variable on the left-hand-side. The = operator has the lowest priority and so the assignment is the last thing to be done when statements like the above execute. A recommended programming practice is to develop code in small steps. In the following we show how someone could develop the code iteratively: 1. Add a little bit of code 2. Compile 3. Debug 4. Repeat the above until all requirements are implemented.
Step 1: Code the application class with no code in the main method. Create a new class in a Java project, and edit your code to be: Make sure the code you enter compiles with no syntax errors. Java has keywords such as public, class, static, and void that must be entered in lowercase. Punctuation comprises { and, ( and ), and [ and ] ; these must all appear in pairs.
Step 2: Get the user s name and display the name. We will use standard input and output for this. That is, we will use a Scanner object and System.out. To get the user s name we first of all prompt the user and then get the value entered. The code below does this note new lines are highlighted in bold text: There are several points to note about the highlighted lines above: The first line is called an import statement. It is necessary to include as we must inform the compiler where to find a definition of the Scanner class. The lines that begin // are single-line comments. Their purpose is to help humans read code these lines are ignored by a compiler. Some programmers will use these to help others understand how the code has been organized. The line declares a variable named keyboard to be of type Scanner. On the right-hand-side of the = sign is how one instantiates a Scanner object that is standard input. The line causes output to be written to the standard output device (BlueJ s terminal window). Obviously this informs the user to use the keyboard to enter their name. The next line is an assignment statement: When this line executes the expression keyboard.next() must be evaluated. This expression has no value until the user types on the keyboard and presses the enter button. So the effect is that the program is paused until the moment the user completes his/her action. The line displays the value of the expression enclosed in parentheses on the standard output device. Note the argument being passed in to the println method is "Name: "+name In this situation the + operator is called string catenation (one string is appended to the other). The + operator works differently depending on the type involved (numbers will be added together, strings will be catenated).
Step 3: Get the gross pay from the user. To add this functionality to our program we need another prompt and another action to get the gross pay from the user. Suppose a user might enter gross pay with a decimal point (e.g. 1500.25); to allow this we will use the double datatype for gross pay, and the nextdouble method of the Scanner class. The code below does this note the new lines are highlighted in bold text: Something to note about the lines above: The line uses the nextdouble method of the Scanner class. If a user might supply a numeric value that has a decimal point we must use this method.
Step 4: Get the deductions from the user. The code to do this is highlighted below: // get deductions System.out.println("enter deductions:"); double deductions = keyboard.nextdouble(); System.out.println("Deductions: "+grosspay);
Step 5: Get the tax rate from the user. The code to do this is highlighted below: // get deductions System.out.println("enter deductions:"); double deductions = keyboard.nextdouble(); // get tax rate System.out.println("enter the tax rate:"); double taxrate = keyboard.nextdouble(); System.out.println("Deductions: "+deductions);
Step 6: Calculate and display the taxes to be paid. The code to do this is highlighted below: // get deductions System.out.println("enter deductions:"); double deductions = keyboard.nextdouble(); // get tax rate System.out.println("enter the tax rate:"); double taxrate = keyboard.nextdouble(); // calculate the taxes to be paid double taxespaid = (grosspay - deductions) * taxrate; System.out.println("Deductions: "+deductions); System.out.println("Taxes Paid: "+taxespaid); The statement (grosspay deductions) * taxrate; shows the use of the * operator for multiplication and the use of parentheses to create a subexpression. Sub-expressions are always evaluated first. Can you see why a sub-expression is necessary here?
Step 7: Calculate and display the net pay. The added code is highlighted below: // get deductions System.out.println("enter deductions:"); double deductions = keyboard.nextdouble(); // get tax rate System.out.println("enter the tax rate:"); double taxrate = keyboard.nextdouble(); // calculate the taxes to be paid double taxespaid = (grosspay - deductions) * taxrate; // calculate the net pay double netpay = grosspay - deductions - taxespaid; System.out.println("Deductions: "+deductions); System.out.println("Taxes Paid: "+taxespaid); System.out.println("Net pay: "+netpay);