Chapter 6 Repetition Asserting Java © Rick Mercer.

Slides:



Advertisements
Similar presentations
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Advertisements

Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
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.
09 Non-deterministic iteration1June Non-deterministic iteration CE : Fundamental Programming Techniques.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
LAB 10.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Building Java Programs Chapter 5 Program Logic and Indefinite Loops Copyright (c) Pearson All rights reserved.
Chapter 6 Repetition Asserting Java © Rick Mercer.
Java Programming: From the Ground Up
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Chapter 5 Loops.
1 Fencepost loops “How do you build a fence?”. 2 The fencepost problem Problem: Write a class named PrintNumbers that reads in an integer called max and.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops; Procedural Design reading: 5.1 – 5.2; 4.5.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Chapter 6A While Loops Asserting Java © Rick Mercer.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 4: Control Structures II
Chapter 5: Control Structures II
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
Building Java Programs Program Logic and Indefinite Loops.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
REPETITION MTS3033 OBJECT ORIENTED PROGRAMMING 1.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
Chapter 5 – Part 3 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/19 Outline The if Statement and Conditions Other Conditional.
Chapter 6 Repetition Asserting Java © Rick Mercer.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 6, Part 1 While Loops Asserting Java © Rick Mercer 1.
Determinate Loop Pattern, Java's for Statement, and Scanner objects
Chapter 5: Control Structures II
Java's for Statement.
Computer Programming Methodology Input and While Loop
Repetition-Sentinel,Flag Loop/Do_While
Chapter 5: Control Structures II
TK1114 Computer Programming
Determinate Loop Pattern, Java's for Statement, and Scanner objects
Something about Java Introduction to Problem Solving and Programming 1.
Building Java Programs
Determinate Loops with the
Chapter 8 Repetition Computing Fundamentals with C++ 3rd Edition
Building Java Programs
Control Statements Loops.
Chapter 6 Repetition Asserting Java © Rick Mercer 1.
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Building Java Programs
Control Statements Loops.
Repetition Statements
Building Java Programs
Building Java Programs
Building Java Programs
Announcements Lab 3 was due today Assignment 2 due next Wednesday
Building Java Programs
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Building Java Programs
Chapter 6, Part 1 While Loops Asserting Java © Rick Mercer 1.
Computer Science Club 1st November 2019.
Presentation transcript:

Chapter 6 Repetition Asserting Java © Rick Mercer

Algorithmic Pattern: The Determinate loop  We often need to perform some action a specific number of times: — Produce 89 paychecks. — Count down to 0 (take 1 second of the clock). — Compute grades for 81 students  The determinate loop pattern repeats some action a specific number of times.

Determinate Loops with while Determinate Loops with while  The determinate loop pattern can be implemented with the Java while loop  This template repeats a process n times: int n = /* how often we must repeat the process */ int n = /* how often we must repeat the process */ int counter = 1; while ( counter <= n ) { int counter = 1; while ( counter <= n ) { // the process to be repeated // the process to be repeated counter = counter + 1; } counter = counter + 1; } — determinate loops must know the number of repetitions before they begin: know exactly how many employees, or students, or whatever that must be processed, for example

Example for loop that produces an average Scanner keyboard = new Scanner(System.in); double sum = 0.0; double number; System.out.print("How many do you want to average? "); int n = keyboard.nextInt(); int counter = 1; // Do something n times while (counter <= n) { System.out.print("Enter number: "); // <- Repeat 3 System.out.print("Enter number: "); // <- Repeat 3 number = keyboard.nextDouble(); // <- statements number = keyboard.nextDouble(); // <- statements sum = sum + number; // <- n times sum = sum + number; // <- n times counter = counter + 1; // make sure the loop stops counter = counter + 1; // make sure the loop stops} double average = sum / n; System.out.print("Average of "+ n + " numbers is "+ average); + " numbers is "+ average);

Active Learning  What is the output : int j = 1; int j = 1; int n = 5; int n = 5; while(j <= n) { while(j <= n) { System.out.print (j + " "); System.out.print (j + " "); j = j + 1; j = j + 1; } j = 0; j = 0; while(j <= 2 * n) { while(j <= 2 * n) { System.out.print (j + " "); System.out.print (j + " "); j = j + 2; j = j + 2; }

Indeterminate Loops  Determinate loops have a limitation — We must know n (the number of repetitions) in advance  Many situations need us to repeat a set of statements an unspecified number of times: — Processing report cards for every student in a school (or paychecks for all employees, or...) — Allowing 1 to many ATM transactions — Asking the user for specific input and allowing re-entry of input after invalid inputs

Some things that terminate indeterminate loops  An indeterminate loop repeats a process until some stopping event terminates the repetition  There are many such events, but we'll focus on these: — User enters a special value indicating end of data — A logical expression becomes false — The Grid's mover hits the wall or an edge — The end of a file is encountered  Indeterminate loops do not need to know n in advance  Indeterminate loops can actually determine n

Pattern Indeterminate loop Problem Some process must repeat an unknown number of times so some event is needed to terminate the loop. Algorithm while( the termination event has not occurred ) { execute these actions bring the loop closer to termination } Code while(myGrid.frontIsClear()) { Example myGrid.move(); }

Example Indeterminate Loop need Grid.java Grid.java // Using random robot placement, instruct robot to get to // the wall in front, turn left, and move to the next wall public class MoveAroundTheGrid { public static void main(String[] args) { Grid g = new Grid(10, 15); while (g.frontIsClear()) { g.move(); } g.turnLeft(); while (g.frontIsClear()) { g.move(); } }

While loop with a Scanner  Sometimes a stream of input from the keyboard or a file needs to be read until there is no more data in the input stream  Consider a Scanner object constructed with a String argument — The string represents an input stream  You will need Scanner in project 2, methods 9 and 10: sumInScanner and maximumInScanner

These assertions public void showScanner() { Scanner scannerWithInts = new Scanner("1 2 3"); assertEquals(1, scannerWithInts.nextInt()); assertEquals(2, scannerWithInts.nextInt()); assertEquals(3, scannerWithInts.nextInt()); Scanner scanner = new Scanner("There are five words here."); assertEquals("There", scanner.next()); assertEquals("are", scanner.next()); assertEquals("five", scanner.next()); assertEquals("words", scanner.next()); assertEquals("here.", scanner.next()); }

A test method to test public void testNum100s() { ControlFun cf = new ControlFun(); Scanner scanner0 = new Scanner("1 2 3"); Scanner scanner1 = new Scanner(" "); Scanner scanner3 = new Scanner(" "); assertEquals(0, cf.num100s(scanner0)); assertEquals(1, cf.num100s(scanner1)); assertEquals(3, cf.num100s(scanner3)); }

Answer public int num100s (Scanner scanner) { int result = 0; while (scanner.hasNextInt()) { int next = scanner.nextInt(); if (next == 100) result++; } return result; }

Careful using next too often!  These assertions should pass with the code that follows on the next public void testSumOfNegs() { ControlFun cf = new ControlFun(); Scanner scanner0 = new Scanner("1 2 3"); Scanner scannerA = new Scanner("1 -2 3"); Scanner scannerB = new Scanner(" "); assertEquals(0, cf.sumOfNegatives(scanner0)); assertEquals(-2, cf.sumOfNegatives(scannerA)); assertEquals(-6, cf.sumOfNegatives(scannerB)); }

What's wrong with this method? public int sumOfNegatives(Scanner scanner) { int result = 0; while (scanner.hasNextInt()) { if (scanner.nextInt() < 0) { result += scanner.nextInt(); } return result; }