Cosc 1P02 Week 2 Lecture slides

Slides:



Advertisements
Similar presentations
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
Advertisements

Introduction to Computers and Programming Lecture 11: Introduction to Methods Professor: Evan Korth New York University.
Introduction to Computers and Programming Introduction to Methods in Java.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
1 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
Lecture 4. Review (Attributes and Methods) Class will contain –a set of attributes and methods public class Turtle { ///// Field (Attributes) ///// …
Arrays, Loops weeks 4-6 (change from syllabus for week 6) Chapter 4.
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 17 / 2008 Instructor: Michael Eckmann.
Turtle Graphics  Turtle graphics provide a simple way to draw pictures in a window.  The name suggests the way in which we can think about the drawing.
Games and Simulations O-O Programming in Java The Walker School
Building Java Programs
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Introduction to Programming Writing Java Beginning Java Programs.
COSC 1P02 Intro. to Computer Science 1.1 Cosc 1P02 Week 1 Lecture slides "If a man does his best, what else is there?” General George S. Patton ( )
School of Computer Science & Information Technology G6DICP - Lecture 22 The Theory of Object Oriented Programming.
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.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Methods in Java CSC1401. Overview In this session, we are going to see how to create class-level methods in Java.
Computer Science 111 Fundamentals of Programming I Introduction to Graphics.
COSC 1P02 Introduction to Computer Science 7.1 Cosc 1P02 Week 7 Lecture slides "There are two ways of constructing a software design; one way is to make.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
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.
Pictures Looping through pixels.. Lab Review (1) Objects  Instantiated from Class  Turtle myTut = new Turtle(myWorld);  new operator creates an instance.
For Wednesday No new reading Complete WebCT quiz.
Art 321 Lecture 7 Dr. J. Parker. Programming In order to ‘make things happen’ on a computer, you really have to program it. Programming is not hard and.
Turtle Graphics  Turtle graphics provide a simple way to draw pictures in a window.  The name suggests the way in which we can think about the drawing.
1 Turtle Graphics and Math Functions And how you can use them to draw cool pictures!
Manipulating Turtles CS1316: Representing Structure and Behavior.
CONTROL SYSTEMS Control Systems A command is a directive that performs a specific task An argument is a variable that can be used by the function reveiving.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Introduction to Programming Writing Java Beginning Java Programs.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
Arithmetic, Class Variables and Class Methods Week 11
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
1 Building Your Own Turtle Functions For making really cool pictures!
Objective You will be able to define the basic concepts of object-oriented programming with emphasis on objects and classes by taking notes, seeing examples,
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
Grammars, L-Systems Jim Whitehead UC Santa Cruz School of Engineering courses.soe.ucsc.edu/courses/cmps265/Spring14/01 23 Apr 2014.
Arrays Chapter 7.
Python Turtle Graphics
Intro CS – Loops & Creating Shapes
More methods, more capabilities
CompSci 230 Software Construction
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Transformations for GCSE Maths
Building Java Programs
Chap. 3 Functions Start Python IDLE (Shell) and open a new file.
Building Java Programs
Building Java Programs
Lecture 12: 2D Arrays AP Computer Science Principles
Transformations for GCSE Maths
Building Java Programs
Building Java Programs
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Building Java Programs
Presentation transcript:

Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

Turtle Graphics Seymour Papert late ’60s COSC 1P02 2008/09 Turtle Graphics Seymour Papert late ’60s teaching children to reason about processes Logo programming language Vector graphics Turtle has a position and direction and travels a distance direction measured in radians (2 = full circle) distance in drawing units (pixels) Brock libraries TurtleDisplayer provides a canvas for a turtle to draw on canvas is 300x300 pixels turtle starts in center facing right (i.e. 0 radians is facing right) can have any number of Turtles draw on the same canvas methods Turtle represents a turtle

Turtle Graphics. Analogy Time !!! We take a piece of paper and place it on the desk. We select a drawing tool, A pencil We put the pencil onto the paper We control the pencil by moving it forward, backward, right, left etc. Turtle Graphics are controlled much the same way. Use resources (pencil and paper) and instructions (methods) provided by BasicIO and Media.

TurtleDisplayer Methods COSC 1P02 2008/09 TurtleDisplayer Methods

COSC 1P02 2008/09 Turtle Methods

Two-by-Two Two turtles on different canvasses COSC 1P02 2008/09 Two-by-Two Two turtles on different canvasses 2 TurtleDisplayers & 2 Turtles 4 variables 2 for the displays 2 for the turtles Where’s yertle? move displays waitForUser() Analogy 2 pieces of paper 2 pencils, each draws on a dedicated surface.

Two-on-One Two (or more) turtles drawing on the same canvas COSC 1P02 2008/09 Two-on-One Two (or more) turtles drawing on the same canvas 1 TurtleDisplayer & 2 Turtles 3 variables Where are yertle’s lines move yertle moveTo(x,y) coordinates (cartesian) (o,o) is center x-coordinate is right(+)/left(-) y-coordinate is up(+)/down(-) Relative coordinates, moveTo repositions the relative location for the 1st turtle.

Variables, Objects, References & Assignment COSC 1P02 2008/09 Variables, Objects, References & Assignment Variables correspond to memory cells allocated when declared Objects are represented by some number of memory cells allocated when object created by new Objects have attributes Turtle location, direction, pen state (up/down), pen color, pen width display it is drawing on TurtleDisplayer current turtle doing drawing Assignment new produces a reference to (location in memory of) an object = replaces the current value stored in the variable with the value produced by the right hand side Example

This reference in assigned to a variable. COSC 1P02 2008/09 This reference in assigned to a variable. Declare variables TurtleDisplayer is created in memory, returns a reference (address) A Turtle object is created the same way. Each Object has memory which can be modified. Memory or Attributes can be set or accessed. An object can be assigned to multiple variables. Here mertle will refer to the original yertle object.

Drawing A Hexagon Geometry 6 sides COSC 1P02 2008/09 Drawing A Hexagon Geometry 6 sides exterior angle /3 (6 times around = 2) Often sequence repeated in Square tedious to rewrite large number of times? Repetition (loop) for statement index variable Square rewritten

COSC 1P02 2008/09

COSC 1P02 2008/09

Square 2 The result is the same package Example_2_2; import BasicIO.*; import Media.*; import static java.lang.Math.*; public class DrawSquare { private TurtleDisplayer display; // display to draw on private Turtle yertle; // turtle to do drawing /** The constructor draws a square using a loop. */ public DrawSquare ( ) { display = new TurtleDisplayer(); yertle = new Turtle(); display.setTurtle(yertle); yertle.penDown(); for ( int i=1 ; i<=4 ; i++ ) { yertle.forward(40); yertle.right(PI/2); }; yertle.penUp(); display.close(); }; // constructor public static void main ( String[] args ) { new DrawSquare(); }; } The result is the same The for loop allows us to do this 4 times, once for each side of the square One side of the square is drawn, and the turtle rotated 90o display

COSC 1P02 2008/09

COSC 1P02 2008/09 Poly-Spiral A spiral has lines of increasing length drawn at same angle smaller the angle the tighter the spiral Essentially a loop repeatedly draw a line and turn Line length changes? previously length was constant (used a constant: 40) now length is variable (use a variable) Variable declaration line length is a number of drawing units (countable) in Mathematics, the countable numbers are called integers in Java integer is represented by the type int Assignment = replaces value of variable on left with value computed on right Increase len each time a line drawn (i.e. in loop) Initial value? set before loop Variables & assignment revisited

COSC 1P02 2008/09

COSC 1P02 2008/09

PolySpiral

Nesting Mathematical expressions use composition 2(4+1) We can expand this to 8+2 This can be viewed as (4+1) done twice. Serves as a way of simplification. Code behaves much the same way If we can do 1 thing Draw a square We could do this multiple times 8(Draw a square) 8 Squares Example Tip: Factor code like you would a mathematical expression. This can simplify code by using composition. Fewer lines of code usually means easier to understand.

Repeating A Pattern Draw 8 squares in pattern Repetition 8 squares COSC 1P02 2008/09 Repeating A Pattern Draw 8 squares in pattern Repetition 8 squares rotate between for loop Composition we already know how to draw a square write program using composition by inserting code for square in code for repetition of pattern also called nesting different index variables inner (nested) loop (i) done completely for each time through outer loop (j)

8 Squares public class Draw8Squares { private TurtleDisplayer display; // display to draw on private Turtle yertle; // turtle to do drawing // The constructor draws 8 squares in a pattern. public Draw8Squares ( ) { display = new TurtleDisplayer(); yertle = new Turtle(); display.setTurtle(yertle); yertle.penDown(); for ( int j=1 ; j<=8 ; j++ ) { for ( int i=1 ; i<=4 ; i++ ) { yertle.forward(40); yertle.right(PI/2); }; yertle.right(PI/4); yertle.penUp(); display.close(); }; // constructor public static void main ( String[] args ) { new Draw8Squares(); }; The variables i & j are counters used to control the loops. Each must be unique. A single square is drawn This for loop controls how many squares will be drawn. In our case 8(squares). The turtle is rotated 45 degrees, or PI/4. This sets up the turtle to draw the next square

8 Squares.

COSC 1P02 2008/09

COSC 1P02 2008/09

COSC 1P02 2008/09

COSC 1P02 2008/09 Repeated Patterns Complex (repeating) patterns can be created using composition Code for pattern nested (composed) inside of code for repetition Some sort of transition between patterns e.g. rotating turtle in EightSquares e.g. moving over to next position General algorithm starting position repetition pattern transition Tiling pattern for one line nested in repetition for many lines

COSC 1P02 2008/09

Tiled Image Create a square, using composition Start of tiling move to top left repeat for as many lines as needed repeat for as many patterns as needed draw pattern (tile) transition to next pattern on line move to left side Create a square, using composition

The end