480 likes | 590 Views
Structured Problem Solving 2009-2010. Week 8: Java: Selection and Repetition Stewart Blakeway FML 213 blakews@hope.ac.uk. Java: Selection & Repetition. Section 7: Pages 88 to 102. What we have done already. Seen what an algorithm is
E N D
Structured Problem Solving2009-2010 Week 8: Java: Selection and Repetition Stewart Blakeway FML 213 blakews@hope.ac.uk
Java: Selection & Repetition Section 7: Pages 88 to 102
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
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
What we shall do today The problem solving constructs in Java
What we shall do today The problem solving constructs in Java Sequence Selection Repetition
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
Conditions in programming algorithm conditions ..... ...... must reduce to ..... ............. testing the value of numbers
Example int age; System.in.read(age); if (age<18) { System.out.println("Too young! "); } else { System.out.println("Pint sir? "); }
Example int age; System.in.read(age); if (age!=18) { System.out.println("Too young! "); } else { System.out.println("Pint sir? "); } Variations on this
Other kinds of variables Character variables – store single characters Boolean variables – store true or false String variables
Character variables char c; c = 'a'; System.out.println("c contains character " + c);
Character variables char type; System.in.read(type); if (type=='x') { System.out.println("Cross!"); } else { System.out.println("Not a cross!"); }
Boolean variables boolean raining; raining = true; if (raining == true) { System.out.println("Get an umbrella!"); } else { System.out.println("Get the sun tan lotion!"); }
Boolean variables Since a boolean can only have the values true and false we can use it on its own as a condition boolean raining; raining = true; if (raining) { System.out.println("Get an umbrella!"); } else { System.out.println("Get the sun tan lotion!"); }
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!"); }
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
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
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
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))
Compound conditions while ((a > 4) && (a < 8))
Compound conditions while ((a > 4) && (a < 8)) && is the Java (and C) symbol for AND
int count; int sum; count = 10; sum = 50; if ((count < 10) && (sum <= 100)) { System.out.println("True"); } else { System.out.println("False"); }
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))
Compound conditions while ((a < 4) || (a > 8))
Compound conditions while ((a < 4) || (a > 8)) || is the Java (and C) symbol for OR | is on the same key as \ (to the left of the Z key)
int count; int sum; count = 10; sum = 100; if ((count < 10) || (sum <= 100)) { System.out.println("True"); } else { System.out.println("False"); }
! - NOT boolean a; boolean b; a = false; b = !a; If a is true then !a is false If a is false then !a is true
((ordertotal > 12) || ((citycode !=locationcode) || (citycode != depot))
Programming Repetition the while loop the for loop
The while loop in Java while (condition) { statements to be executed }
Java sum = 0; while (sum < 1000) { System.out.print("Enter Number "); System.in.read(number); sum = sum + number; } System.out.println("Sum is " + sum); Design Sum := 0 while Sum is less than 1000 begin display ‘Enter Number’ read(Number) Sum := Sum + Number end display ‘Sum is ’ , Sum
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 < 1) || (choice > 5)) { System.out.println("Please Enter Your Choice 1 to 5"); System.in.read(choice); }
The for Loop in Java Design for 100 times begin display ‘I am sorry’ end Java for (i = 1; i <= 100; i++) { System.out.println(“I am sorry”); }
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++)
Programming Selection Depends on the number of alternatives
One alternative if (condition) { statements to be performed when condition is true } Java if (age < 18) { System.out.println(“Too young”); } System.out.println(“Done”);
Two alternatives if (condition) { statements to be performed when condition is true } else { statements to be performed when condition is false }
Two alternatives Java if (age < 18) { System.out.println(“Too young”); } else { System.out.println(“Pint Sir ?”); } System.out.println(“Done”);
More than two alternatives if (size == 1) { price = 50; } if (size == 2) { price = 70; } if (size == 3) { price = 80; } System.out.println(“Done”);
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 { }