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

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

Computer Programming Lab(7).
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.
Fundamentals of Software Development 1Slide 1 Review: Why start with WordGames? You wrote your first lines of code in this course in WordGames.You wrote.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Fundamentals of Software Development 1Slide 1 Today’s Summary Hello World concepts – –main, static, console Programming patterns involving assignment and.
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.
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.
Computer Programming Lab(5).
Introduction to Java. Main() Main method is where the program execution begins. There is only one main Displaying the results: System.out.println (“Hi.
Java Basics (continued)
Java Coding 3 David Davenport Computer Eng. Dept., Bilkent University Ankara - Turkey. Over & over again!
COM S 207 While-Loop Statement Instructor: Ying Cai Department of Computer Science Iowa State University
Fundamentals of Software Development 1Slide 1 Recap: “From scratch” projects Today’s software engineering is almost NEVER “from scratch”Today’s software.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Console Input & Output CSS 161: Fundamentals of Computing Joe McCarthy 1.
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.
School of Computer Science & Information Technology G6DICP - Lecture 9 Software Development Techniques.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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,
Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
CMSC 202 Java Console I/O. July 25, Introduction Displaying text to the user and allowing the user to enter text are fundamental operations performed.
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.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Building java programs, chapter 3 Parameters, Methods and Objects.
1 CS 177 Week 6 Recitation Slides Review for Midterm Exam.
Functions Structured Programming. Topics to be covered Introduction to Functions Defining a function Calling a function Arguments, local variables and.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
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.
FOR LOOPS "LOOP FOR A SET NUMBER OF TIMES.". FOR ( START_VALUE; END_VALUE; INCREMENT_NUMBER ) { //YOUR_CODE_HERE } So after the word "for" (in lowercase)
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Java Basics (continued) Ms. Pack AP Computer Science A.
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.
Chapter 2 Basic Computation
Chapter 2 More on Math More on Input
Programming Patterns Many problems fit a “pattern” that experienced software developers recognize And hence can easily code, from experience Previously,
Fundamental of Java Programming Basics of Java Programming
User input We’ve seen how to use the standard output buffer
TK1114 Computer Programming
CSC 142 Computer Science II
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CSS 161 Fundamentals of Computing Introduction to Computers & Java
The for-loop and Nested loops
Building Java Programs
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Java Programming Loops
For Loops.
python.reset() Also moving to a more reasonable room (CSE 403)
Loops.
Java Programming Loops
Review of Previous Lesson
Building Java Programs
Computer Science Club 1st November 2019.
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 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 to 1000, inclusive, have their square divisible by 60? Display the count.Problem: How many integers from 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 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 Display the sum.Problem: Sum the cosines of the integers from -20 to 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?

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)