Topic 17 assertions and program logic

Slides:



Advertisements
Similar presentations
Review pages for the AP CS Exam
Advertisements

Escape Sequences \n newline \t tab \b backspace \r carriage return
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.
Chapter 4 - Control Statements
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Q1 Review. Other News US News top CS Universities global-universities/computer- science?int=994b08.
Building Java Programs
Loops (Part 1) Computer Science Erwin High School Fall 2014.
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 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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Topic 11 Scanner object, conditional execution Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
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.
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
Chapter 5 Loops.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CS101 Computer Programming I Chapter 4 Extra Examples.
Topic 14 while loops and loop patterns Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Chapter 5: Control Structures II
1 Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1, 5.6.
1 Reasoning about assertions Readings: Assertions assertion: A statement that is either true or false. Examples:  Java was created in (true)
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
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.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Copyright 2009 by Pearson Education Building Java Programs Chapter 5 Lecture 5-3: Boolean Logic reading: 5.2 self-check: # exercises: #12 videos:
Topic 21 arrays - part 1 Copyright Pearson Education, 2010 Based on slides by Marty Stepp and Stuart Reges from "Should.
Midterm Review Problems
Topic 11 Scanner object, conditional execution
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Preconditions, Postconditions & Assertions
Topic 14 while loops and loop patterns
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
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Module 4 Loops and Repetition 2/1/2019 CSE 1321 Module 4.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Indefinite loop variations
Building Java Programs
CSS161: Fundamentals of Computing
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Topic 17 assertions and program logic "As soon as we started programming, we found out to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs." Maurice V Wilkes Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from http://www.buildingjavaprograms.com/

Clicker question What is output by the following method? public static void mysteryB(boolean b) { System.out.print(b + " "); b = (b == false); System.out.print(b); } no output due to syntax error no output due to runtime error not possible to predict output will always output true false OR false true Will always output true true or false false

Assertions Assertion: A declarative sentence that is either true or false Examples: 2 + 2 equals 4 The Yankees did not play in the world series in 2006. x > 45 It is raining. UT will beat OU next year. UT volleyball will qualify for the NCAA tourney. Not assertions How old are you? Take me to H.E.B.

Assertions Some assertions are true or false depending on context. Which of these depend on the context? 2 + 2 equals 4 The Yankees did not play in the world series in 2006. x > 45 It is raining. UT will beat OU next year. UT volleyball will qualify for the NCAA tourney.

Assertions Assertions that depend on context can be evaluated if the context is provided. when x is 13, x > 45 It was raining in Round Rock, at 8 am on, October 10, 2006. Many skills required to be a programmer or computer scientists Just a few we have seen so far ability to generalize create structured solutions trace code manage lots of details

Assertions Another important skill in programming and computer science is the ability "to make assertions about your programs and to understand the contexts in which those assertions will be true." Scanner console = new Scanner(System.in); System.out.print("Enter Y or N: "); String result = console.nextLine(); // is result equal to "Y" or "N" here?

Checking Input Scanner console = new Scanner(System.in); System.out.print("Enter Y or N: "); String result = console.nextLine(); while(!result.equals("Y") && !result.equals("N")){ System.out.print("That wasn't a Y or N. "); result = console.nextLine(); } // is result equal to "Y" or "N" here?

Assertions Provable Assertion: An assertion that can be proven to be true at a particular point in program execution. Program Verification: A field of computer science that involves reasoning about the formal properties of programs to prove the correctness of a program. Instead of testing. A number of UTCS faculty are involved in verification research: Emerson, Hunt, Lam, Moore, Young

Reasoning about assertions Suppose you have the following code: if (x > 3) { // Point A x--; } else { // Point B x++; // Point C } // Point D What do you know about x's value at the three points? Is x > 3? Always? Sometimes? Never?

Assertions in code System.out.print("Type a nonnegative number: "); 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(); // 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? (SOMETIMES) (ALWAYS) (NEVER)

Reasoning about programs 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

Reasoning about programs 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 ... }

Assertions and loops At the start of a loop's body, the loop's test must be true: while (y < 10) { // 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

"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 initial value of a parameter in 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”.

Assertion example 1 public static void mystery(int x, int y) { int z = 0; // Point A while (x >= y) { // Point B x = x - y; z++; if (x != y) { // Point C z = z * 2; } // Point D // Point E System.out.println(z); Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. SOMETIMES ALWAYS NEVER x < y x == y z == 0 Point A Point B Point C Point D Point E

Assertion example 2 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; Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. SOMETIMES ALWAYS NEVER next == 0 prev == 0 next == prev Point A Point B Point C Point D Point E Observation: You can base your knowledge of variable B on variable A if B's value is related to A's. In this slide, we know things about next, so we also know things about prev at certain points.

Assertion example 3 // Assumes y >= 0, and 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 * x; y = y / 2; // Point D } else { // Point E prod = prod * x; y--; // Point F } // Point G return prod; Which of the following assertions are true at which point(s) in the code? Choose ALWAYS, NEVER, or SOMETIMES. y > 0 y % 2 == 0 Point A SOMETIMES Point B ALWAYS Point C Point D Point E NEVER Point F Point G y > 0 y % 2 == 0 Point A Point B Point C Point D Point E Point F Point G