Java Planning our Programs Flowcharts Arithmetic Operators Planning

  • Slides: 17
Download presentation
Java Planning our Programs Flowcharts Arithmetic Operators

Java Planning our Programs Flowcharts Arithmetic Operators

Planning Programs �It is very important to plan our programs before we start coding

Planning Programs �It is very important to plan our programs before we start coding �There are two ways of planning a program; 1. Pseudo-code = makes use of English statements to plot the program 2. Flowcharts = use graphical symbols

Example �Lets say we had the following program class Variables. Example { public static

Example �Lets say we had the following program class Variables. Example { public static void main (String args[]){ //variables are declared and assigned int N 1 = 50; int N 2 = 13; int tot; //the total of variables N 1 and N 2 //is stored in tot = N 1 + N 2; //Finally, we can show the result System. out. println(tot); } }

Pseudo-code Plan �The following is the Pseudo-code Plan for the previous program; 1. Start

Pseudo-code Plan �The following is the Pseudo-code Plan for the previous program; 1. Start 2. Store 50 in N 1. 3. Store 13 in N 2. 4. Add N 1 to N 2 and store result in tot. 5. Display the value in tot. 6. Stop

Flowchart Plan �The following is the Flowchart Plan of the previous program; Start N

Flowchart Plan �The following is the Flowchart Plan of the previous program; Start N 1 = 50 N 2 = 13 tot = N 1 + N 2 Display tot End

Why do we Plan Programs? �We plan our programs in order to know what

Why do we Plan Programs? �We plan our programs in order to know what we will be doing before we start coding �With a plan it will be much easier to know what structure our program will have �Planning easier makes programming much

Flowcharts �A flowchart is basically a graphical presentation of our program �A flowchart is

Flowcharts �A flowchart is basically a graphical presentation of our program �A flowchart is very is to read and understand �Flowcharts break down our programs into many steps

Flowchart Symbols

Flowchart Symbols

Terminator �The terminator is used to show 1. The start and 2. The end

Terminator �The terminator is used to show 1. The start and 2. The end of a program START END

Process �A process is any action to be done by the program The process

Process �A process is any action to be done by the program The process in this case is declaring two variables N 1 = 50 N 2 = 13

Decision �Decision are used when we have a comparison �Decisions have two outputs which

Decision �Decision are used when we have a comparison �Decisions have two outputs which are YES or NO. Is it Raining ? NO YES

Input/Output �The Input/ Output symbol is used 1. When the program requires and input

Input/Output �The Input/ Output symbol is used 1. When the program requires and input 2. When the program results in an output In this case the output to be shown is the contents of tot Display tot

Try it out … class Variable{ �Create public static void main (String args[]){ //variables

Try it out … class Variable{ �Create public static void main (String args[]){ //variables are declared and assigned 1. The pseudo-codes plan int N 1 = 20; 2. The flowchart plan int N 2 = 10; int tot; for the following program; int tot 2; //tot 1 and tot 2 declared tot = N 1 + N 2; tot 2 = N 1 – N 2; //Finally, we can show the result System. out. println(tot 2); } }

Arithmetic Operators �Arithmetic Operators are used to perform mathematical calculations �The 1. 2. 3.

Arithmetic Operators �Arithmetic Operators are used to perform mathematical calculations �The 1. 2. 3. 4. 5. basic arithmetic operators ; + (addition) These are called - (subtraction) binary operators / (division) because they need * (multiplication) to use at least two % (remainder) variables

Unary Operators �Then are also what we call unary operators �These only need one

Unary Operators �Then are also what we call unary operators �These only need one variable 1. ++ (increment by 1) 2. -- (decrement by 1) 3. variable += x (same as variable = variable + x) variable -= x (same as variable = variable - x) 5. variable *= x (same as variable = variable * x) 6. variable /= x (same as variable = variable / x) 7. variable %= x (same as variable = variable % 4.

Combining Operators �We could create a formula by combining a number of operators X

Combining Operators �We could create a formula by combining a number of operators X = 10 + 4 * 3 / 2; �The order the operators are work out is the following; 1. 2. 3. 4. 5. Multiplication Division Remainder Addition Subtraction 10 + 4 * 3 / 2 10 + 12 / 2 10 + 6 16

Use of Brackets �When we use brackets in our formula we are telling the

Use of Brackets �When we use brackets in our formula we are telling the program which operations to calculate first X = (10 + 4) * 3 / 2; As we can see the result has changed!! (10 + 4 ) * 3 / 2 14 * 3 / 2 42 / 2 21