Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 8: Java: Selection and Repetition Stewart Blakeway.

Similar presentations


Presentation on theme: "Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 8: Java: Selection and Repetition Stewart Blakeway."— Presentation transcript:

1 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 8: Java: Selection and Repetition Stewart Blakeway FML 213 blakews@hope.ac.uk

2 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Java: Selection & Repetition Section 7: Pages 88 to 102 2

3 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done already Seen what an algorithm is – a set of instructions that, if carried out, will lead to a successful conclusion Learned how to represent algorithms in – Structured English – Flow charts Used variables to remember 3

4 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we have done already Applied the top down, stepwise refinement approach to creating algorithms Looked at problems more oriented towards being solved on a computer – stacks, queues, functions, procedures Seen how high level languages like Java are used to create programs with the aid of compilers 4

5 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we shall do today The problem solving constructs in Java 5

6 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE What we shall do today The problem solving constructs in Java – Sequence – Selection – Repetition 6

7 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Conditions from Problem Solving Conditions in constructs – if today is Tuesday – while there are objects on conveyor belt Too vague for computers – need to be more specific Computers deal only in comparing numbers – JEZ in Threebit 7

8 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Conditions in programming algorithm conditions........... must reduce to.................. testing the value of numbers 8

9 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Simple numerical conditions a < ba is less than b a > ba is greater than b a == ba equals b a != ba does not equal b a >= ba is greater than or equal to b a <= ba is less than or equal to b 9

10 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Example int age; System.in.read(age); if (age<18) { System.out.println( " Too young! " ); } else { System.out.println( " Pint sir? " ); } 10

11 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Example int age; System.in.read(age); if (age!=18) { System.out.println( " Too young! " ); } else { System.out.println( " Pint sir? " ); } 11 Variations on this

12 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Other kinds of variables Character variables – store single characters Boolean variables – store true or false String variables 12

13 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Character variables char c; c = 'a'; System.out.println("c contains character " + c); 13

14 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Character variables char type; System.in.read(type); if (type=='x') { System.out.println("Cross!"); } else { System.out.println("Not a cross!"); } 14

15 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Boolean variables boolean raining; raining = true; if (raining == true) { System.out.println("Get an umbrella!"); } else { System.out.println("Get the sun tan lotion!"); } 15

16 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Boolean variables 16 boolean raining; raining = true; if (raining) { System.out.println("Get an umbrella!"); } else { System.out.println("Get the sun tan lotion!"); } Since a boolean can only have the values true and false we can use it on its own as a condition

17 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE String variables String studentname; System.in.read(studentname); if (studentname < "N") { System.out.println("1st half of alphabet!"); } else { System.out.println("2nd half of alphabet!"); } 17

18 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Comparing char, Boolean, Strings Internally, all data types such as char, boolean and Strings are stored as numbers Comparisons are carried out on those numbers – E.g. ‘A’ is stored as ASCII code 65 – ‘B’ is stored as ASCII code 66 – Thus it is true to say ‘A’ < ‘B’ because of their ASCII codes make it so 18

19 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE ASCII Codes ASCII = American Standard Code for Information Interchange Uses 7 bits – hence numbers 0 to 127 Hence 128 different characters – 94 printable English alphabet upper and lower case Digits Punctuation – 33 non-printing to control printers etc E.g. carriage return – Space Fits into a byte – spare bit used as parity check 19

20 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE ASCII Codes Parity check can be even parity or odd parity Even parity – Set eighth bit to 1 if there are an odd number of 1s in the other seven bits otherwise set it to 0 Odd parity – Set eighth bit to 1 if there are an even number of 1s in the other seven bits otherwise set it to 0 Parity bit used to check data as it is received down a transmission line. – If parity bit is wrong then error must have occurred in transmission 20

21 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Compound conditions We often want to say things like – While a is more than 4 but less then 8 A is to be simultaneously more than 4 and less than 8 – both conditions must be true for the overall condition to be true We write this as – While a is more than 4 AND a is less than 8 In Java we get: – while ((a > 4) && (a < 8)) 21

22 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Compound conditions while ((a > 4) && (a < 8)) 22

23 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Compound conditions while ((a > 4) && (a < 8)) 23 && is the Java (and C) symbol for AND

24 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SAQ 7.3 – try this now 24

25 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int count; int sum; count = 10; sum = 50; if ((count < 10) && (sum <= 100)) { System.out.println("True"); } else { System.out.println("False"); } 25

26 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Compound conditions While a is less than 4 or greater than 8 Here either is enough for the whole condition to be true We write this as – While a is less than 4 OR a is more than 8 In Java we get: – while ((a 8)) 26

27 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Compound conditions while ((a 8)) 27

28 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Compound conditions while ((a 8)) 28 || is the Java (and C) symbol for OR | is on the same key as \ (to the left of the Z key)

29 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SAQ 7.7 – try this now 29

30 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE int count; int sum; count = 10; sum = 100; if ((count < 10) || (sum <= 100)) { System.out.println("True"); } else { System.out.println("False"); } 30

31 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE ! - NOT boolean a; boolean b; a = false; b = !a; 31 If a is true then !a is false If a is false then !a is true

32 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Truth tables 32

33 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE SAQ 7.5 – try this now 33

34 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE ((month >= 1) && (month <= 12)) 34

35 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE ((answer == ‘y’) || (answer == ‘Y’)) 35

36 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE ((ordertotal > 12) || ((citycode !=locationcode) || (citycode != depot)) 36

37 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Programming Repetition the while loop the for loop 37

38 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE The while loop in Java while (condition) { statements to be executed } 38

39 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 39 Design Sum := 0 while Sum is less than 1000 begin display ‘Enter Number’ read(Number) Sum := Sum + Number end display ‘Sum is ’, Sum Java sum = 0; while (sum < 1000) { System.out.print( " Enter Number " ); System.in.read(number); sum = sum + number; } System.out.println( " Sum is " + sum);

40 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Something to Discuss page 97 int choice; choice = 6; System.out.println("1. Display the Time"); System.out.println("2. Display the Date"); System.out.println("3. Display the Weather Forecast"); System.out.println("4. Display the Traffic Report"); System.out.println("5. Exit"); while ((choice 5)) { System.out.println("Please Enter Your Choice 1 to 5"); System.in.read(choice); } 40

41 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE 41 Design for 100 times begin display ‘I am sorry’ end Java for (i = 1; i <= 100; i++) { System.out.println(“I am sorry”); } The for Loop in Java

42 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE for loop variations Java for loops do exactly what they appear to say. How many times do each of the following repeat? – for (i = 5; i <=14; i++) – for (i = 12; i >=8; i--) – for (i = -3; i <=2; i++) 42

43 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Programming Selection Depends on the number of alternatives 43

44 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE One alternative if (condition) { statements to be performed when condition is true } 44 if (age < 18) { System.out.println(“Too young”); } System.out.println(“Done”); Java

45 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Two alternatives if (condition) { statements to be performed when condition is true } else { statements to be performed when condition is false } 45

46 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Two alternatives 46 if (age < 18) { System.out.println(“Too young”); } else { System.out.println(“Pint Sir ?”); } System.out.println(“Done”); Java

47 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE More than two alternatives 47 if (size == 1) { price = 50; } if (size == 2) { price = 70; } if (size == 3) { price = 80; } System.out.println(“Done”);

48 www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE So far we have seen that.. Conditions must be reduced to comparing numbers or chars or booleans or Strings We can make compound conditions with && (AND) and || (OR) symbols in Java Repetition is done with – while { } – for { } Selection is done with – if.{ }... else { } 48


Download ppt "Www.hope.ac.uk Faculty of Sciences and Social Sciences HOPE Structured Problem Solving 2009-2010 Week 8: Java: Selection and Repetition Stewart Blakeway."

Similar presentations


Ads by Google