Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Patterns Many problems fit a “pattern” that experienced software developers recognize And hence can easily code, from experience Previously,

Similar presentations


Presentation on theme: "Programming Patterns Many problems fit a “pattern” that experienced software developers recognize And hence can easily code, from experience Previously,"— Presentation transcript:

1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognize And hence can easily code, from experience 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 Later, we will see other looping patterns, including: Max/min Loops inside loops Fundamentals of Software Development 1

2 Programming patterns (non-looping)
absolute value maximum of two swap if (y < 0) { x = -y; } else { x = y; } if (x > y) { z = x; } else { z = y; } temp = x; x = y; y = temp; 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’; } Increment (for counting) Select from cases x = x + 1; Keep these patterns in mind, to help you with similar problems that you encounter Fundamentals of Software Development 1

3 Loop pattern: “do N times”
Problem 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 1st, 2nd and 3rd roots of 8 through 11, inclusive 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(); } Note the “cast” of k to type double Fundamentals of Software Development 1

4 Loop pattern: “do N times”
Problem 3: Display the 1st, 2nd and 3rd roots of start through stop, inclusive, where start and stop are determined at run time by the user 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 }; This example also shows how to do console input Note that start and stop are both variables here Fundamentals of Software Development 1

5 Loop pattern: “count” Problem: How many integers from to 1000, inclusive, have cosines that are negative? Display the count. int count; count = 0; for (int k = -1000; k <= 1000; ++k) { if (Math.sin((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 Display the sum. Answer on next slide. Fundamentals of Software Development 1

6 Loop pattern: “sum” Problem: Sum the cosines of the integers from -20 to Display the sum. sum has type double here, because the sum is of double’s 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 Fundamentals of Software Development 1

7 Loop pattern: “loop through a list”
Problem: Write a method that returns how many times a given character appears in a given string A static method is one that does not use any of the fields of the class 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; 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 1

8 Loop pattern: “sum” Sums the digits in a string of digits: int digit;
public static int digitSum(String s) { int digit; int sum = 0; for (k = 0; k < s.length(); k++) { digit = Integer.parseInt(s.substring(k, k+1)); sum = sum + digit; } return sum; Fundamentals of Software Development 1

9 Loop pattern: “maximum”
Finds the largest digit in a string of digits: public static int digitMax(String s) { int digit; int max = Integer.parseInt(s.substring(0, 1)); for (k = 1; k < s.length(); k++) { digit = Integer.parseInt(s.substring(k, k+1)); if (digit > max) { max = digit; } return max; Fundamentals of Software Development 1

10 Loop Pattern: “find-first”
Find the first place where a given character appears in a string. Return -1 if the character does not appear. public int findChar(String s, char c) { int i = 0; while (i < s.length()) { if (s.charAt(i) == c) { return i; } i++; return -1; // Not found for (i=0; i < s.length(); i++) { if (s.charAt(i) == c) { return i; } Fundamentals of Software Development 1

11 Loop Pattern: “do-forever”
Our instruction-followers often go “forever”: while (true) { System.out.println("hi"); } for (; true;) { System.out.println("hi"); } Fundamentals of Software Development 1

12 Loop pattern: “break in middle”
while (true) { ... if (...) { break; } continue; would continue the loop, skipping the rest of the current iteration Fundamentals of Software Development 1

13 Fundamentals of Software Development 1


Download ppt "Programming Patterns Many problems fit a “pattern” that experienced software developers recognize And hence can easily code, from experience Previously,"

Similar presentations


Ads by Google