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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Fundamentals of Software Development 1Slide 1 Today’s Summary Statements: Conditionals (if-then-else)Statements: Conditionals (if-then-else) Investigated.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Fundamentals of Software Development 1Slide 1 Programming Patterns: Motivation Many problems fit a “pattern” that experienced software developers recognizeMany.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
CHAPTER 2 ANALYSIS OF ALGORITHMS Part 2. 2 Running time of Basic operations Basic operations do not depend on the size of input, their running time is.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
By Chad Blankenbeker.  The for-loop is best used when you know how many times it is going to be looped  So if you know you want it to only loop 10 times,
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
Topic 14 while loops and loop patterns Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems.
Fundamentals of Software Development IProgramming patterns involving iteration1 Overview LoopsOverview Loops We’ll also see how loops are often combined.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Fundamentals of Software Development 1Slide 1 Loops A loop is:A loop is: –a block of code that executes repeatedly while some condition holds true. Java.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Fundamentals of Software Development 1Slide 1 Programming Patterns Many problems fit a “pattern” that experienced software developers recognizeMany problems.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 5.1 – 5.2.
CPSC 233 Tutorial January 21 st /22 nd, Linux Commands.
Loops Review. How to generate random numbers Math.random() will return a random decimal value in the form of a double. double num1 = Math.random(); num1.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Loops A loop is: Java provides three forms for explicit loops:
CSC111 Quick Revision.
Good practices that help minimize the need for debugging
Java Coding 3 – part2 David Davenport Computer Eng. Dept.,
Chapter 5: Control Structures II
Fundamental of Java Programming Basics of Java Programming
Repetition-Counter control Loop
Incrementing ITP © Ron Poet Lecture 8.
TK1114 Computer Programming
CSC 142 Computer Science II
Building Java Programs
Building Java Programs
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
TOPIC 4: REPETITION CONTROL STRUCTURE
Building Java Programs
CSS 161 Fundamentals of Computing Introduction to Computers & Java
The for-loop and Nested loops
Chapter 4: Mathematical Functions, Characters, and Strings
Topic 14 while loops and loop patterns
Building Java Programs
Building Java Programs
Defining methods and more arrays
Building Java Programs
For Loops.
CS2011 Introduction to Programming I Loop Statements (II)
Building Java Programs
Building Java Programs
Java Programming Loops
Building Java Programs
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 14, 2011
Review of Previous Lesson
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
CSS161: Fundamentals of Computing
Building Java Programs
More on iterations using
Ch 5 : Mathematical Functions, Characters, and Strings
Presentation transcript:

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

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

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 8 2.8284271247461903 1.998614185980905 9 3.0 2.078560910383862 10 3.1622776601683795 2.152781734724373 11 3.3166247903554 2.222203177024025 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

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

Loop pattern: “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.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 2000. Display the sum. Answer on next slide. Fundamentals of Software Development 1

Loop pattern: “sum” Problem: Sum the cosines of the integers from -20 to 2000. 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

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

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

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

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

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

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

Fundamentals of Software Development 1