Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans,

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Program Design and Development
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Review COMP 102 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
©Xiaoying Gao, Peter Andreae First Java Program COMP 102 #2 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
PYTHON CONDITIONALS AND RECURSION : CHAPTER 5 FROM THINK PYTHON HOW TO THINK LIKE A COMPUTER SCIENTIST.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
Agenda Exam #1 Review Modulus Conditionals Boolean Algebra Reading: Chapter Homework #5.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
ITBP 119 Algorithms and Problem Solving Section 2.1 Installing software Section 2.2 First Programs Section 2.3 Variables.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Chapter 2 Using Objects. Types A type defines a set of values and the operations that can be carried out on the values Examples: 13 has type int "Hello,
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Lesson Two: Everything You Need to Know
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Selection Statements. Introduction Today we learn more about learn to make decisions in Turing ▫Nested if statements, ▫case statements.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Creating Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington UI methods, Graphical.
2011-T1 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
© Xiaoying Gao, Peter Andreae Variables, Constants, UI methods COMP 102 #4 2014T2 Xiaoying Sharon Gao Computer Science Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington UI methods, Graphical Output.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
Test Review. General Info. All tests will be comprehensive. You will be tested more on your understanding of code as opposed to your ability to write.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Dealing with Files COMP 102.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Random Functions Selection Structure Comparison Operators Logical Operator
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
The Selection Structure
Chapter 4 – Control Structures Part 1
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Conditions and Ifs BIS1523 – Lecture 8.
Selection Statements.
Chapter 2 Programming Basics.
Chapter 3: Selection Structures: Making Decisions
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Chapter 3: Selection Structures: Making Decisions
Unit 3: Variables in Java
CS Week 2 Jim Williams, PhD.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Programs with Choice Booleans, COMP 102 #

© Peter Andreae COMP :2 Menu Programs with ChoiceLDC 4.2 if ( … ) { … } else { … } Boolean Expressions LDC 4.1, 4.3 Tomorrow: Programs with repetitionLDC 4.5 while (…) {….} Admin: Test Monday 31 st 5-6pm. (6-7pm for PHYS 114 students) Lectures + assig 2&3: LDC chapters 1 – 4 (but not 3.7, 4.4, 4.6-8) Practice reading code to work out what it does Practice writing code on paper and checking it. Past tests on website Forum and online help.

© Peter Andreae COMP :3 Where have we been? Classes and methods, (to specify objects) class headers, method headers with parameters and return type Statements, (to specify instructions): calling methods on objects, specifying arguments text output, text input, graphical output. declaring variables, assigning values to variables return Expressions, (to specify values) literal values variables combining values with operations (+, -, *, etc) making new objects calling methods that return values

© Peter Andreae COMP :4 Programs that make decisions Programs that perform the same action every time are boring! You can vary the action in a program By calling method with different arguments: UI.setColor(col); UI.drawRect(left, top, width, height); By getting input from the user: String name = UI.askString("name:"); : UI.printf("Hello %s, how are you?", name);

© Peter Andreae COMP :5 Programs that make decisions But this just changes the values, not the action itself. To vary the action inside a method: Need a conditional, or choice, statement: IF some condition is true THEN do this action ELSE do that action We do this in English instructions all the time: IF you submitted your reflection.txt file last week THEN raise your hand If your name starts with “A” or your name starts with “J” Then draw a small circle in the top left corner of your notes Else draw a small square in the bottom right corner of your notes.

© Peter Andreae COMP :6 Decisions in Java Java has an if … else … statement: Can do an action only in some circumstances: if ( action.equals("reset") ) { UI.clearGraphics(); this.drawBoard(10, 10, 100); } Can choose between different actions: if ( userChoice.equals("R" ) { UI.drawRect(left, top, width, height); } else { UI.fillOval(left, top, width, height); }

© Peter Andreae COMP :7 Java: if and if … else LDC 4.2 Two forms of the if statement: if (〈condition 〉) { 〈actions to perform if condition is true 〉 } ⇒ just skip the actions when the condition is not true ! and if (〈condition 〉 ) { 〈actions to perform if condition is true 〉 } else { 〈actions to perform if condition is false 〉 } Note: the { … } represent a "Block" – a sequence of actions that are wrapped up together into a single statement.

© Peter Andreae COMP :8 if … vs if … else … if(boolean valued expression{ } else statements ) { }

© Peter Andreae COMP :9 Method with a condition /** Ask for amount and currency; print note if –ve, print value.*/ public void convertMoney( ) { ; double amt = UI.askDouble("Enter amount $NZ"); if ( amt < 0 ) { UI.println("Note: you have entered a debt!"); } String cur = UI.askString ("Enter currency (US or Aus)"); if ( cur.equals("US") ) { UI.printf("$NZ%4.2f = $US%4.2f\n", amt, (amt * 0.815)); } else { UI.printf("$NZ%4.2f = $AUS%4.2f\n", amt, (amt * 0.779)); } }

© Peter Andreae COMP :10 Multiway choice: if … else if … else if … Can put another if statement in the else part: if (〈condition1 〉 ) { 〈actions to perform if condition1 is true〉 : } else if (〈condition2 〉 ) { 〈actions to perform if condition 2 is true (but not condition 1) 〉 : } else if (〈condition3 〉 ) { 〈actions to perform if condition 3 is true (but not conditions 1, 2)〉 : } else { 〈actions to perform if other conditions are false〉 : }

© Peter Andreae COMP :11 Example with multiway choice public void convertMoney( ) { double amt = UI.askDouble("Enter amount"); if ( amt < 0 ) { UI.println("Note: you have entered a debt!"); } String cur = UI.askString("Enter currency (US or Aus)"); if ( cur.equals("US") ) { UI.printf("$NZ%.2f = $US%.2f\n", amt, amt * 0.751); } else if ( cur.equals("Aus") ) { UI.printf("$NZ%.2f = $AUS%.2f\n", amt, amt * 0.741); } else { UI.printf("I cannot convert to %s currency\n", cur); }

© Peter Andreae COMP :12 Example 2 with multi way choice public void printPay( int day, int hours ) { double rate = 13.45; double pay = rate * hours; if ( day > 7 ) { UI.println(" Day must be between 1 and 7 "); } else if ( day < 6 ) { UI.printf("Pay = $ %4.2f \n", pay); } else if ( day == 6 ) { pay = pay * 1.5; UI.printf("Pay = $ %4.2f ( time-and-a-half) \n", pay); } else { pay = pay * 2; UI.printf("Pay = $ %4.2f ( double-time) \n", pay); }

© Peter Andreae COMP :13 Boolean expressions LDC 4.1 What can go in the condition of an if statement? A Boolean value – a value that is either true or false. Boolean expressions: constant values:true, false numeric comparisons:(x > 0) (day <= 7), (x == y), (day != 7) boolean method calls:month.equals("July") UI.hasNextInt() boolean variables:outlineOnly [ if declared boolean outlineOnly; ] logical operators:!, &&, || (not, and, or) ( x > 0 && x < 7 && outlineOnly ) ( month.startsWith("Ju") || month.equals("May") ) ( ! fileModified || ! (cmd.equals("exit")) ) and hasNext() hasNextInt() hasNextDouble() and equalsIgnoreCase("John”) startsWith(“Ab”) endsWith(“ies”) contains(“th”)

© Peter Andreae COMP :14 Writing Boolean expressions Mostly, boolean expressions are straightforward, There are just a few traps: == is the "equals" operator for simple values, = is assignment age == 15 vs age = 15 ; But only use == for numbers (or characters, or references) Use the equals method for Strings, not == (occasionally == will give the right answer by chance!) cur.equals("US") vs cur == "US" String equality is case sensitive: "US".equals("us") → false "US".equalsIgnoreCase("us")→ true

© Peter Andreae COMP :15 Boolean Variables A boolean value is a value! ⇒ it can be stored in a variable. Useful if the program needs to remember some option. Must declare the variable, and assign to it, before using it boolean printSteps = UI.askBoolean("Print all steps?"); : if ( printSteps ) UI.println("Processed input"); : if ( printSteps ) UI.println("Computed Statistics"); :

© Peter Andreae COMP :16 Writing Boolean expressions When combining with && and ||, which binds tighter? if ( x > 5 && y <= z || day == 0 ) { …. Use ( and ) whenever you are not sure! if ( ( x > 5 && y <= z ) || day == 0 ) { … if ( x > 5 && ( y <= z || day == 0 ) ) { … The not operator ! goes in front of expressions: if ( !(x > 5 && y <= z ) { … if ( ! cur.equals("US") ) { … if ( ! (count == 0) ) { … OR if ( count != 0 ) { …