Type boolean. Building Java Programs. Recap: Type boolean. "Short-circuit" evaluation. De Morgan's Law. Boolean practice questions.

Similar documents
Building Java Programs

Building Java Programs

Building Java Programs Chapter 5

Topic 16 boolean logic

Building Java Programs

Building Java Programs

assertion: A statement that is either true or false.

! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

CSc 110, Autumn Lecture 13: Cumulative Sum and Boolean Logic. Adapted from slides by Marty Stepp and Stuart Reges

CS 112 Introduction to Programming

Text processing. Characters. The charat method. Fun with char! char vs. String. Text processing. Readings: 4.4 (pg ) 'h' is a char

Text processing. Readings: 4.4

Lecture 8: The String Class and Boolean Zen

CSc 110, Spring Lecture 13: Random numbers and Boolean Logic. Adapted from slides by Marty Stepp and Stuart Reges

The keyword list thus far: The Random class. Generating "Random" Numbers. Topic 16

Topic 16. battle -they are strictly limited in number, they require fresh horses, and must only be made at decisive moments." -Alfred North Whitehead

Building Java Programs

Building Java Programs

-Alfred North Whitehead. Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from

1. An operation in which an overall value is computed incrementally, often using a loop.

CS 112 Introduction to Programming. Exercise: MatchDB. match1. Removing break. Solution I: Using break. Loop Patterns: break; Fencepost.

Building Java Programs

CS 112 Introduction to Programming

Flow of Control of Program Statements CS 112 Introduction to Programming. Basic if Conditional Statement Basic Test: Relational Operators

CSc 110, Autumn Lecture 26: Assertions. Adapted from slides by Marty Stepp and Stuart Reges

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

Garfield AP CS. User Input, If/Else. Most slides from Building Java Programs. Thanks, Stuart Regesand Marty Stepp!

Building Java Programs

CSc 110, Spring Lecture 14: Booleans and Strings. Adapted from slides by Marty Stepp and Stuart Reges

Full file at

Building Java Programs

CSc 110, Autumn Lecture 14: Strings. Adapted from slides by Marty Stepp and Stuart Reges

Building Java Programs

Java Coding 3. Over & over again!

Topic 11 Scanner object, conditional execution

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

Topic 14 while loops and loop patterns

Midterm Examination (MTA)

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Midterm Review Session

Repetition Structures

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

CONDITIONAL EXECUTION

Chapter 3. Selections

Topics. Chapter 5. Equality Operators

Loops. CSE 114, Computer Science 1 Stony Brook University

2.2 - Making Decisions

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

CIS 110: Introduction to Computer Programming

Chapter 4: Control Structures I

CSE142 Sample Midterm Spring Name UW NetId (e.g. whitab) Section (e.g., AA) TA

How would you handle the case for when a user selects an option? 1/15/2016 CSE 11 WINTER LEC 6 2

Java. Programming: Chapter Objectives. Why Is Repetition Needed? Chapter 5: Control Structures II. Program Design Including Data Structures

CS 112 Introduction to Programming

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Conditional Execution

Building Java Programs

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

Control Flow Practice Problems Mr. Fahrenbacher AP Computer Science A

Topic 11 Scanner object, conditional execution

Repetition, Looping. While Loop

CSE 142 Sample Midterm Exam #3

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

AP Computer Science. Return values, Math, and double. Copyright 2010 by Pearson Education

AP Computer Science. if/else, return values. Copyright 2010 by Pearson Education

CS 211: Methods, Memory, Equality

Building Java Programs A Back to Basics Approach 4th Edition Reges TEST BANK Full download at:

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Flow of Control. Chapter 3

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Some Sample AP Computer Science A Questions - Solutions

Building Java Programs

Final Examination Semester 2 / Year 2011

CSC 1051 Data Structures and Algorithms I

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal


New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Loops. GEEN163 Introduction to Computer Programming

Building Java Programs Chapter 4

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Repetition. Chapter 6

COSC 123 Computer Creativity. Java Decisions and Loops. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Repetition. Chapter 6

CSE 142 Sample Midterm Exam #2

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

Lab1 Solution. Lab2 Solution. MathTrick.java. CoinFlip.java

Honors Computer Programming 1-2. Chapter 5 Exam Prep

Computational Expression

Flow of Control. Chapter 3

In this chapter, you will:

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to write programs for executing statements repeatedly using a while, do while and for loop

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

CSE142 Sample Midterm, Winter 2018

Object Oriented Programming. Java-Lecture 6 - Arrays

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

Building Java Programs

Oct Decision Structures cont d

Transcription:

Building Java Programs Chapter 5 Lecture 5-4: More boolean, Assertions, do/while loops Type boolean reading: 5.3 reading: 5.3, 5.4, 5.1 1 Recap: Type boolean boolean: A logical type whose values are true and false. A test in an if, for, or while is a boolean expression. boolean minor = (age < 21); if (minor) { System.out.println("Can't purchase alcohol!"); boolean expressions can be combined using logical operators: Operator Description Example Result && and (2 == 3) && (-1 < 5) false or (2 == 3) (-1 < 5) true! not!(2 == 3) true 3 "Short-circuit" evaluation Java stops evaluating a test if it knows the answer. && stops early if any part of the test is false stops early if any part of the test is true The following test will crash if s2's length is less than 2: // Returns true if s1 and s2 end with the same two letters. public static boolean rhyme(string s1, String s2) { return s1.endswith(s2.substring(s2.length() - 2)) && s1.length() >= 2 && s2.length() >= 2; The following test will not crash; it stops if length < 2: // Returns true if s1 and s2 end with the same two letters. public static boolean rhyme(string s1, String s2) { return s1.length() >= 2 && s2.length() >= 2 && s1.endswith(s2.substring(s2.length() - 2)); 4 De Morgan's Law De Morgan's Law: Rules used to negate boolean tests. Useful when you want the opposite of an existing test. Original Expression Negated Expression Alternative a && b!a!b!(a && b) a b!a &&!b!(a b) Boolean practice questions Write a method named isvowel that returns whether a String is a vowel (a, e, i, o, or u), case-insensitively. isvowel("q") returns false isvowel("a") returns true isvowel("e") returns true Example: Original Code if (x == 7 && y > 3) { Negated Code if (x!= 7 y <= 3) { Change the above method into an isnonvowel that returns whether a String is any character except a vowel. isnonvowel("q") returns true isnonvowel("a") returns false isnonvowel("e") returns false 5 6 bye 1

Boolean practice answers // Enlightened version. I have seen the true way (and false way) public static boolean isvowel(string s) { return s.equalsignorecase("a") s.equalsignorecase("e") s.equalsignorecase("i") s.equalsignorecase("o") s.equalsignorecase("u"); // Enlightened "Boolean Zen" version public static boolean isnonvowel(string s) { return!s.equalsignorecase("a") &&!s.equalsignorecase("e") &&!s.equalsignorecase("i") &&!s.equalsignorecase("o") &&!s.equalsignorecase("u"); // or, return!isvowel(s); When to return? Methods with loops and return values can be tricky. When and where should the method return its result? Write a method seven that accepts a Random parameter and uses it to draw up to ten lotto numbers from 1-30. If any of the numbers is a lucky 7, the method should stop and return true. If none of the ten are 7 it should return false. The method should print each number as it is drawn. 15 29 18 29 11 3 30 17 19 22 (first call) 29 5 29 4 7 (second call) 7 8 Flawed solution Returning at the right time // Draws 10 lotto numbers; returns true if one is 7. public static boolean seven(random rand) { for (int i = 1; i <= 10; i++) { int num = rand.nextint(30) + 1; System.out.print(num + " "); if (num == 7) { return true; else { return false; // Draws 10 lotto numbers; returns true if one is 7. public static boolean seven(random rand) { for (int i = 1; i <= 10; i++) { int num = rand.nextint(30) + 1; System.out.print(num + " "); if (num == 7) { return true; // found lucky 7; can exit now return false; // if we get here, there was no 7 The method always returns immediately after the first roll. This is wrong if that roll isn't a 7; we need to keep rolling. Returns true immediately if 7 is found. If 7 isn't found, the loop continues drawing lotto numbers. If all ten aren't 7, the loop ends and we return false. 9 10 Random/while question Write a multiplication tutor program. Use a static method that returns a boolean value. Test multiplication of numbers between 1 and 20. The program stops after too many incorrect answers. Mistakes allowed: 0 Assertions 14 * 8 = 112 5 * 12 = 60 reading: 5.5 8 * 3 = 24 5 * 5 = 25 20 * 14 = 280 19 * 14 = 256 Incorrect; the answer was 266 You solved 5 correctly. 11 bye 2

Logical assertions assertion: A statement that is either true or false. Examples: Java was created in 1995. The sky is purple. 23 is a prime number. 10 is greater than 20. x divided by 2 equals 7. (depends on the value of x) An assertion might be false ("The sky is purple" above), but it is still an assertion because it is a true/false statement. Reasoning about assertions Suppose you have the following code: if (x > 3) { x--; else { x++; What do you know about x's value at the three points? Is x > 3? Always? Sometimes? Never? 13 14 Assertions in code We can make assertions about our code and ask whether they are true at various points in the code. Valid answers are ALWAYS, NEVER, or SOMETIMES. System.out.print("Type a nonnegative number: "); double number = console.nextdouble(); : is number < 0.0 here? (SOMETIMES) while (number < 0.0) { : is number < 0.0 here? (ALWAYS) System.out.print("Negative; try again: "); number = console.nextdouble(); : is number < 0.0 here? : is number < 0.0 here? (SOMETIMES) (NEVER) Reasoning about assertions Right after a variable is initialized, its value is known: int x = 3; // is x > 0? ALWAYS In general you know nothing about parameters' values: public static void mystery(int a, int b) { // is a == 10? SOMETIMES But inside an if, while, etc., you may know something: public static void mystery(int a, int b) { if (a < 0) { // is a == 10? NEVER 15 16 Assertions and loops At the start of a loop's body, the loop's test must be true: // is y < 10? ALWAYS After a loop, the loop's test must be false: // is y < 10? NEVER Inside a loop's body, the loop's test may become false: y++; // is y < 10? SOMETIMES 17 "Sometimes" Things that cause a variable's value to be unknown (often leads to "sometimes" answers): reading from a Scanner reading a number from a Random object a parameter's initial value to a method If you can reach a part of the program both with the answer being "yes" and the answer being "no", then the correct answer is "sometimes". If you're unsure, "Sometimes" is a good guess. 18 bye 3

Assertion example 1 public static void mystery(int x, int y) { int z = 0; while (x >= y) { x = x - y; z++; if (x!= y) { z = z * 2; System.out.println(z); Point B Point C Point D Point E x < y x == y z == 0 SOMETIMES SOMETIMES ALWAYS NEVER SOMETIMES SOMETIMES SOMETIMES NEVER NEVER SOMETIMES SOMETIMES NEVER ALWAYS NEVER SOMETIMES 19 Assertion example 2 public static int mystery(scanner console) { int prev = 0; int count = 0; int next = console.nextint(); while (next!= 0) { if (next == prev) { count++; prev = next; next = console.nextint(); return count; Point B Point C Point D Point E next == 0 prev == 0 next == prev SOMETIMES ALWAYS SOMETIMES NEVER SOMETIMES SOMETIMES NEVER NEVER ALWAYS SOMETIMES NEVER SOMETIMES ALWAYS SOMETIMES SOMETIMES 20 Assertion example 3 // Assumes y >= 0, and returns x^y public static int pow(int x, int y) { int prod = 1; while (y > 0) { if (y % 2 == 0) { x = x * x; y > 0 y % 2 == 0 y = y / 2; SOMETIMES SOMETIMES else { Point B ALWAYS SOMETIMES prod = prod * x; Point C ALWAYS ALWAYS y--; // Point F Point D ALWAYS SOMETIMES Point E ALWAYS NEVER // Point G return prod; Point F SOMETIMES ALWAYS Point G NEVER ALWAYS while loop variations reading: 5.1, Appendix D 21 22 The do/while loop do/while question do/while loop: Performs its test at the end of each repetition. Guarantees that the loop's { body will run at least once. while (test); Modify the previous Dice program to use do/while. 2 + 4 = 6 3 + 5 = 8 5 + 6 = 11 1 + 1 = 2 4 + 3 = 7 You won after 5 tries! // Example: prompt until correct password is typed String phrase; System.out.print("Type your password: "); phrase = console.next(); while (!phrase.equals("abracadabra")); Is do/while a good fit for our past Sentinel program? 23 24 bye 4

do/while answer break // Rolls two dice until a sum of 7 is reached. import java.util.*; public class Dice { public static void main(string[] args) { Random rand = new Random(); int tries = 0; int sum; int roll1 = rand.nextint(6) + 1; // one roll int roll2 = rand.nextint(6) + 1; sum = roll1 + roll2; System.out.println(roll1 + " + " + roll2 + " = " + sum); tries++; while (sum!= 7); System.out.println("You won after " + tries + " tries!"); break statement: Immediately exits a loop. Can be used to write a loop whose test is in the middle. The loop's test is often changed to true ("always repeat"). while (true) { if (test) { break; break is considered to be bad style by some programmers. 25 26 Sentinel loop with break Scanner console = new Scanner(System.in); int sum = 0; while (true) { System.out.print("Enter a number (-1 to quit): "); int number = console.nextint(); if (number == -1) { // don't add -1 to sum break; sum = sum + number; // number!= -1 here System.out.println("The total was " + sum); 27 bye 5