1 CS1110, 20 Oct. 2009, Lec 14 Elementary graphics; intro to loops and for-loops Reading: Sec. 2.3.8 and chapter 7 on loops. The lectures on the ProgramLive.

Slides:



Advertisements
Similar presentations
First of all – lets look at the windows you are going to use. At the top you have a toolbar, with all your various tools you can use when customising your.
Advertisements

CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
RAPTOR Syntax and Semantics By Lt Col Schorsch
1 CS100J February 28, 2006 Loops, iterative statements, or repetitive statements A bit about graphics From news.com, on 23 February 2006, about browser.
1 CS March 2009 While loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim in two days: Th 7:30-9pm Uris Aud.
CSE 1301 Lecture 6B More Repetition Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Computer Science 1620 Loops.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 CS100J October 06, 2005 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
CS 1110 Prelim III: Review Session 1. Info My name: Bruno Abrahao – We have two other TA’s in the room to help you individually Beibei Zhu Suyong Zhao.
Loops – While, Do, For Repetition Statements Introduction to Arrays
More loops Linag, Chpt 3, pp The do-loop continue- condition ? loop-body statements next statement false true WHILE-LOOP continue- condition? loop-body.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
CSE 113 Week 5 February , Announcements  Module 2 due 2/15  Exam 3 is on 2/15  Module 3 due 2/22  Exam 4 is on 2/25  Module 4 due 2/29.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
110-K1 Iterations (1) Up to now, need to call the procedure again (e.g. click again on a command button) To repeat a piece of code: Can also use loops:
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Invitation to Computer Science, Java Version, Second Edition.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Chapter 2 - Algorithms and Design
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
1 CS1110, 8 March 2009 Two topics: elementary graphics (for A5); loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can.
1 CS March 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Visual Basic Programming
1 Chapter 2 - Algorithms and Design print Statement input Statement and Variables Assignment Statement if Statement Flowcharts Flow of Control Looping.
1 CS100J 4 March 2008 Two topics: Turtles; loops Start reading Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 3, Lecture 1.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
Introduction to Loops Iteration Repetition Counting Loops Also known as.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014 Office hours: Thursday 1300 – 1400hrs.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Getting started with the turtle Find the latest version of this document at
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
5. Loops 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
1 CS100J 06 March 2008 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal pointI'm.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CS100J October 21, 2003 For Loops Reading: Secs Quote for the Day: Perhaps the most valuable result of all education is the ability to make yourself.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
UCT Department of Computer Science Computer Science 1015F Iteration
Start reading Sec and chapter 7 on loops
Introduction To Repetition The for loop
Lecture 6 Repetition Richard Gesick.
CS October 2009 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
CS 115 Lecture 8 Structured Programming; for loops
Lecture 4A Repetition Richard Gesick.
Structured Program
Looping Topic 4.
Searching, Sorting, and Asymptotic Complexity
Loops.
Building Java Programs
CS100J 16 Oct, 2007 Assignment A5: loops
CS October 2010 Read: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal point I'm.
Chapter 4: Loops and Iteration
Presentation transcript:

1 CS1110, 20 Oct. 2009, Lec 14 Elementary graphics; intro to loops and for-loops Reading: Sec and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. A5 is out: graphics, loops, recursion. Due Sat. Oct 30th. Talks on Thursday Oct 21: Adaptive Drama Management: Bringing Machine Learning to Interactive Entertainment 4:15pm, Upson B17 (computer science, game design, social psychology) Computational Advertising, 7pm, Phillips 203 (intersection of computer science and econ) Prelim 2: Tu Nov 9 th, 7:30-9pm. Conflicts? Submit CMS "assignment" "P2 conflicts" by Oct 26 th. Have your iClicker out.

22 Graphical User Interfaces (GUIs): graphics. A JFrame, with a "panel" on which you can draw A “panel” in which you can draw On the panel, each pair (x,y) indicates a “pixel” or picture element. For Assignment 5, you need to understand that: x-coordinates increase rightward y-coordinates increase downward. (0,0) (1,0) (2,0) … (0,1) (1,1) (2,1) … (0,2) (1,2) (2,2) … …

33 In A5, write methods to draw shapes and spirals, and draw things using recursive procedures. Assignment A5: drawing with a Turtle We'll use ACM's GraphicsProgram, which supplies a "GTurtle": point (x, y): where the “Turtle” is angle: the direction the Turtle faces a pen color whether the pen is up or down Class GTurtle has methods for moving a GTurtle around, drawing as it goes. Draw equilateral triangle with side lengths 30; turtle t ending up at starting point and facing the same direction: t.forward(30); t.left(120); 0 degrees 90 degrees 180 degrees 270 degrees

4 From recursion to loops: doing things repeatedly We write programs to make computers do things. We often want to make them do things multiple times. 1.Perform n trials or get n samples. A5: draw a triangle six times to make a hexagon Run a protein-folding simulation for 10 6 time steps 2.Process each item in a given String, Vector, or other “list” Compute aggregate statistics for a dataset, such as the mean, median, standard deviation, etc. Send everyone in a certain (Facebook) group an individual appointment time 3.Do something an unknown number of times ALVINN, the van that learned to drive itself, continuously watched human driving behavior and adjusted its model accordingly

5 From recursion to loops: doing things repeatedly We’ve talked about recursion. Alternatives: while-loops for-loops (special syntax for cases involving a "counter") ; while (stuff still to do) { ; } for ( ; ; ) { ; }

6 The for loop, for processing a range of integers x= 0; // add the squares of ints // in range to x x= x + 2*2; x= x + 3*3; … x= x + 200*200; for each number i in the range , add i*i to x. The for-loop: for (int i= 2; i <= 200; i= i +1) { x= x + i*i; } loop counter: i initialization: int i= 2; loop condition: i <= 200; increment: i= i + 1 repetend or body: { x= x + i*i; }

7 Execution of the for-loop The for-loop: for (int i= 2; i <= 200; i= i +1) { x= x + i*i; } loop counter: i initialization: int i= 2; loop condition: i <= 200; increment: i= i + 1 repetend or body: { x= x + i; } To execute the for-loop. 1.Execute initialization. 2.If loop condition false, exit the loop (skip 3&4). 3.Execute repetend. 4.Execute increment, repeat from step 2. Called a “flow chart” i= 2; i <= 200 i= i +1; true false x= x + i*i;

8 Application: URL analysis for search engines Problem: how does a search engine (e.g., Google) decide which webpages are the most important to present? This requires counting the number of slashes in a URL (given as a String). You know a recursive solution; next slide: loop solution. (Small) part of the answer: use URL cues “Deep” URLs are usually less important, e.g.,

9 The pattern for processing range of integers: range a..b-1 range c..d for (int i= a; i < b; i= i + 1) { Process integer i; } // store in count # of ‘/’s in String s // inv: count is # of ‘/’s in s[0..i-1] count=0; for (int i= 0; i < s.length(); i= i +1) { if (s.charAt(i) == '/') count= count+1; } // count is # of ‘/’s in s[0..s.length()-1] for (int i= c; i <= d; i= i + 1) { Process integer i; } // Store in double var. v the sum // 1/1 + 1/2 + …+ 1/n v= 0; // call this 1/0 for today // inv: v is 1/1 + 1/2 + …+ 1/(i-1) for (int i= 1; i <= n; i= i +1) { v= v / i; } // v= 1/1 + 1/2 + …+ 1/n

10 Note on range notation (later, will make reasoning about loops easier) 2..5 contains 2, 3, 4, 5. It contains 5+1 – 2 = 4 values The number of values in m..n is n+1 – m: "follower minus first" In the notation m..n, we require always, without saying it, that m <= n + 1 (so, “2..1” is OK but not “2..0”) If m = n + 1, the range has 0 values contains 2, 3, 4. It contains 4+1 – 2 = 4 values 2..3 contains 2, 3. It contains 3+1 – 2 = 2 values 2..2 contains 2. It contains 2+1 – 2 = 1 values 2..1 contains.It contains 1+1 – 2 = 0 values

11 Application: Some Personalized (SPEM) Problem: how can we get people to read our mass announcements? This requires storing individualized information, iterating over the items we stored, and figuring out msg/mail output. One answer: make it personal. Only one recipient Customized message (“Hi Lisa, great seeing you at the talk yesterday. Don’t forget the meeting tomorrow”; “Hail Batman. What this course needs is a better class of Criminal. Don’t forget the meeting tomorrow”) We don’t want to add duplicate recipients to the list (people notice and hate getting redundant s).

12 Some Personalized (SPEM): design decisions How shall we represent a group of recipients (e.g., TAs vs. students)? The usual design problem: how should we lay everything out? We want the functionality of Vectors (so we can add recipients), … but we want to modify that functionality to be suitable for our purposes (no adds of duplicate recipients, ability to mail each recipient). new class MailRecip with appropriate equals method [remember last lecture?], personalization and mailing methods new class MailGroup extending Vector, using MailRecip's equals method to prevent addition of duplicates

13 Some Personalized (SPEM): design decisions Where should we put new method "add" (which silently does nothing when someone attempts to add a duplicate)? … We should ensure there is no way to add duplicates to the list. A.static method in MailRecip MailRecip.add(lisa, staff) B.object method in MailRecip list.add(staff) C.static method in MailGroup MailGroup.add(lisa, staff) D.object method in MailGroupstaff.add(lisa) E.no new method needed; just use Vector's staff.add(lisa)