1 Reasoning about assertions Readings: 5.5. 2 Assertions assertion: A statement that is either true or false. Examples:  Java was created in 1995. (true)

Slides:



Advertisements
Similar presentations
Topic 17 assertions and program logic
Advertisements

Building Java Programs
Copyright 2010 by Pearson Education Building Java Programs Chapter 4 Lecture 4-2: Advanced if/else ; Cumulative sum reading: 4.1, 4.3, 4.5; "Procedural.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Copyright 2008 by Pearson Education 1 Midterm announcements Next week on Friday May 8 Must bring an ID Open book, open notes, closed electronics Must attend.
CS305j Introduction to Computing While Loops 1 Topic 15 Indefinite Loops - While Loops "If you cannot grok [understand] the overall structure of a program.
1 Scanner objects. 2 Interactive programs We have written programs that print console output. It is also possible to read input from the console.  The.
1 BUILDING JAVA PROGRAMS CHAPTER 3 THE SCANNER CLASS AND USER INPUT.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops.
Building Java Programs Chapter 5
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
Lecture 10 Instructor: Craig Duckett Lecture 10 is in Lecture 11 Folder.
CS305j Introduction to Computing Odds and Ends 1 Topic 16 Creating Correct Programs "It is a profoundly erroneous truism, repeated by all the copybooks,
1 Logical Assertions. 2 Logical assertions assertion: A statement that is either true or false. Examples: –Java was created in –The sky is purple.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
CS 112 Introduction to Programming Program Analysis Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
Chapter 4: Control Structures II
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
1 CSE 142 Lecture Notes Conditional Execution with if Statements; Methods that Return Values (briefly) Chapters 3 and 4 Suggested reading: ;
1 CSE 142 Midterm Review Problems These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or modified.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Copyright 2009 by Pearson Education Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
CS 112 Introduction to Programming Program Analysis; Fencepost Loops Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
1 Building Java Programs Chapter 5 Lecture 12: Random, boolean Logic reading: 4.4, 5.1, 5.3, 5.6 (Slides adapted from Stuart Reges, Hélène Martin, and.
CompSci 230 S Programming Techniques
Repetition-Counter control Loop
Building Java Programs Chapter 5
Building Java Programs Chapter 5
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Topic 17 assertions and program logic
Text processing Readings: 4.4.
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Indefinite loop variations
Building Java Programs
Building Java Programs
Building Java Programs
Optional Topic: User Input with Scanner
Presentation transcript:

1 Reasoning about assertions Readings: 5.5

2 Assertions assertion: A statement that is either true or false. Examples:  Java was created in (true)  10 is greater than 20. (false)  Humphrey Bogart never said "Play it again, Sam" in Casablanca. (true)  Marty is 12. (true)  Marty is cooler than me. (false)  x divided by 2 equals 7. (depends on the value of x)

3 Reasoning about assertions Suppose you have the following if (x > 3) { // Point A: do something } else { // Point B: do something else } What do you know at the two different points?  Is x > 3? Always? Sometimes? Never?

4 System.out.print("Type a nonnegative number: "); double number = console.nextDouble(); // Point A: is number < 0.0 here? while (number < 0.0) { // Point B: is number < 0.0 here? System.out.print("Negative; try again: "); number = console.nextDouble(); // Point C: is number < 0.0 here? } // Point D: is number < 0.0 here? Reasoning about assertions System.out.print("Type a nonnegative number: "); double number = console.nextDouble(); // Point A: is number < 0.0 here? (SOMETIMES) while (number < 0.0) { // Point B: is number < 0.0 here? (ALWAYS) System.out.print("Negative; try again: "); number = console.nextDouble(); // Point C: is number < 0.0 here? (SOMETIMES) } // Point D: is number < 0.0 here? (NEVER)

5 Debugging with reasoning: What’s wrong? import java.util.*; public class BuggyLogin { public static void main(String[] args) { Scanner console = new Scanner(System.in); String password = "password"; System.out.print("Enter password: "); String input = console.next(); while (input != password) { System.out.println("Wrong password!"); System.out.print("Enter password: "); input = console.next(); }

6 Strings are objects—should not use != import java.util.*; public class Login { public static void main(String[] args) { Scanner console = new Scanner(System.in); String password = "password"; System.out.print("Enter password: "); String input = console.next(); while (!input.equals(password)) { System.out.println("Wrong password!"); System.out.print("Enter password: "); input = console.next(); }

7 Assertions: Example 1 public static int mystery(Scanner console) { int prev = 0; int count = 0; int next = console.nextInt(); // Point A while (next != 0) { // Point B if (next == prev) { // Point C count++; } prev = next; next = console.nextInt(); // Point D } // Point E return count; } next == 0prev == 0next == prev Point A Point B Point C Point D Point E next == 0prev == 0next == prev Point ASOMETIMESALWAYSSOMETIMES Point BNEVERSOMETIMES Point CNEVER ALWAYS Point DSOMETIMESNEVERSOMETIMES Point EALWAYSSOMETIMES

8 Assertions: Example 2 public static void mystery(int x, int y) { int z = 0; // Point A while (x >= y) { // Point B x -= y; // Point C z++; // Point D } // Point E System.out.println(z + " " + x); } x < yx == yz == 0 Point A Point B Point C Point D Point E x < yx == yz == 0 Point ASOMETIMES ALWAYS Point BNEVERSOMETIMES Point CSOMETIMES Point DSOMETIMES NEVER Point EALWAYSNEVERSOMETIMES

9 Assertions: Example 3 // pre : y >= 0, post: returns x^y public static int pow(int x, int y) { int prod = 1; // Point A while (y > 0) { // Point B if (y % 2 == 0) { // Point C x *= x; y /= 2; // Point D } else { // Point E prod *= x; y--; // Point F } // Point G } // Point H return prod; } y == 0y % 2 == 0 Point A Point B Point C Point D Point E Point F Point G Point H y == 0y % 2 == 0 Point ASOMETIMES Point BNEVERSOMETIMES Point CNEVERALWAYS Point DNEVERSOMETIMES Point ENEVER Point FSOMETIMESALWAYS Point GSOMETIMES Point HALWAYS