Title: Introduction to Programming and Software Development CMPCD1017
1Introduction to Programming and Software
DevelopmentCMPCD1017
- Session 9 Principles of Programming
2Content
- Session 7 Tutorial Feedback
- Principles of Programming
- Sequence, Choice and Iteration
- Choice Decisions
- If Statements
- If Else Statements
- Comparison and Logic (Boolean) Operators
- Nest If statements
- Tutorial
3Session 7 Tutorial Feedback
- Q1 of Task 1
- There are five people in a room and each person
shakes every other person's hand exactly one
time. How many handshakes will there be? - This question was really all about ambiguous
requirements. Did the text mean, either - Each person has four handshakes, even if theyve
had their hand shook by that person before. - 5 x (5-1) 20
- Each person only shakes the hand of another only
once - (5-1) (5-2) (5-3) (5-4) (5-5) 10
- 4 3 2 1 0 10
4Session 7 Tutorial Feedback
- Q2 of Task 1
- Customers at a particular yogurt shop may select
one of three flavors of yogurt. They may choose
one of four toppings. How many one-flavor,
one-topping combinations are possible? - A simple table, 3 columns by 4 rows would have
shown that a simple multiple calculation solves
this. - 3 x 4 12
- You may want to try one topping, two flavor
combinations?? - Look for a pattern Triangles!!!
5Session 7 Tutorial Feedback
- Task 2
- Pseudo-code for Celsius to Fahrenheit
- Get Celsius (C) from user
- Calculate Fahrenheit (F) 1.8C 32
- Output Fahrenheit
- Pseudo-code for Fahrenheit to Celsius required
you to do some algebra to get C on its own - F 1.8C 32
- F - 32 1.8C
- (F 32) / 1.8 C
- Get Fahrenheit (F) from user
- Calculate Celsius (C) (F 32) / 1.8
- Output Celsius
- This can be checked by dry-running 34 through the
first (using a calculator) and taking the output
and passing it through the second and seeing if
it calculated Celsius to be 34.
6Principles of Programming
- Throughout this module we have highlighted three
programming/design constructs - Sequence
- Iteration (Repetition)
- Choice (Selection)
- It is these constructs that help us to control
the flow of execution of a program and therefore
define its run-time behaviour (Logic). - Over the next two sessions we are going to focus
upon the later two, investigating their sub-types
and how they can be represented in a design
(pseudo-code and flow chart) as well as how they
are written in Java.
7Decisions If Statement
- Some times called Selection Statement
- Decisions are used a lot in programs. Whether the
program has a choice of actions and decides to
take one action or the other. - For example, we might want the computer to test
someones age, and then tell them if they can
vote. - An if statement is used to describe this
situation. - If statements are so important that they are used
in every programming language that has ever been
invented.
8Decision If Statement
- There are two parts to the if statement
- the condition being tested
- the statement(s) to be executed if the condition
is true
Pseudo-Code if Condition to be tested.
The statement to be executed if the condition is
true
Flowchart
Check condition
True
Java Code if (condition to be tested)
Statement(s) to be executed
False
Execute Statement
9Decision If Else statement
- This statement can be used to specify two
sequences of action, that carried out - If the condition is TRUE
- and that carried out if the condition is FALSE.
Pseudo-Code if Condition to be tested. The
statement to be executed if the condition is
true else The statement(s) to be executed if
the condition is false.
Flowchart
Check condition
False
True
Java Code if (condition to be tested)
Statement(s) to be executed else
Statement(s) to be executed
Execute Statement(s)
Execute Different Statement(s)
10Case Study (extending the voting system)
- If we include different actions to be displayed
on whether the AGE is less than or greater than
18. The system should display may vote if the
age greater than or equal to 18, and may not
vote if the age less than 18.
Pseudo-Code get Age from user If Age 18 or above
display message May vote Else
display message may not vote
Flowchart
Input Age
Java Code int Age code to get Age from user if
((Agegt18) ..display may vote else
..display may not vote
True
Age gt18
False
Display May Vote
Display May not Vote
11Case Study The Code
import java.util. public class
session10 public static void main(String
args) // setup data input and declare
variables Scanner dataInput new
Scanner(System.in) int Age System.out.p
rint("Enter Age ") Age dataInput.nextInt()
if (Agegt18) System.out.print(You May
Vote ") else
System.out.print(You May NOT Vote ")
12Comparison Operators
- gt means greater than
- Example Age gt 18
- lt means less than
- Example Age lt 18
- means equals to
- Example Age 18
- ! means not equal to
- Example Age ! 18
- lt means less than or equal to
- Example Age lt 18
- gt means greater or equal to
- Example Age gt 18
13Logical Operators (and, or, not)
- Often in programming, we need to test two things
at once. - The double operator is used for and
Example, we want to test whether someone should
pay a junior rate for a ticket - if ((age gt 6) (age lt 16))
- Pay Junior Rate
- The operator is used for or Example, we
want to test whether someone should pay discount
rate for a ticket - if ((age lt 16) (age gt 65))
- Pay Discount Rate
- The ! operator is meaning not Example, we want
to test whether someone should pay young person
discount rate - if (!(age gt26))
- Pay Young Person Discount Rate
14Nested ifs
- Look at the following nested program
-
- if (age lt 16)
- if (age lt 5)
- display(No Charge)
- else
- display(Junior Rate)
- else
- display(Adult Rate)
-
- The meaning of this nested code is as following
- the first if will check if age is less than 16
- If this is true then the nested if will check
if the age is less than 5 - If this is true, No Charge is displayed
- Else, it is false and Junior Rate is displayed
- Else, first if is false, Adult Age is
displayed.
15Tutorial
- Task 1 (Tom and Jerry)
- Design and write a program that compares the ages
of Tom and Jerry and reports on which one is
older. - Hints
- Toms age different than Jerrys age.
- Toms age could be the same as Jerrys age, use
nested if statement to enhance the first program.
16Tutorial
- Task 2 (tax payment system)
- Design and write a program that calculates how
much tax someone should pay according to the
following rules - People pay no tax if they earn up to 4,000.
- They pay tax at the rate of 20 on the amount
they earn over 4,000 but up to 20,000. - They pay tax at the rate of 25 on the amount
they earn over 20,000 but up to 29,000. - They pat tax at the rate of 40 on any amount
they earn over 29,000. - E.g. if someone earns 35,000 they will pay
- 0 on the first 4000
- 20 on the next 16000 3200
- 25 on the next 9000 2250
- 40 on the last 6000 2400
- 0 3200 2250 2400
- 7850 Tax