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

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

Fundamentals of Software Development 1Slide 1 Programming Patterns: Motivation 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

Fundamentals of Software Development 1Slide 2 Programming Patterns 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 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: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

Fundamentals of Software Development 1Slide 3 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

Fundamentals of Software Development 1Slide 4 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, 1.0/3.0) + ″ \t ″ ); System.out.print(Math.pow((double) k, 0.25); System.out.println(); } Problem 1: Display the square of 0.01, sine of 0.02, … 4.00Problem 1: Display the square of 0.01, sine of 0.02, … 4.00 for (int k = 1; k <= 400; ++k) { System.out.println( (k/100.0)*(k/100) ); } 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

Fundamentals of Software Development 1Slide 5 int start, stop; for (int k = start; k <= stop; k++) { Same code as in the loop in the previous example }; 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

Fundamentals of Software Development 1Slide 6 Loop pattern: “count” Problem: How many integers from -1000 to 1000, inclusive, have their square divisible by 60? Display the count.Problem: How many integers from -1000 to 1000, inclusive, have their square divisible by 60? Display the count. int count; count = 0; for (int k = -1000; k <= 1000; ++k) { if ((k*k) % 60 = 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.

Fundamentals of Software Development 1Slide 7 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

Fundamentals of Software Development 1Slide 8 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 an instance of the class (not this.XXX) 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 + ””)

Fundamentals of Software Development 1Slide 9 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

Fundamentals of Software Development 1Slide 10 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: Motivation Many problems fit a “pattern” that experienced software developers recognizeMany."

Similar presentations