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

Slides:



Advertisements
Similar presentations
A8 – Control Structures if, if-else, switch Control of flow in Java Any sort of complex program must have some ability to control flow.
Advertisements

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 10: Java: Strings Stewart Blakeway FML 213
08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Programming TBE 540 Farah Fisher. Objectives After viewing this presentation, the learner will be able to… Given a task, create pseudocode Given pseudocode,
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 5: Steps in Problem Solving Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Variables and Trace Tables Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 6: Problem Solving Exercises Stewart Blakeway FML.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 7: Java basics Stewart Blakeway
Faculty of Sciences and Social Sciences HOPE PHP Flow Control Website Development Stewart Blakeway FML
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 12: Data Structures 1 Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Java: Loops within loops Stewart Blakeway FML 213
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving An Introduction Stewart Blakeway
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
COMP 14 Introduction to Programming Mr. Joshua Stough February 7, 2005 Monday/Wednesday 11:00-12:15 Peabody Hall 218.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Logical Operators and Conditional statements
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Repetition Statements.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
CHARACTERS Data Representation. Using binary to represent characters Computers can only process binary numbers (1’s and 0’s) so a system was developed.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
CIS162AD - C# Decision Statements 04_decisions.ppt.
CIS Computer Programming Logic
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Computer Programming I. Today’s Lecture  Components of a computer  Program  Programming language  Binary representation.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Computer Science Selection Structures.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CPS120: Introduction to Computer Science
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Flow of Control Part 1: Selection
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Flow of Control Chapter 3 Flow of control Branching Loops
Algorithm Design.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Introduction to Java Java Translation Program Structure
Representing Characters in a computer Pressing a key on the computer a code is generated that the computer can convert into a symbol for displaying or.
Chapter 4 Control Structures: Part I 1 3 “ There is No goto in Java ” Structured programming: the building blocks There are 3 different kinds.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 3: Algorithms Stewart Blakeway FML 213
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
1 CS161 Introduction to Computer Science Topic #8.
CPS120: Introduction to Computer Science Variables and Constants.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Java I--Copyright © Tom Hunter. Chapter 4 Control Structures: Part I.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Selection Using IF THEN ELSE CASE Introducing Loops.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Variables and Primative Types
Java Programming Control Structures Part 1
Presentation transcript:

Faculty of Sciences and Social Sciences HOPE Structured Problem Solving Week 8: Java: Selection and Repetition Stewart Blakeway FML 213

Faculty of Sciences and Social Sciences HOPE Java: Selection & Repetition Section 7: Pages 88 to 102 2

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

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

Faculty of Sciences and Social Sciences HOPE What we shall do today The problem solving constructs in Java 5

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

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

Faculty of Sciences and Social Sciences HOPE Conditions in programming algorithm conditions must reduce to testing the value of numbers 8

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

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

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

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

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

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

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

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

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

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

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

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

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

Faculty of Sciences and Social Sciences HOPE Compound conditions while ((a > 4) && (a < 8)) 22

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

Faculty of Sciences and Social Sciences HOPE SAQ 7.3 – try this now 24

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

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

Faculty of Sciences and Social Sciences HOPE Compound conditions while ((a 8)) 27

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)

Faculty of Sciences and Social Sciences HOPE SAQ 7.7 – try this now 29

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

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

Faculty of Sciences and Social Sciences HOPE Truth tables 32

Faculty of Sciences and Social Sciences HOPE SAQ 7.5 – try this now 33

Faculty of Sciences and Social Sciences HOPE ((month >= 1) && (month <= 12)) 34

Faculty of Sciences and Social Sciences HOPE ((answer == ‘y’) || (answer == ‘Y’)) 35

Faculty of Sciences and Social Sciences HOPE ((ordertotal > 12) || ((citycode !=locationcode) || (citycode != depot)) 36

Faculty of Sciences and Social Sciences HOPE Programming Repetition the while loop the for loop 37

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

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

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

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

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

Faculty of Sciences and Social Sciences HOPE Programming Selection Depends on the number of alternatives 43

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

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

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

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

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