9
Fundamentals of Softw are Development 1 Slide 1 Programming Patterns Programming Patterns Many problems fit a Many problems fit a “pattern” that “pattern” that experienced experienced software developers software developers recognize recognize And hence can easily And hence can easily code, from code, from experience experience Previously, we saw Previously, we saw these patterns: these patterns: Swap Swap Absolute value Absolute value Maximum of two Maximum of two Select from cases Select from cases Increment Increment Today, we will see Today, we will see these looping these looping patterns: patterns: Do N times Do N times Count Count Sum Sum Loop through a list Loop through a list Loop followed by another Loop followed by another loop loop Loop inside a loop Loop inside a loop Later, we will see Later, we will see other looping other looping patterns, including: patterns, including: Max/min Max/min Find-first in a list Find-first in a list Do forever Do forever Break in the middle of a Break in the middle of a loop loop Continue to the next Continue to the next iteration of the loop iteration of the loop

Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Embed Size (px)

Citation preview

Page 1: Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Fundamentals of Software Development 1

Slide 1

Programming PatternsProgramming Patterns

• Many problems fit a Many problems fit a “pattern” that “pattern” that experienced software experienced software developers recognizedevelopers recognize– And hence can easily And hence can easily

code, from experiencecode, from experience• Previously, we saw Previously, we saw

these patterns:these patterns:– SwapSwap– Absolute valueAbsolute value– Maximum of twoMaximum of two– Select from casesSelect from cases– IncrementIncrement

• Today, we will see these Today, we will see these looping patterns:looping patterns:– Do N timesDo N times– CountCount– SumSum– Loop through a listLoop through a list– Loop followed by Loop followed by

another loopanother loop– Loop inside a loopLoop inside a loop

• Later, we will see other Later, we will see other looping patterns, looping patterns, including:including:– Max/minMax/min– Find-first in a listFind-first in a list– Do foreverDo forever– Break in the middle of a Break in the middle of a

looploop– Continue to the next Continue to the next

iteration of the loopiteration of the loop

Page 2: Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Fundamentals of Software Development 1

Slide 2

Programming patterns Programming patterns (non-looping)(non-looping)

temp = x;x = y;y = temp;

if (y < 0) { x = -y;} else { x = y;}

if (x > y) { z = x;} else { z = y;}

swapabsolute value maximum of two

if (x >= 90) { z = ‘A’;} else if (x >= 80) { z = ‘B’;} else if (x >= 70) { z = ‘C’;} else if (x >= 60) { z = ‘D’;} else { z = ‘F’;}

x = x + 1;

Increment(for counting)

Select from cases

Keep these patterns in mind, to help you with similar problems that

you encounter

Page 3: Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Fundamentals of Software Development 1

Slide 3

Loop pattern: “do N Loop pattern: “do N times”times”

for (int k = 8; k <= 11; k++) { System.out.print(k + ″:\t″; System.out.print(Math.pow((double) k, 0.5) + ″\t″); System.out.print(Math.pow((double) k, 0.333) + ″\t″); System.out.print(Math.pow((double) k, 0.25); System.out.println();}

• Problem 1: Display the sine of 0.01, sine of 0.02, … sine of 4.00Problem 1: Display the sine of 0.01, sine of 0.02, … sine of 4.00

for (int k = 1; k <= 400; ++k) {

System.out.println(Math.sin(k/100.0));

}

• Problem 2: Display the Problem 2: Display the 11stst, 2, 2ndnd and 3 and 3rdrd roots of roots of8 through 11, inclusive8 through 11, inclusive

8 2.8284271247461903 1.9986141859809059 3.0 2.07856091038386210 3.1622776601683795 2.15278173472437311 3.3166247903554 2.222203177024025

Note the “cast” of k to type double

Page 4: Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Fundamentals of Software Development 1

Slide 4

Loop pattern: “do N Loop pattern: “do N times”times”

Scanner inputStream = new Scanner(System.in);int start, stop;

System.out.print(″Start at? ″);start = inputStream.nextInt();

System.out.print(″Stop at? ″);stop = inputStream.nextInt();

for (int k = start; k <= stop; k++) { Same code as in the loop in the previous example};

• Problem 3: Display the 1Problem 3: Display the 1stst, 2, 2ndnd and 3 and 3rdrd roots of roots of startstart through through stopstop, , inclusive, where inclusive, where startstart and and stopstop are determined at run time by the user are determined at run time by the user

This example also shows how to do console input

Note that start and stop are both variables here

Page 5: Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Fundamentals of Software Development 1

Slide 5

Loop pattern: “count”Loop pattern: “count”• Problem: How many integers from -1000 to 1000, Problem: How many integers from -1000 to 1000,

inclusive, have cosines that are negative? Display inclusive, have cosines that are negative? Display the count. the count.

int count;

count = 0;for (int k = -1000; k <= 1000; ++k) { if (Math.cos((double) k) < 0 { ++ count; }}

System.out.println(count);}

Start count at 0

Increment count whenever it is time to “count”

Exercise: Sum the cosines of the integers from -20 to 2000. Display the sum.

Answer on next slide.

Page 6: Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Fundamentals of Software Development 1

Slide 6

Loop pattern: “sum”Loop pattern: “sum”• Problem: Sum the cosines of the integers from -Problem: Sum the cosines of the integers from -

20 to 2000. Display the sum.20 to 2000. Display the sum.

double sum; sum = 0.0;for (int k = -20; k <= 2000; ++k) { sum = sum + Math.cos((double) k);} System.out.println(count);

Start sum at 0.0

Increment sum whenever it is time to “sum”, by whatever amount should be summed

sum has type double here, because the sum is of double’s

Page 7: Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Fundamentals of Software Development 1

Slide 7

Loop pattern: “loop Loop pattern: “loop through a list”through a list”

• Problem: Write a method that returns how many Problem: Write a method that returns how many times a given character appears in a given Stringtimes a given character appears in a given String

public static int charCount(char c, String s) { int count;

count = 0; for (int k = 0; k < s.length(); ++k) { if (s.charAt(k) == c) { ++ count; } }

return count;}

A static method is one that does not use any of the fields of the class

This example includes the counting pattern

FYI: Use == only to compare primitives. To compare objects (like Strings), use their equals method, e.g. s.substring(k, k+1).equals(c + ””)

Page 8: Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Fundamentals of Software Development 1

Slide 8

Loop followed by a Loop followed by a looploopLoop inside a loopLoop inside a loopfor (int k = 0; k < 5; ++k) {

System.out.print(k);}

for (int k = 0; k < 5; ++k) { System.out.print(k);}

for (int k = 0; k < 5; ++k) { for (int m = 0; m < 3; ++m) { System.out.print(k); }}

Same as previous box, except print m (not k)

Exercise: What output is produced by the first box to the left? By the 2nd box? The 3rd box?

0123401234

000111222333444

012012012012012

Page 9: Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems

Fundamentals of Software Development 1

Slide 9

Some Some loopinglooping programming programming patternspatterns

for (int k = start; k <= stop; ++k) { do stuff}

Do N times

Keep these patterns in mind, to help you with similar

problems that you encounter

int count;

count = 0;for (some loop) { if (some condition to count) { ++ count; }}

count

Similar to count, but: sum = sum + thing-to-sum;

sum

for (int k = 0; k < s.length; ++k) { do stuff with s.charAt(k)}

Loop through a list (here, a String s)