Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

Similar documents
Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

CSE115 Introduction to Computer Science I Coding Exercise #7 Retrospective Fall 2017

Outline CSE 142. Specification vs Implementation - Review. CSE142 Wi03 F-1. Instance Variables

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

CSE 1223: Introduction to Computer Programming in Java Chapter 3 Branching

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Assertions. Assertions - Example

CSE373 Fall 2013, Midterm Examination October 18, 2013

EXAM Computer Science 1 Part 1

Assertions, pre/postconditions

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

Comparing procedure specifications

CSE 331. Programming by contract: pre/post conditions; Javadoc

Spring 2003 Instructor: Dr. Shahadat Hossain. Administrative Matters Course Information Introduction to Programming Techniques

Introduction to the Java Basics: Control Flow Statements

More Shared Memory Programming

Program development plan

Problem Grade Total

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Another interface: Comparable

CSE331 Winter 2014, Midterm Examination February 12, 2014

MEHMET YAYAN - İSMAİL HAKKI ÖZTÜRK CS101/SEC.-2 CLASS NOTES 1. March 28-30, 2007

Outline for Today CSE 142. Running Example for Today. Song Data Representation. Quick Review Collection classes Iteration and iterators.

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Binary Search Trees. Chapter 21. Binary Search Trees

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List

System Correctness. EEC 421/521: Software Engineering. System Correctness. The Problem at Hand. A system is correct when it meets its requirements

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

CSE332: Data Abstractions Lecture 25: Deadlocks and Additional Concurrency Issues. Tyler Robison Summer 2010

Use the scantron sheet to enter the answer to questions (pages 1-6)

CSE 20. Lecture 4: Number System and Boolean Function. CSE 20: Lecture2

Specifications. CSE 331 Spring 2010

Binary Search. Roland Backhouse February 5th, 2001

CS111: PROGRAMMING LANGUAGE II

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

CSE 2123: Collections: Priority Queues. Jeremy Morris

Agenda: Notes on Chapter 3. Create a class with constructors and methods.

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

Integrating verification in programming languages

CSE 331 Midterm Exam Sample Solution 2/18/15

Lecture 06: Classes and Objects

Debugging. CSE 2231 Supplement A Annatala Wolf

CIS 265/506 Exam1 Spring 2012 Prof. V. Matos Exam Last Name First Name:

Outline. Data Definitions and Templates Syntax and Semantics Defensive Programming

Example: Monte Carlo Simulation 1

Accelerating Information Technology Innovation

6.170 Lecture 6 Procedure specifications MIT EECS

Decisions in Java IF Statements

Control Structures: The IF statement!

Module 2: Choice and Iteration

Lecture 4 Specifications

CSE 331 Final Exam 3/16/15 Sample Solution

ENGR 2710U Midterm Exam UOIT SOLUTION SHEET

8.3 Common Loop Patterns

public class Account { private int id; private static int nextaccountid = 0; private String name; private double balance;

Testing, Debugging, and Verification

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

CSE wi: Practice Midterm

CSE 142 Wi03 Midterm 2 Sample Solution All Versions Page 1 of 6

CPS 216, Problem Solving and Programming Techniques in C++ Saleh M. Alnaeli, Ph.D. Spring, Lab_Week2

assertion: A statement that is either true or false.

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Expressions & Flow Control

PROGRAMMING FUNDAMENTALS

CSE331 Winter 2014, Midterm Examination February 12, 2014

Type Checking and Type Equality

Programming Abstractions

CS 206 Introduction to Computer Science II

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

COMSC-051 Java Programming Part 1. Part-Time Instructor: Joenil Mistal

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

Collections. Michael Ernst CSE 190p University of Washington

Einführung in die Programmierung Introduction to Programming

CSE 332, Spring 2010, Midterm Examination 30 April 2010

SOFTWARE DEVELOPMENT 1. Control Structures 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

The learning objectives of this chapter are the followings. At the end of this chapter, you shall

Representation Invariants and Abstraction Functions

Object-Oriented Design Lecture 3 CSU 370 Fall 2007 (Pucella) Friday, Sep 14, 2007

CSE332: Data Abstractions Lecture 19: Mutual Exclusion and Locking

1. Consider the following code snippet. Describe what the user will see when it is executed.

CSC 111, Test 1 (Fall 2009)

More About Heaps. 14 June 2017 OSU CSE 1

Computational Expression

Exceptions and assertions

Stack Implementation

1 Conditions and control statements. 1.1 Conditional statement if

Instructor: SIR MUHAMMAD NAVEED Created by: ARSLAN AHMED SHAAD ( ) MUHAMMAD BILAL ( ) ISIT:

Building Java Programs

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

CSE 143 Lecture 10. Recursion

Lecture 7 Objects and Classes

CSE 307: Principles of Programming Languages

21 Abstracting Traversals

Control Structures in Java if-else and switch

Test-Driven Development (TDD)

ECE 122. Engineering Problem Solving with Java

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

Transcription:

CSE 142 Outline for Today Conditional statements if Boolean expressions Comparisons (<, <=, >, >=,!=, ==) Boolean operators (and, or, not - &&,,!) Class invariants Conditional Statements & Boolean Expressions 1/10/2003 (c) 2001-3, University of Washington G-1 1/10/2003 (c) 2001-3, University of Washington G-2 withdraw Method for BankAccount /** Withdraw the requested amount from this BankAccount */ balance = balance - amount; Critique: is this good/bad/incomplete? Class Invariants In many cases, the state of an object must obey some rules to have a sensible value For a BankAccount, some rules might be: that balance >= 0.0 always that the account must have a non-empty name These rules are examples of invariants Things that must always be true, if the program is operating correctly Invariants concerning the state of objects are called class invariants 1/10/2003 (c) 2001-3, University of Washington G-3 1/10/2003 (c) 2001-3, University of Washington G-4 CSE142 Wi03 G-1

More About Invariants Invariants are not syntax rules of Java Advice: Write down invariants as comments When you implement methods, double check that you never violate the invariants Very powerful bug prevention technique Java 1.4 has a special statement to help check invariants assert (to be discussed at another time) A Better withdraw Method Specification /** Withdraw requested amount from this BankAccount provided that the * balance is at least as large as the amount requested. Otherwise do nothing */ Comment in the spec. changes, but not the Java (yet) We want to say (in Java) something like if the amount is less than or equal to the balance, withdraw the amount Java solution: if statement Or conditional statement 1/10/2003 (c) 2001-3, University of Washington G-5 1/10/2003 (c) 2001-3, University of Washington G-6 withdraw Method Implementation /** Withdraw requested amount from this BankAccount provided that the * balance is at least as large as the amount requested. Otherwise do nothing */ if (amount <= balance) { balance = balance amount; If Statement Syntax Syntax or list1 of statements else { list2 of statements condition must be a Boolean expression one that is either true or false may contain any Java statements, including if(!) 1/10/2003 (c) 2001-3, University of Washington G-7 1/10/2003 (c) 2001-3, University of Washington G-8 CSE142 Wi03 G-2

If Statement: Meaning of Each Form Better withdraw Method or list1 of statements else { list2 of statements Meaning of first form Evaluate condition If the condition is true, execute the If it is false, do nothing (skip statements) Meaning of second form Evaluate condition If the condition is true, execute the first and skip the second one If the condition is false, skip the first and execute the second one Instead of silently doing nothing if amount is too large, return a Boolean result to indicate if the withdraw succeeded. Note that this is a change in the specification! /** Withdraw requested amount from this BankAccount and return true, provided * that the balance is at least as large as the amount requested. Otherwise * return false */ public boolean withdraw(double amount) {... 1/10/2003 (c) 2001-3, University of Washington G-9 1/10/2003 (c) 2001-3, University of Washington G-10 Better withdraw Method: Implementation Instead of silently doing nothing if amount is too large, return a Boolean result to indicate if the withdraw succeeded. Note that this is a change in the specification! /** Withdraw requested amount from this BankAccount and return true, provided * that the balance is at least as large as the amount requested. Otherwise * return false */ public boolean withdraw(double amount) { if (amount <= balance) { balance = balance amount; return true; else { return false; Boolean Expressions Boolean constants true false Simple relations on numbers also give boolean values > >= < <=!= == All are binary operators Note use of == for equality comparison (not!!! single =) Examples x > y x*2.5-17.0 <= 0.0 balance >= amount 1/10/2003 (c) 2001-3, University of Washington G-11 1/10/2003 (c) 2001-3, University of Washington G-12 CSE142 Wi03 G-3

Boolean Operators Make complex boolean expressions from simpler boolean expressions && means "and" true if both expressions are true, false otherwise x > 10 && x <= 100 Can only compare two things at a time; can t do 10 < x <= 100 means "or" true if either expression is true, false only if both are false x > y x <= 0! means "not" true if expression is false! (x < y) // means same thing as x >= y Practice With Boolean Expressions Suppose x is 10 and y is two. What is the value of each expression? x < 9 x == y 8 x >= 0 y == 0 x!= 3 y!= x && x > y!(x < y) 1/10/2003 (c) 2001-3, University of Washington G-13 1/10/2003 (c) 2001-3, University of Washington G-14 Exercise Recall that the statement System.out.println( Hi there! ); will write a message (in this case, Hi there! ) Exercise 1: assume that we have a double variable called temperature holding the outside temperature. Write the message Too Hot! if the temperature is above 80. Exercise 2: use the variable temperature as above, but this time write Too Hot! if the temperature is above 80, Too Cold! if it is below 60, and Just Right if it is in between. Solution to Exercise 1 1/10/2003 (c) 2001-3, University of Washington G-15 1/10/2003 (c) 2001-3, University of Washington G-16 CSE142 Wi03 G-4

Solution to Exercise 2 Summary Invariants Conditional execution if statement Boolean expressions Comparisons Operators and, or, not 1/10/2003 (c) 2001-3, University of Washington G-17 1/10/2003 (c) 2001-3, University of Washington G-18 CSE142 Wi03 G-5