1 / 48

Structured Problem Solving 2009-2010

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

Download Presentation

Structured Problem Solving 2009-2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Structured Problem Solving2009-2010 Week 8: Java: Selection and Repetition Stewart Blakeway FML 213 blakews@hope.ac.uk

  2. Java: Selection & Repetition Section 7: Pages 88 to 102

  3. 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

  4. 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

  5. What we shall do today The problem solving constructs in Java

  6. What we shall do today The problem solving constructs in Java Sequence Selection Repetition

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

  8. Conditions in programming algorithm conditions ..... ...... must reduce to ..... ............. testing the value of numbers

  9. Simple numerical conditions

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

  11. Example int age; System.in.read(age); if (age!=18) { System.out.println("Too young! "); } else { System.out.println("Pint sir? "); } Variations on this

  12. Other kinds of variables Character variables – store single characters Boolean variables – store true or false String variables

  13. Character variables char c; c = 'a'; System.out.println("c contains character " + c);

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

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

  16. 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!"); }

  17. 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!"); }

  18. 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

  19. 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

  20. 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

  21. 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))

  22. Compound conditions while ((a > 4) && (a < 8))

  23. Compound conditions while ((a > 4) && (a < 8)) && is the Java (and C) symbol for AND

  24. SAQ 7.3 – try this now

  25. int count; int sum; count = 10; sum = 50; if ((count < 10) && (sum <= 100)) { System.out.println("True"); } else { System.out.println("False"); }

  26. 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))

  27. Compound conditions while ((a < 4) || (a > 8))

  28. 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)

  29. SAQ 7.7 – try this now

  30. int count; int sum; count = 10; sum = 100; if ((count < 10) || (sum <= 100)) { System.out.println("True"); } else { System.out.println("False"); }

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

  32. Truth tables

  33. SAQ 7.5 – try this now

  34. ((month >= 1) && (month <= 12))

  35. ((answer == ‘y’) || (answer == ‘Y’))

  36. ((ordertotal > 12) || ((citycode !=locationcode) || (citycode != depot))

  37. Programming Repetition the while loop the for loop

  38. The while loop in Java while (condition) { statements to be executed }

  39. 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

  40. 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); }

  41. 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”); }

  42. 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++)

  43. Programming Selection Depends on the number of alternatives

  44. 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”);

  45. Two alternatives if (condition) { statements to be performed when condition is true } else { statements to be performed when condition is false }

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

  47. More than two alternatives if (size == 1) { price = 50; } if (size == 2) { price = 70; } if (size == 3) { price = 80; } System.out.println(“Done”);

  48. 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 { }

More Related