Download exercise - acs.uwinnipeg.ca

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ACS-1903
Basic program structure
Feb 16, 2017
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.
2.
3.
4.
Add a little bit of code
Compile
Debug
Repeat the above until all requirements are implemented.
ACS-1903
Basic program structure
Feb 16, 2017
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:
public class CalculateNetPay {
public static void main(String[] args){
}
}
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.
ACS-1903
Basic program structure
Feb 16, 2017
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:
import java.util.Scanner;
public class CalculateNetPay {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
// get name
System.out.println("enter your name with no spaces");
String name = keyboard.next();
// display information to user
System.out.println("Name: "+name);
}
}
There are several points to note about the highlighted lines above:
 The first line
import java.util.Scanner;


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
Scanner keyboard = new Scanner(System.in);

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
System.out.println("enter your name with no spaces");

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:
String name = keyboard.next();

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
System.out.println("Name: "+name);
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).
ACS-1903
Basic program structure
Feb 16, 2017
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:
import java.util.Scanner;
public class CalculateNetPay {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
// get name
System.out.println("enter your name with no spaces");
String name = keyboard.next();
// get gross pay
System.out.println("enter the gross pay:");
double grossPay = keyboard.nextDouble();
// display information to user
System.out.println("Name: "+name);
System.out.println("Gross pay: "+grossPay);
}
}
Something to note about the lines above:
 The line
double grossPay = keyboard.nextDouble();
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.
ACS-1903
Basic program structure
Step 4: Get the deductions from the user. The code to do this is highlighted below:
import java.util.Scanner;
public class CalculateNetPay {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
// get name
System.out.println("enter your name with no spaces");
String name = keyboard.next();
// get gross pay
System.out.println("enter the gross pay:");
double grossPay = keyboard.nextDouble();
// get deductions
System.out.println("enter deductions:");
double deductions = keyboard.nextDouble();
// display information to user
System.out.println("Name: "+name);
System.out.println("Gross pay: "+grossPay);
System.out.println("Deductions: "+grossPay);
}
}
Feb 16, 2017
ACS-1903
Basic program structure
Step 5: Get the tax rate from the user. The code to do this is highlighted below:
import java.util.Scanner;
public class CalculateNetPay {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
// get name
System.out.println("enter your name with no spaces");
String name = keyboard.next();
// get gross pay
System.out.println("enter the gross pay:");
double grossPay = keyboard.nextDouble();
// 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();
// display information to user
System.out.println("Name: "+name);
System.out.println("Gross pay: "+grossPay);
System.out.println("Deductions: "+deductions);
}
}
Feb 16, 2017
ACS-1903
Basic program structure
Feb 16, 2017
Step 6: Calculate and display the taxes to be paid. The code to do this is highlighted below:
import java.util.Scanner;
public class CalculateNetPay {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
// get name
System.out.println("enter your name with no spaces");
String name = keyboard.next();
// get gross pay
System.out.println("enter the gross pay:");
double grossPay = keyboard.nextDouble();
// 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;
// display information to user
System.out.println("Name: "+name);
System.out.println("Gross pay: "+grossPay);
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?
ACS-1903
Basic program structure
Step 7: Calculate and display the net pay. The added code is highlighted below:
import java.util.Scanner;
public class CalculateNetPay {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
// get name
System.out.println("enter your name with no spaces");
String name = keyboard.next();
// get gross pay
System.out.println("enter the gross pay:");
double grossPay = keyboard.nextDouble();
// 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;
// display information to user
System.out.println("Name: "+name);
System.out.println("Gross pay: "+grossPay);
System.out.println("Deductions: "+deductions);
System.out.println("Taxes Paid: "+taxesPaid);
System.out.println("Net pay: "+netPay);
}
}
Feb 16, 2017