48
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 [email protected]

Structured Problem Solving 2009-2010

Embed Size (px)

DESCRIPTION

Structured Problem Solving 2009-2010. Week 8: Java: Selection and Repetition Stewart Blakeway FML 213 [email protected]. Java: Selection & Repetition. Section 7: Pages 88 to 102. What we have done already. Seen what an algorithm is - PowerPoint PPT Presentation

Citation preview

Page 1: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Structured Problem Solving2009-2010

Week 8: Java: Selection and Repetition

Stewart Blakeway

FML 213

[email protected]

Page 2: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Java: Selection & Repetition

Section 7: Pages 88 to 102

2

Page 3: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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

Page 4: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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

Page 5: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

What we shall do today

• The problem solving constructs in Java

5

Page 6: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

What we shall do today

• The problem solving constructs in Java– Sequence– Selection– Repetition

6

Page 7: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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

Page 8: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Conditions in programming

• algorithm conditions .....

...... must reduce to .....

............. testing the value of numbers

8

Page 9: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Simple numerical conditions

a < b a is less than b

a > b a is greater than b

a == b a equals b

a != b a does not equal b

a >= b a is greater than or equal to b

a <= b a is less than or equal to b

9

Page 10: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Example

int age;

System.in.read(age);if (age<18) { System.out.println("Too young! "); }else { System.out.println("Pint sir? "); }

10

Page 11: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Exampleint age;

System.in.read(age);if (age!=18) { System.out.println("Too young! "); }else { System.out.println("Pint sir? "); }

11

Variations on this

Page 12: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Other kinds of variables

• Character variables – store single characters• Boolean variables – store true or false• String variables

12

Page 13: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Character variables

char c;

c = 'a';

System.out.println("c contains character " + c);

13

Page 14: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Character variableschar type;

System.in.read(type);if (type=='x') { System.out.println("Cross!"); }else { System.out.println("Not a cross!"); }

14

Page 15: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Boolean variablesboolean raining;

raining = true;if (raining == true) { System.out.println("Get an umbrella!"); }else { System.out.println("Get the sun tan lotion!"); }

15

Page 16: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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

Page 17: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

String variablesString studentname;System.in.read(studentname);if (studentname < "N") { System.out.println("1st half of alphabet!"); }else { System.out.println("2nd half of alphabet!"); }

17

Page 18: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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

Page 19: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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

Page 20: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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

Page 21: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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

Page 22: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Compound conditions

• while ((a > 4) && (a < 8))

22

Page 23: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Compound conditions

• while ((a > 4) && (a < 8))

23

&& is the Java (and C) symbol for AND

Page 24: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

SAQ 7.3 – try this now

24

Page 25: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

int count;

int sum;

count = 10;

sum = 50;

if ((count < 10) && (sum <= 100))

{

System.out.println("True");

}

else

{

System.out.println("False");

}

25

Page 26: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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 < 4) || (a > 8))

26

Page 27: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Compound conditions

• while ((a < 4) || (a > 8))

27

Page 28: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Compound conditions

• while ((a < 4) || (a > 8))

28

|| is the Java (and C) symbol for OR

| is on the same key as \ (to the left of the Z key)

Page 29: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

SAQ 7.7 – try this now

29

Page 30: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

int count;

int sum;

count = 10;

sum = 100;

if ((count < 10) || (sum <= 100))

{

System.out.println("True");

}

else

{

System.out.println("False");

}

30

Page 31: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

! - 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

Page 32: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Truth tables

32

Page 33: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

SAQ 7.5 – try this now

33

Page 34: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

• ((month >= 1) && (month <= 12))

34

Page 35: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

• ((answer == ‘y’) || (answer == ‘Y’))

35

Page 36: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE• ((ordertotal > 12) || ((citycode !=locationcode) || (citycode != depot))

36

Page 37: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Programming Repetition

• the while loop• the for loop

37

Page 38: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

The while loop in Java

while (condition)

{

statements to be executed

}

38

Page 39: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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);

Page 40: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Something to Discuss page 97int 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 < 1) || (choice > 5)) { System.out.println("Please Enter Your Choice 1 to 5"); System.in.read(choice); }

40

Page 41: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

41

Design

for 100 times

begindisplay ‘I am sorry’

end

Java

for (i = 1; i <= 100; i++) { System.out.println(“I am sorry”); }

The for Loop in Java

Page 42: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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

Page 43: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Programming Selection

• Depends on the number of alternatives

43

Page 44: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

One alternativeif (condition)

{

statements to be performed when condition is true

}

44

if (age < 18) { System.out.println(“Too young”); }

System.out.println(“Done”);

Java

Page 45: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Two alternatives

if (condition) {

statements to be performed when condition is true }else {

statements to be performed when condition is false }

45

Page 46: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

Two alternatives

46

if (age < 18) { System.out.println(“Too young”); }else { System.out.println(“Pint Sir ?”); }

System.out.println(“Done”);

Java

Page 47: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

More than two alternatives

47

if (size == 1) {

price = 50; }if (size == 2) { price = 70; }if (size == 3) { price = 80; }System.out.println(“Done”);

Page 48: Structured Problem Solving 2009-2010

www.hope.ac.uk Faculty of Sciences and Social Sciences

HO

PE

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