Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems."— Presentation transcript:

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

2 Fundamentals of Software Development 1Slide 2 Programming patterns (non-looping) temp = x; x = y; y = temp; if (y < 0) { x = -y; } else { x = y; } if (x > y) { z = x; } else { z = y; } swap absolute 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

3 Fundamentals of Software Development 1Slide 3 Loop pattern: “do N 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 1 st, 2 nd and 3 rd roots of 8 through 11, inclusiveProblem 2: Display the 1 st, 2 nd and 3 rd roots of 8 through 11, inclusive 8 2.8284271247461903 1.998614185980905 9 3.0 2.078560910383862 10 3.1622776601683795 2.152781734724373 11 3.3166247903554 2.222203177024025 Note the “cast” of k to type double

4 Fundamentals of Software Development 1Slide 4 Loop pattern: “do N 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 1 st, 2 nd and 3 rd roots of start through stop, inclusive, where start and stop are determined at run time by the userProblem 3: Display the 1 st, 2 nd and 3 rd roots of start through stop, inclusive, where start and stop 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

5 Fundamentals of Software Development 1Slide 5 Loop pattern: “count” Problem: How many integers from -1000 to 1000, inclusive, have cosines that are negative? Display the count.Problem: How many integers from -1000 to 1000, inclusive, have cosines that are negative? Display 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.

6 Fundamentals of Software Development 1Slide 6 Loop pattern: “sum” Problem: Sum the cosines of the integers from -20 to 2000. Display the sum.Problem: Sum the cosines of the integers from -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

7 Fundamentals of Software Development 1Slide 7 Loop pattern: “loop through a list” Problem: Write a method that returns how many times a given character appears in a given StringProblem: Write a method that returns how many times 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 + ””)

8 Fundamentals of Software Development 1Slide 8 Loop followed by a loop Loop inside a loop for (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

9 Fundamentals of Software Development 1Slide 9 Some looping programming patterns 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)


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

Similar presentations


Ads by Google