16
Determinate Loop Pattern, Java's for Statement, and Scanner objects 2 nd part of Chapter 6: Repetition

Determinate Loop Pattern, Java's Statement, and Scanner

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Determinate Loop Pattern, Java's Statement, and Scanner

Determinate Loop Pattern, Java's for Statement, and Scanner objects 2nd part of Chapter 6: Repetition

Page 2: Determinate Loop Pattern, Java's Statement, and Scanner

Algorithmic Pattern: The Determinate loop

w 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). —  Simulate playing a game of "Let's Make a Deal"

10,000 times w The determinate loop pattern repeats some action

a specific number of times

Page 3: Determinate Loop Pattern, Java's Statement, and Scanner

Pattern:   Determinate Loop P roblem:   Do something exactly n times, where n is known in

advance. Algorithm   determine n

repeat the following n times { perform these actions }

C ode  E xample:  

int sum = 0; int n = 4; for(int c = 1; c <= n; c = c + 1) { sum = sum + c; } // What is sum now?

Page 4: Determinate Loop Pattern, Java's Statement, and Scanner

Determinate Loops

w This template repeats a process n times —  replace comments with appropriate statements

int n = /* how often we must repeat the process */ for( int j = 1; j <= n; j = j + 1 ) { // the process to be repeated }

w 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

Page 5: Determinate Loop Pattern, Java's Statement, and Scanner

General Form The Java for loop

for ( initial statement ; loop-test ; update-step) { repeated-part }

—  When a for loop is encountered, the initial-statement is executed (used here quite often as int j = 1). The loop-test evaluates. If loop-test is false, the for loop terminates. If loop-test is true, the repeated-part executes followed by the update-step.

Page 6: Determinate Loop Pattern, Java's Statement, and Scanner

Flow chart view of a for loop

Initial statement

False Loop test

Iterative part

update-step

True

Page 7: Determinate Loop Pattern, Java's Statement, and Scanner

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

Page 8: Determinate Loop Pattern, Java's Statement, and Scanner

Code Demo: Use the debugger to trace this code

int n = 5;

for (int j = 1; j <= n; j = j + 1) { System.out.println(j); } for (int k = 10; k >= 0; k = k - 2) { System.out.println(k); }

Page 9: Determinate Loop Pattern, Java's Statement, and Scanner

Other Incrementing Operators w It is common to see determinate loops of this form

where n is the number of repetitions for( int j = 1; j <= n; j++ ) } // ... }

w The unary ++ and -- operators add 1 and subtract 1 from their operands, respectively.

int n = 0; n++; // n is now 1 equivalent to n=n+1; or n+=1; n++; // n is now 2 n--; // n is now 1 again

w The expression count++ is equivalent to the more verbose count = count + 1;

Page 10: Determinate Loop Pattern, Java's Statement, and Scanner

Other Assignment Operators w Java has several assignment operators in

addition to = (-= and +=) j -= 2; is the equivalent of j = j - 2; sum += x; is the equivalent of sum = sum + x;

w What is sum when a user enters 7 and 8? int sum = 0; int x = 0; System.out.print("Enter a number: "); x = keyboard.nextInt(); // user enters 7 sum += x; System.out.print("Enter a number: "); x = keyboard.nextInt(); // user enters 8 sum += x;

Page 11: Determinate Loop Pattern, Java's Statement, and Scanner

Indeterminate loop with a Scanner

w 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

w Consider a Scanner object constructed with a String argument —  The string represents an input stream

w You will need Scanner in project 2, methods 9 and 10: sumInScanner and maximumInScanner

Page 12: Determinate Loop Pattern, Java's Statement, and Scanner

These assertions pass

@Test 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()); }

Page 13: Determinate Loop Pattern, Java's Statement, and Scanner

A test method to test num100s

@Test public void testNum100s() { ControlFun cf = new ControlFun(); Scanner scanner0 = new Scanner("1 2 3"); Scanner scanner1 = new Scanner("4 100 2 5"); Scanner scanner3 = new Scanner("100 100 2 -3 5 3 2 -100 100"); assertEquals(0, cf.num100s(scanner0)); assertEquals(1, cf.num100s(scanner1)); assertEquals(3, cf.num100s(scanner3)); }

Page 14: Determinate Loop Pattern, Java's Statement, and Scanner

Answer

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

Page 15: Determinate Loop Pattern, Java's Statement, and Scanner

Careful using next too often!

w These assertions should pass with the code that follows on the next slide

@Test 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("-4 1 -2 3"); assertEquals(0, cf.sumOfNegatives(scanner0)); assertEquals(-2, cf.sumOfNegatives(scannerA)); assertEquals(-6, cf.sumOfNegatives(scannerB)); }

Page 16: Determinate Loop Pattern, Java's Statement, and Scanner

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; }