1 CS1110, 8 March 2009 Two topics: elementary graphics (for A5); loops Reading: Sec. 2.3.8 and chapter 7 on loops. The lectures on the ProgramLive CD can.

Slides:



Advertisements
Similar presentations
1 CS100J February 28, 2006 Loops, iterative statements, or repetitive statements A bit about graphics From news.com, on 23 February 2006, about browser.
Advertisements

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.
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.
1 CS100J 15 March, 2006 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Some anagrams A decimal.
Introducing Loop Statements Liang, pages Loop statements control repeated execution of a block of statements Each time the statements in the block.
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
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.
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
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.
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.
1 CS October 2008 The while loop and assertions Read chapter 7 on loops. The lectures on the ProgramLive CD can be a big help. Quotes for the Day:
CS1022 Computer Programming & Principles Lecture 1.2 A brief introduction to Python.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Chapter 12: How Long Can This Go On?
COSC 1P02 Introduction to Computer Science 3.1 Cosc 1P02 Week 3 Lecture slides Birthdays are good for you. Statistics show that the people who have the.
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
CRE Programming Club - Class 4 Robert Eckstein and Robert Heard.
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.
Visual Basic Programming
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
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.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
CS November 2010 Developing array algorithms. Reading: Haikus (5-7-5) seen on Japanese computer monitors Yesterday it worked. Today it is.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
Algorithms Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin,
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 10.
Loops Robin Burke IT 130. Outline Announcement: Homework #6 Conditionals (review) Iteration while loop while with counter for loops.
CS100A, Fall Lecture 8 1 CS100A, Fall 1997 Lecture 8, Thursday, 25 September. More on Iteration We’ll develop some loops of the form: // general.
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.
CS305j Introduction to Computing Simple Graphics 1 Topic 11 Simple Graphics "What makes the situation worse is that the highest level CS course I've ever.
CS 5JA Introduction to Java Graphics One of the powerful things about Java is that there is.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
int [] scores = new int [10];
1 CS100J September 27, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
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.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
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 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
CS 112 Introduction to Programming Java Graphics Yang (Richard) Yang Computer Science Department Yale University 208A Watson, Phone:
Basic Graphics 03/03/16 & 03/07/16 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
1 Recursion and induction We teach these early, instead of new object- oriented ideas, so that those who are new to Java can have a chance to catch up.
CompSci 4 Java 4 Apr 14, 2009 Prof. Susan Rodger.
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2015.
UCT Department of Computer Science Computer Science 1015F Iteration
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
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.
CS October 2008 The while loop and assertions
int [] scores = new int [10];
Looping Topic 4.
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
For loops Taken from notes by Dr. Neil Moore
February , 2009 CSE 113 B.
CSC1401 Manipulating Pictures 2
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.
Presentation transcript:

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 be a big help. Videonote can be a big help. Assignment A5: graphics, loops, recursion Due date: Thursday Mar 31 at 11:59pm Prelim 1: Tonight,! 7:30pm, Uris Hall 101

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 // Draw line from (10, 10) to (50, 40). graphics.drawLine(10,10,50, 40); // Draw rectangle: top-left point (2, 5), width 40, height 60 graphics.drawRect(2, 5, 40, 60); import javax.swing.*; import java.awt.*; jframe= new JFrame("Turtle window"); jpanel= new JPanel(); jframe.getContentPane().add(jpanel, BorderLayout.CENTER); jframe.pack(); jframe.setVisible(true); graphics= jpanel.getGraphics(); // contains methods to draw on jpanel // Draw string “this” at (40, 30) graphics.drawString(“this”, 40, 30); // set the pen color to red graphics.setColor(Color.red); // Store the current color in c Color c= graphics.getColor(); You don’t have to learn all this now (unless you want to). We will be telling you more about later. For more on graphics, see class Graphics in the Java API and page 1-5 in the CD ProgramLive. For more on GUIs, read chapter 17. The corresponding part of the CD is arguably even more helpful.

44 In A5, write methods to draw shapes and spirals, and draw things using recursive procedures. Assignment A5: drawing with a Turtle Use a class developed by the ACM: point (x, y): where the “Turtle” is angle: the direction the Turtle faces a pen color whether the pen is up or down Class Turtle has methods for moving a Turtle around, drawing as it goes. Draw equilateral triangle with side lengths 30; turtle ending up at starting point and facing the same direction: t.forward(30); t.left(120); 0 degrees east North 90 degrees 180 degrees west 270 degrees

5 From recursion to loops: doing things repeatedly Write programs to make computers do things. Often want to make them do things many 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 CUAUV team, vehicle has to continue moving to get somewhere

6 From recursion to loops: doing things repeatedly We haveve talked about recursion. Alternatives: while-loops for-loops (special syntax for common special cases) ; while (stuff still to do) { ; }

7 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; }

8 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; } Called a “flow chart” To execute the for-loop. 1.Execute initialization. 2.If loop condition false, terminate execution. 3.Execute repetend. 4.Execute increment, repeat from step 2. i= 2; i <= 200 i= i +1; true false x= x + i*i;

9 Note on ranges contains 2, 3, 4, 5. It contains 5+1 – 2 = 4 values The number of values in m..n is n+1 – m. In the notation m..n, we require always, without saying it, that m <= n + 1. If m = n + 1, the range has 0 values contains 2, 3, 4. It contains 4+1 – 2 = 3 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

10 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). (Small) part of the answer: use URL cues “Deep” URLs are usually less important, e.g.,

11 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] // 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 for (int i= c; i <= d; i= i + 1) { Process integer i; }

12 Our goal: Provide you with a methodology for the development of loops that process a range of integers. 1. Separate concerns —focus on one thing at a time. 2. Make small steps toward completing the loop. 4. Keep program simple. 3. Don’t introduce a new variable without a good reason. 1. Set c to the number of chars is String s that are digits (in 0..9). 2. Store in res a copy of String s but with no blanks. 3. Store in res a copy of String s but with adjacent duplicates removed. 4. Set boolean v to the value of “no integer in 2..n–1 divides x”. 5. Set boolean v to the value of “every element in Vector v is an object of class JFrame”. 6. Add up the squares of the odd integers in the range m..n. Try these. Test in DrJava