Download - P04 - Arithmetic Operators 1.pdf
-
7/30/2019 P04 - Arithmetic Operators 1.pdf
1/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 1 of 5
Arithmetic Operators (04)
Basic calculations, assignments, unary, type conversion and type casting.
Introduction
A very good idea is to plan the program before start coding it in Java. There are two ways of coding a
program; pseudo-code or flowcharts. Pseudo-code use English like statements to plot the program whilst
flowcharts use graphical symbols. The following program could have been planned using either method as
shown below.
Java Program
class VariablesExample {
public static void main (String args[]){
//variables are declared are assigned
int N1 = 50;
int N2 = 13;
int tot;
//the total of variables N1 and N2
//is stored in tot
tot = N1 + N2;
//Finally, we can show the result
System.out.println(tot);
}
}
-
7/30/2019 P04 - Arithmetic Operators 1.pdf
2/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 2 of 5
Pseudo Code Flowchart
1. Start
2. Store 50 in N1.
3. Store 13 in N2.
4. Add N1 to N2 and store result in tot.
5. Display the value in tot.
6. Stop
As you can see from the flowchart there are different symbols which can be used as shown in the next
table. However, we will see them in more detail later on.
Symbol Name Use
Terminator Starts and ends a program.
Process Processing is anything done by the program which
doesnt require input or output, such as calculations and
variable declaration and assignments.
Decision Whenever a comparison must be done, the decision
symbol must be used and it must have two outputs
which are YES or NO.
Method If the program calls for a method, this symbol must be
used.
Start
N1 = 50
N2 = 13
tot = N1 + N2
Display tot
End
-
7/30/2019 P04 - Arithmetic Operators 1.pdf
3/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 3 of 5
Connector If a shape has more than one input, group them in a
connector beforehand.
Off-Page Connector If the flowchart does not fit on a single page it must be
continued on a different page using an off-page
connector.
Input / Output Whenever user input is required or something is
outputted this symbol must be used.
Arithmetic Operators
The basic arithmetic operators are + (addition), - (subtraction), / (division), * (multiplication) and %
(remainder or modulus). These are called binary operators because they involve two variables. Then there
are the unary operators which require only one variable. All of these operators can be seen in the next
table.
Operator Equivalent Operation Type
+ Addition
Binary
- Subtraction
* Multiplication
/ Division
% Remainder
++ x =x + 1 Increment (add 1)
Unary
-- x =x 1 Decrement (subtract 1)
+= x = x + y Add y to x
-= x = x y Subtract y from x
*= x =x * y Multiply x by y
/= x = x /y Divide x by y
%= x =x % y Divide x by y and get the remainder
-
7/30/2019 P04 - Arithmetic Operators 1.pdf
4/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 4 of 5
So there are various ways of writing the same arithmetic statement. The following table illustrates the
difference.
Operation A
is the same as
Operation B
x = x + 1 x++
x = x 1 x--
x =x + 8 x+=8
x = x 90 x-=90
x = x * 8 x*=8
x = x / 12 x/=12
Also keep in mind the precedence of operators; this means that if various operators are combined in the
same formula, they are worked out in the following manner:
1. Multiplication2. Division3. Remainder4. Addition5. Subtraction
So if we have:
result = 10 + 4 * 3 / 2;
It is worked out as follows:
10 + 4 * 3 / 2
10 + 12 / 2
10 + 6
16
However, brackets can be used which override precedence. So (10+4) * 3 / 2 will be worked out as:(10 + 4) * 3 / 2
(14) * 3 / 2
42 / 2
21
And as you can see its a totally different result.
-
7/30/2019 P04 - Arithmetic Operators 1.pdf
5/5
Computer Studies 2013 Syllabus
Mr. A. Gatt Page 5 of 5
Activities
1. Draw a simple flowchart for the following program.
class averageTwo {
public static void main (String args[]) {
int n1 = 56;
int n2 = 12;
float avg = (n1 + n2) / 2;
System.out.println(Average is +avg);
}
}
2. What is the value of x in the following calculations?a. X = 5%3;b. X = 4 * 6 + (3 9);c. X += 6;
3. Write a program that stores your year of birth in a variable. Then use a calculation to show youractual age.
4. Write a program that shows the result of four calculations (+, -, / and *) of two numbers. The resultsmust be in the following format.
Two Numbers are 5 and 6
Addition : 11
Subtraction : -1
Multiplication : 30
Division : 0.833
***