cosc 1p02 introduction to computer science 2.1 cosc 1p02 week 2 lecture slides you can pretend to be...

12
COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

Upload: chester-lawrence-ellis

Post on 17-Dec-2015

232 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.1

Cosc 1P02

Week 2 Lecture slides

You can pretend to be serious; you can't pretend to be witty.

- Sacha Guitry

Page 2: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.2

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 methods

Page 3: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.3

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.

Page 4: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.6

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.

Page 5: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.7

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 centerx-coordinate is right(+)/left(-)y-coordinate is up(+)/down(-)

Relative coordinates, moveTo repositions the relative location for the 1st turtle.

Page 6: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.8

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

Page 7: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.10

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

Page 8: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.15

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

Page 9: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.19

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.

Page 10: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.20

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)

Page 11: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.26

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

Page 12: COSC 1P02 Introduction to Computer Science 2.1 Cosc 1P02 Week 2 Lecture slides You can pretend to be serious; you can't pretend to be witty. - Sacha Guitry

COSC 1P02

Introduction to Computer Science 2.29

The end