1 CS100J 4 March 2008 Two topics: Turtles; loops Start reading Sec. 2.3.8 and chapter 7 on loops. The lectures on the ProgramLive CD can be a big help.

Slides:



Advertisements
Similar presentations
Graphics Chapter 16.  If you want to draw shapes such as a bar chart, a clock, or a stop sign, how do you do it?
Advertisements

Code Elements and Processing Coordinate System. Code elements: pages Comments: are documentation notes that are ignored by the computer but are.
Lecture 10.2 Different Types of Loops, including for and do.
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.
Quadratic Equations In One Unknown
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
CS100J October 07, 2003 Loops Repetitive statements, or iterative statements, or loops “O! Thou hast damnable iteration and art, indeed, able to corrupt.
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.
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
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Recitation 7. Developing loops Introduction. This recitation concerns developing loops using their invariants and bound functions. Your recitation instructor.
Lesson Three: Organization
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
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:
Chapter 2 - Algorithms and Design
1 CS100J 09 March, 2006 More on Loops Reading: Secs 7.1–7.4 A Billion. The next time you hear someone in government rather casually use a number that includes.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
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.
Loops & Graphics IP 10 Mr. Mellesmoen Recall Earlier we wrote a program listing numbers from 1 – 24 i=1 start: TextWindow.WriteLine(i) i=i+1 If.
CONTROL FLOW The order in which blocks are executed is called the “control flow” of the script So far all our scripts have just executed blocks in the.
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 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Getting started with the turtle Find the latest version of this document at
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
1 CS April 2010 while loops Reading: today: Ch. 7 and ProgramLive sections. For next time: Ch Prelim 2. Thursday evening, 7:30PM Watch.
Algorithms and Pseudocode
1 SIC / CoC / Georgia Tech MAGIC Lab Rossignac Processing  Install Processing  Learn how to edit, run, save, export,
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
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.
1 CS100J Spring Instructor: David Gries CS100M: Matlab No prior programming experience One semester of calculus Math & engineering type 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)
Recursion in Java The answer to life’s greatest mysteries are on the last slide.
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.
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.
UCT Department of Computer Science Computer Science 1015F Iteration
Chapter 02 Data and Expressions.
Correctness issues and Loop invariants
Start reading Sec and chapter 7 on loops
Basic Graphics Chapter 5 3/19/15 Thursday Section Only
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 October 2008 The while loop and assertions
One-Dimensional Array Introduction Lesson xx
Testing change to a linked list
Lecture Notes – Week 3 Lecture-2
Chap. 3 Functions Start Python IDLE (Shell) and open a new file.
Looping Topic 4.
Lecture 8:The For Loop AP Computer Science Principles.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
CS100A Lecture 25 1 December 1998 Chance to replace grade on Prelim 3, Question 2. Everyone is expected to be in lecture on Thursday, 3 December.
CS100J 10 April 2005 Rectangular arrays and ragged arrays. Secs. 9
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 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. The next time someone rather casually use a number that includes the word "billion", think about it. A billion seconds ago was A billion minutes ago Jesus was alive. A billion hours ago our ancestors were living in the Stone Age. A billion days ago no creature walked the earth on two feet. A billion dollars lasts 8 hours and 20 minutes at the rate our government spends it.

2 Graphical User Interfaces (GUIs): graphics. A JFrame, with a "panel" on which you can draw jframe= new JFrame("Turtle window"); jpanel= new JPanel(); jpanel.setPreferredSize(new Dimension(width, height)); jpanel.setBackground(Color.white); jframe.getContentPane().add(panel, BorderLayout.CENTER); jframe.pack(); jframe.setVisible(true); graphics= jpanel.getGraphics(); A “panel” in which you can draw You don’t have to learn all this unless you want to. We will be telling you more and more about GUIs as the course progresses.

3 Commands to draw // Draw line from (10, 10) to (50, 40). d.graphics.drawLine(10,10,50, 40); // Draw rectangle: top-left point (2, 5), width 40, height 60 d.graphics.drawRect(2, 5, 40, 60); // Fill rectangle: top-left point (50, 70), width 40, height 60 d.graphics.fillRect(50, 70, 40, 60); (0,0) (0,1) (0, 2) … (1,0) (1,1), (1,2) … (2,0) (2,1), (2,2) … … The panel: each pair (i,j) is a “pixel” or picture element.

4 // Draw string s at (40, 30) d.graphics.drawString(s, 40, 30); // set the pen color to red d.graphics.setColor(Color.red); // Store the current color in c Color c= d.graphics.getColor(); // Draw oval: top-left point (2, 5), width 40, height 60 d.graphics.drawRect(2, 5, 40, 60); // Fill an oval: top-left point (50, 70), width 40, height 60 d.graphics.fillRect(50, 70, 40, 60); (0,0) (0,1) (0, 2) … (1,0) (1,1), (1,2) … (2,0) (2,1), (2,2) … … 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 corresponding part of the CD is much easier!

5 In A5, write methods to draw shapes, draw spirals, make balls that move and bounce off the sides of the window, and draw things using recursive procedures. Assignment A5: drawing with a Turtle We have written a class Turtle, an instance of which maintains: point (x, y): where the “Turtle” is angle: the direction the Turtle faces a pen color whether 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 ends up at starting point and facing the same direction: forward(30); addAngle(120); 0 degrees 90 degrees 180 degrees 270 degrees

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; repetend: the thing to be repeated. The block: { x= x + i*i; } 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 <= 4; i= i +1) { x= x + i*i; } loop counter: i initialization: int i= 2; loop condition: i <= 4; 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 <= 4 i= i +1; true false x= x + i*i;

8 Execution of the for-loop x= 1; for (int i= 1; i <= 4; i= i +1) { x= 2*x; } Called a “flow chart” i= 2; i <= 4 i= i +1; true false x= x + i*i; At the end: A.x = 16 i = 5 B.x = 16 i = 4 C.x = 8 i = 5 D.x = 32 i = 32 E.none of the above 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.

9 Execution of the for-loop The for-loop: for (int i= 2; i <= 4; i= i +1) { x= x + i*i; } loop counter: i initialization: int i= 0; loop condition: i <= 4; increment: i= i + 1 repetend or body: { x= x + i; } Trace execution of for-loop. We do it as shown below, rather than using a single box, for x and one for i, so that we can keep track of when events happened. x i2345

10 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–1 <= n. If m–1 = n, 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 Pattern for processing range of integers: range a..b-1 range c..d for (int k= a; k < b; k= k + 1) { Process integer k; } // Print the integers in 10..n-1 // inv: All ints in 10..k-1 been printed for (int k= 10; k < n; k= k +1) { System.out.println(k); } // All ints in 10..n-1 been printed for (int i= c; i <= d; i= i + 1) { Process integer i; } // Print the integers in // inv: All ints in 10..i-1 printed for (int i= 1; i <= 10; i= i +1) { System.out.println(i); } // All ints in 10..i-1 printed

12 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; } // Print indices of all ‘e’s in String s // inv: Indices of ‘e’s in s[0..s.i-1] for (int i= 0; i < s.length(); i= i +1) { if (s.charAt(i) == ‘e’) System.out.println(i); } // Indices of ‘e’s in s[0..s.length()-1] // printed 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; // inv: 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

13 Loops are often not easy to develop or understand. Our goal: Provide you with a methodolgy for the development of loops that process a range of integers. 1. Separate your 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.

14 Development of a loop to process a range a..b for (int i= a; i <= b; i= i + 1) { Process integer i; } // Store in m the sum of even // numbers in Follow this methodology for ease in writing loops!!! Step 1. Recognize that a range of integers has to be processed. Step 3. Write the skeleton of the loop. for (int k= ; ; ) { // Process k } Step 4. Fill in the loop control. 10k <= 46k= k+1 Step 5. Write down, before the loop, what the variables mean and initialize other variables. // m = sum of even ints in 10..(k-1) Step 6. Write the method body (to process k). if (k % 2 == 0) { m= m + k; } // m = sum of even ints in Step 2. Write a postcondition, based on the spec, which says what is true at the end. m= 0;

15 Development of a loop to process a range a..b-1 for (int i= ; ; ) { Process integer i; } // Set c to the number of chars in String s that are digits 0..9 What is the range of integers to process? A.1.. s.length() B.1.. s.length() – 1 C.0.. s.length() D.0.. s.length() – 1 E.I don’t know.

16 Development of a loop to process a range a..b-1 for (int i= ; ; ) { Process integer i; } // Set c to number of chars in String s that are digits ‘0’..’9’ What is the the postcondition? A.c = no. of chars in s that are in ‘0’..’9’ B.c = no. of chars in s[0..s.length()-1] that are in ‘0’..’9’ C.c = no. of chars in s[0..s.length()] that are in ‘0’..’9’ D.A or B E.I don’t know

17 Development of a loop to process a range a..b-1 for (int i= ; ; ) { Process integer i; } // c = no. of chars in s[0..s.length()-1] that are in ‘0’..’9’ // Set c to number of chars in String s that are digits ‘0’..’9’ Write the initialization, loop condition, and increment A.for (int i= 1; i <= 9 ; i= i + 1 ) B.for (int i= 1; i <= s.length() ; i= i + 1 ) C.for (int i= 1; i < s.length() ; i= i + 1 ) D.for (int i= 0; i < s.length() ; i= i + 1 ) E.for (int i= 0; i <= s.length() - 1 ; i= i + 1 )

18 Development of a loop to process a range a..b-1 // What should be true here about c and i? for (int i= 0; i < s.length(); i= i + 1 ) { Process integer i; } // c = no. of chars in s[0..s.length()-1] that are in ‘0’..’9’ // Set c to number of chars in String s that are digits ‘0’..’9’ A.// c= no. of chars in s[0..i-1] that are in ‘0’.. ‘9’ B.// c= no. of chars in s[0..i] that are in ‘0’.. ‘9’ C.// c= no. of chars in s[1..i] that are in ‘0’.. ‘9’ D.I don’t know.

19 Development of a loop to process a range a..b-1 // inv: c = no. of chars of s[0..i-1] that are in ‘0’..’9’ for (int i= 0; i < s.length(); i= i + 1 ) { Process integer i; } // c = no. of chars of s[0..s.length()-1] that are in ‘0’..’9’ // Set c to number of chars is String s that are digits ‘0’..’9’ How should c be initialized c? A.c= 1;C. c= 5; B.c= 0;D. c= -1;

20 Try these problems. Develop them using the methodology given on slide 9. Then type them into DrJava and test them! 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.