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

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

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

Solving Recursive Sequences by Iteration

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

CPE 101 slides adapted from UW course. Overview. Chapter UW CSE H1-1. An Old Friend: Fahrenheit to Celsius. Concepts this lecture

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

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

Lab ACN : C++ Programming Exercises

Building Java Programs

Building Java Programs

Introduction to the Java Basics: Control Flow Statements

Loops. CSE 114, Computer Science 1 Stony Brook University

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

EXAM Computer Science 1 Part 1

APCS Semester #1 Final Exam Practice Problems

A Look Back at Arithmetic Operators: the Increment and Decrement

Recap: Assignment as an Operator CS 112 Introduction to Programming

STUDENT LESSON A12 Iterations

A Quick Review of Chapter 1

Control Structures in Java if-else and switch

CS 112 Introduction to Programming

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

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

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

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

Iteration. CSE / ENGR 142 Programming I. while loops. Chapter 5. Motivating Loops. Loop to Add 5 Numbers 1996 UW CSE H - 1

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

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

COMP-202: Foundations of Programming. Lecture 8: for Loops, Nested Loops and Arrays Jackie Cheung, Winter 2016

CS 106 Introduction to Computer Science I

Warmup : Name that tune!

Chapter 7. Iteration. 7.1 Multiple assignment

Perception Gap Who are the financially excluded or underserved across Indonesia?

Chapter 4: Control structures. Repetition

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

Repetition, Looping CS101

An Introduction to Programming with C++ Sixth Edition. Chapter 8 More on the Repetition Structure

Repetition. Chapter 6

Repetition. Chapter 6

Understanding the U.S. Banking System

Chapter 4: Control structures

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

Module 2: Choice and Iteration

CSE 143 Lecture 10. Recursion

ATM Use Cases. ID: CIS Title: Check Balance Description: Customer aims to know the balance in his/her account

More About WHILE Loops

IT 1033: Fundamentals of Programming Loops

An Introduction to Programming with C++ Sixth Edition. Chapter 7 The Repetition Structure

CS112 Lecture: Repetition Statements

Simple Java Programming Constructs 4

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

Chapter Goals. Contents LOOPS

COMP 111. Introduction to Computer Science and Object-Oriented Programming

Control Flow: Loop Statements

Topic 5 for loops and nested loops

Full file at

Scope of this lecture. Repetition For loops While loops

CONDITIONAL EXECUTION

Flow of Control: Loops

Java Coding 3. Over & over again!

CS 106 Introduction to Computer Science I

Object-Oriented Programming

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

Session 14 March 31, 2018

Introduction to Computer Science Unit 3. Programs

Outline of UML and Unified Process. Object Oriented Analysis/Design/Programming UML1.5. Koichiro Ochimizu, JAIST. UML&UP outline 1.

Warm-up for Foundations of Precalculus

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

REPETITION CONTROL STRUCTURE LOGO

Chapter 3. Iteration

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

C-1. Overview. CSE 142 Computer Programming I. Review: Computer Organization. Review: Memory. Declaring Variables. Memory example

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

Welcome! COMP s1. Programming Fundamentals

Chapter 5 : Repetition (pp )

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

Introduction to Software Development (ISD) Week 3

LESSON 3 CONTROL STRUCTURES

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

Activity 6: Loops. Content Learning Objectives. Process Skill Goals

Accelerating Information Technology Innovation

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

Flow of Control: Loops. Objectives. Java Loop Statements: Outline 6/27/2014. Chapter 4

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Chapter 4 Lab. Loops and Files. Objectives. Introduction

CS112 Lecture: Loops

CSE 143. Lecture 9: introduction to recursion reading: 12.1

Decision-Making and Repetition

Short Version of Matlab Manual

Recitation: Loop Jul 7, 2008

Notes - Recursion. A geeky definition of recursion is as follows: Recursion see Recursion.

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Algebraic Reasoning. Participant Materials

Programming for Engineers Iteration

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

Outline. 1 If Statement. 2 While Statement. 3 For Statement. 4 Nesting. 5 Applications. 6 Other Conditional and Loop Constructs 2 / 19

Assignment 2.4: Loops

Lecture 3: Loops. While Loops. While Loops: Newton-Raphson Method. While Loops: Powers of Two

Transcription:

CSE 142 Outline for Today Iteration repeating operations Iteration in Java while statement Shorthand for definite (counting) iterations for statement Nested loops Iteration Introduction to Loops 1/10/2003 (c) 2001-3, University of Washington I-1 1/10/2003 (c) 2001-3, University of Washington I-2 Programming a Teller Machine Suppose you are working on the code for a automated teller machine (ATM). Your code should give out the right number of bills when the user withdraws money. The ATM contains $20 and $5 bills. Problem: Hand out the right number of $20 and $5 bills to make up d dollars. Assume that d is a multiple of $5. Best solution would use as many $20s as possible Design an algorithm for this with your neighbors ATM Algorithm for Dispensing Money Design your solution(s) here 1/10/2003 (c) 2001-3, University of Washington I-3 1/10/2003 (c) 2001-3, University of Washington I-4 CSE142 Wi03 I-1

Additional notes ATM Algorithm Iteration/Repetition The ATM cash algorithm is an example of an iteration or repetition repeatedly perform some operation A few more examples Bake the roast; keep checking the internal temperature until it reaches 220 degrees While there are still donuts in the box, eat one Lather, rinse, repeat Simulations/games science, entertainment Repeatedly update actions of objects in the simulation Video display frames repeatedly Practically all interesting programs contain loops 1/10/2003 (c) 2001-3, University of Washington I-5 1/10/2003 (c) 2001-3, University of Washington I-6 Grow Your Money... On your birthday, you spend $5000 to buy a Certificate of Deposit which earns 3.5% a year on its balance. There is a $10 service fee deducted from the balance each year. How much money will you have at the end of 5 years? Work with your neighbor to design an algorithm to solve the problem. Write down a brief description of your algorithm (without using Java code). Iteration in Java: while Syntax Basic form while statement while ( condition ) { Terminology condition is sometimes called the loop condition is often called the loop body 1/10/2003 (c) 2001-3, University of Washington I-7 1/10/2003 (c) 2001-3, University of Washington I-8 CSE142 Wi03 I-2

Iteration in Java Meaning of while ( condition ) { Repeatedly do the following: Evaluate the condition If the condition is false, the loop terminates execution continues with the statement following the loop body (after ) Execute the and repeat Note: condition is only reevaluated after finishing the complete execution of the loop body not concurrently as loop body statements are executed Flow Chart Another way to visualize loop execution 1/10/2003 (c) 2001-3, University of Washington I-9 1/10/2003 (c) 2001-3, University of Washington I-10 Exercise Write Numbers and Squares Suppose we want to write a table of numbers and their squares for the numbers 1 to 5 Brute force ( + used to combine strings) System.out.println(1 + squared = + 1*1); System.out.println(2 + squared = + 2*2); System.out.println(3 + squared = + 3*3); System.out.println(4 + squared = + 4*4); System.out.println(5 + squared = + 5*5); How could we improve this? What We re Really Trying to Do We really want to repeatedly execute System.out.println(k + squared = + k*k); with k taking on the values 1 through 5 on successive repetitions Solution (?) k = 1; while (k <= 5) { System.out.println(k + squared = + k*k); Does this work? How can we tell? 1/10/2003 (c) 2001-3, University of Washington I-11 1/10/2003 (c) 2001-3, University of Washington I-12 CSE142 Wi03 I-3

Tracing Loops You can desk-check code by hand simulating the steps the computer performs Desk-checking a loop involves simulating each iteration of the loop Check: Exercise In arithmetic, n! (read as n factorial ) is defined to be 1 * 2 * 3 * 4 * * (n-1) * n Exercise: write a loop to compute 7! and check it Hint(?): try writing this out by hand, then figure out what statements can be repeated while some values in them change 1/10/2003 (c) 2001-3, University of Washington I-13 1/10/2003 (c) 2001-3, University of Washington I-14 Loop to Calculate 7! Check: Trace Your code here 1/10/2003 (c) 2001-3, University of Washington I-15 1/10/2003 (c) 2001-3, University of Washington I-16 CSE142 Wi03 I-4

Counting Loops For Statement The loops we ve seen so far all execute a definite number of times with some variable taking on a sequence of values Java, like most other languages, provides a special statement to make this convenient the for statement for (initialization; condition; update) { Counting Loops Example: Print the numbers 1 through 100 With a while loop int k = 1; while (k <= 100) { System.out.println(k); k = k + 1; With a for loop for (int k = 1; k <= 10; k = k + 1) { System.out.println(k); These mean exactly the same thing Style point: Java programmers would normally use for instead of while for a counting loop. 1/10/2003 (c) 2001-3, University of Washington I-17 1/10/2003 (c) 2001-3, University of Washington I-18 For Loops and While Loops A for statement is a convenient shorthand for an equivalent while statement for (initialization; condition; update) { has (for our purposes) exactly the same meaning as initialization; while ( condition ) { update Note that the update executes after the loop body For Statement Flow Chart 1/10/2003 (c) 2001-3, University of Washington I-19 1/10/2003 (c) 2001-3, University of Washington I-20 CSE142 Wi03 I-5

Factorial as a Method A calculation like factorial is a logically coherent operation. It makes sense to package it as a method. Complete the implementation below using a for statement /** Return the value n! */ public int factorial(int n) { Shortcut In loops like for (int k = 1; k <= 10; k = k + 1) { the update k = k + 1 is so common that Java provides a shorthand way to write it: k++ Equivalent loop for (int k = 1; k <= 10; k++) { 1/10/2003 (c) 2001-3, University of Washington I-21 1/10/2003 (c) 2001-3, University of Washington I-22 Double Your Money Problem: Suppose you have invested $1000 at 3% annual interest (meaning that each year, 3% of the present value of the investment is added to it). How many years will it take to double the original investment? Analysis: repeatedly increase the investment value by 3% until it reaches $2000. Count how many times this has to be done. A Non-Counting Iteration In this problem, the operation needs to be repeated until something happens (value >= $2000) We don t know how long this will take This is an indefinite iteration the number of repetitions needed is not known in advance A while loop is appropriate here 1/10/2003 (c) 2001-3, University of Washington I-23 1/10/2003 (c) 2001-3, University of Washington I-24 CSE142 Wi03 I-6

Your Code Here Double Your Money Nested Loops How would you print the following figure? * * * * * * * * * * * * * * * Useful information: System.out.print( * ); will print a single * System.out.println( * ); will print a single *, then move to the beginning of the next output line How would your answer need to be changed if we changed the number of rows or columns? 1/10/2003 (c) 2001-3, University of Washington I-25 1/10/2003 (c) 2001-3, University of Washington I-26 Analysis Solution 1/10/2003 (c) 2001-3, University of Washington I-27 1/10/2003 (c) 2001-3, University of Washington I-28 CSE142 Wi03 I-7

Check Trace the Code Another Problem: A Multiplication Table How would we print the following table? 1 2 3 4 1 1 2 3 4 2 2 4 6 8 3 3 6 9 12 Analysis 1/10/2003 (c) 2001-3, University of Washington I-29 1/10/2003 (c) 2001-3, University of Washington I-30 Your Solution Here Multiplication Table Code Trace your code here Multiplication Table Check 1/10/2003 (c) 2001-3, University of Washington I-31 1/10/2003 (c) 2001-3, University of Washington I-32 CSE142 Wi03 I-8

Summary All interesting programs contain iteration repetition of statements Basic loop while statement Method of choice for indefinite iterations Normal shorthand for definite iterations for statement Nested loops 1/10/2003 (c) 2001-3, University of Washington I-33 CSE142 Wi03 I-9