Processing Recap CS 106 Winter 2019 1 Processing is a language a - - PowerPoint PPT Presentation

processing recap
SMART_READER_LITE
LIVE PREVIEW

Processing Recap CS 106 Winter 2019 1 Processing is a language a - - PowerPoint PPT Presentation

Module 01 Processing Recap CS 106 Winter 2019 1 Processing is a language a library an environment 2 Variables A variable is a named value. It has a type (which cant change) and a current value (which can change). 3


slide-1
SLIDE 1

Module 01

Processing Recap

CS 106 Winter 2019

1

slide-2
SLIDE 2

Processing is…

…a language …a library …an environment

2

slide-3
SLIDE 3

Variables

A variable is a named value. It has a type (which can’t change) and a current value (which can change).

3

slide-4
SLIDE 4

Variables

A declaration introduces a new variable, and

  • ptionally gives it an initial value.

int a; float b = 6.28; boolean c = b > 19;

Three declarations

4

slide-5
SLIDE 5

Variables

Say a variable’s name to read from it. Use assignment (=) to write to it. Processing includes many built-in names.

  • True constants can’t be changed.
  • Some variables are meant to be read-only.
  • Some are updated automatically, and are

meant to be read repeatedly.

5

slide-6
SLIDE 6

CQ

What does this program print? (A) 5 (B) 15 (C) 22 (D) a + 5 (E) Nothing int a = 17; void test( int a ){ println( a + 5 ); } void setup(){ test( 10 ); }

6

slide-7
SLIDE 7

Scope

Every declaration in Processing has a scope: the part of the program source code in which that declaration is valid. Usually either “global” or bounded by the nearest enclosing {}. Scope is a complicated topic. If in doubt, just avoid re-using the same names!

7

slide-8
SLIDE 8

Control flow

By default, Processing will execute statements in the

  • rder they’re given. Control flow can modify that
  • rder.

8

slide-9
SLIDE 9

Conditionals

if( keyPressed && key == ' ' ) { ellipse( mouseX, mouseY, 20, 20 ); } An if statement

9

slide-10
SLIDE 10

Conditionals

if( keyPressed && key == ' ' ) { ellipse( mouseX, mouseY, 20, 20 ); } else { rect( mouseX, mouseY, 20, 20 ); }

10

slide-11
SLIDE 11

Conditionals

if ( keyPressed ) { if ( key == 'e' ) { ellipse( mouseX, mouseY, 20, 20 ); } else if ( key == 'l' ) { line( 10, 10, 100, 100 ); } else { rect( mouseX, mouseY, 20, 20 ); } }

11

slide-12
SLIDE 12

While loops

int y = 0; while( y < height ) { line( 0, y, width, y ); y = y + 10; }

12

slide-13
SLIDE 13

For loops

for( int y = 0; y < height; y += 10 ) { line( 0, y, width, y ); }

13

slide-14
SLIDE 14

Functions

A function gives a name to a computation. Benefits:

  • Ease of (error-free) repetition.
  • Encapsulation: hide the messy details.
  • Abstraction: think about problem solving at a

higher level.

  • Establish a point of connection between parts
  • f a program.

14

slide-15
SLIDE 15

Hooks

Processing knows about a few predetermined function names. If you define functions (hooks) with those names, Processing will call them at the right times. Examples: setup(), draw(), mousePressed(), keyPressed() Some libraries add more hooks.

15

slide-16
SLIDE 16

float[] ts1 = {

  • 4.8,
  • 4.79, -4.764, -4.762,
  • 4.764, -4.824, /* 86 more numbers... */
  • 1.083, -1.2,
  • 1.3,
  • 1.41

};

Arrays

An array is a sequence of values, all of the same type, bundled into a single master value.

float[] ts2 = {

  • 21.08933, -21.814, -22.542, -22.01667,
  • 20.912, -21.564 /* 86 more numbers... */
  • 27.48999, -27.43200, -27.88466, -28.09467

};

16

slide-17
SLIDE 17

float[] ts1 = {

  • 4.8,
  • 4.79, -4.764, -4.762,
  • 4.764, -4.824, /* 86 more numbers... */
  • 1.083, -1.2,
  • 1.3,
  • 1.41

}; for( int idx = 0; idx < ts1.length; ++idx ) { if( ts1[idx] > 0.0 ) { println( "Where's my sunscreen?" ); } }

17

slide-18
SLIDE 18

Classes and objects

  • This is a review from CS105
  • This material is in L01 due Wednesday this

week (Jan 9)

18

slide-19
SLIDE 19

Objects

  • LP Chapter 8
  • object-oriented thinking
  • declaring and creating
  • assigning object "fields"
  • calling object "methods“
  • Slides created by Professor Dan Vogal.

19

slide-20
SLIDE 20

The Properties and Actions of Something

Dog

20

slide-21
SLIDE 21

Dog Properties

  • Breed
  • Weight
  • Gender
  • Colour

Dog Actions

  • Sleep
  • Eat
  • Run
  • Jump
  • Fetch

these are like variables these are like functions

21

slide-22
SLIDE 22

Class vs. Object

A type of animal called "dog" A 2 year-old German Shepherd named Rex

22

slide-23
SLIDE 23

Class vs. Object

23

slide-24
SLIDE 24

Car Class

Properties (variables) Actions (functions)

24

slide-25
SLIDE 25

car (non object)

// car properties (variables) float carX; float carY; float carHue; float carSpeed; // car actions (functions) void carUpdate() { … void carDraw(float x, float y, float hue) { …

25

slide-26
SLIDE 26

race (defining car class)

class Car { // car properties (fields) float x; float y; float hue; float speed; // car actions (methods) void move() { … } void draw() { … } }

coding

26

slide-27
SLIDE 27

race (using car class)

Car car = new Car(); void setup() { size(600, 100); colorMode(HSB, 360, 100, 100, 100); car.x = width; car.y = 60; car.speed = 2; car.hue = 100; } void draw() { background(360); car.move(); car.draw(); }

27

slide-28
SLIDE 28

Declaring an Object

Car sedan;

int a;

Car class

  • bject type

int variable type variable name

  • bject name

Variable

(for comparison)

Object declare a Car object called sedan

28

slide-29
SLIDE 29

Declare and Create an Object

  • new is an operator that means “create”
  • we need to “create” the object before using it
  • “normal” variables have a single value, no need to create
  • creating the object also initializes the object

Car sedan = new Car();

create an

  • bject of

class Car declare an

  • bject of

class Car

29

slide-30
SLIDE 30

How to declare and create a Circle object called 'c'?

class Circle { float x; float y; float radius; void display() { ellipse(x, y, radius * 2, radius * 2); } }

  • A. Circle c;
  • B. Circle = new Circle(c);
  • C. Circle c = new Circle;
  • D. Circle c = new Circle();
  • E. c = new Circle();

30

slide-31
SLIDE 31

How to assign a value to the Circle 'c' radius?

class Circle { float x; float y; float radius; void display() { ellipse(x, y, radius * 2, radius * 2); } }

  • A. c.radius = 5;
  • B. Circle.radius = 5;
  • C. radius = 5;
  • D. c.radius(5);

31

slide-32
SLIDE 32

race (using constructor)

Car(float xIn, float yIn, float speedIn, float hueIn) { x = xIn; y = yIn; hue = hueIn; speed = speedIn; }

In (“in”) is added to each parameter to avoid shadowing the object fields

32

slide-33
SLIDE 33

bounce (non-object)

33

// car properties (variables) float carX; float carY; float carHue; float carSpeed; // car actions (functions) void carUpdate() { … void carDraw(float x, float y, float hue) { …

slide-34
SLIDE 34

bounce1 (ball class)

class Ball { // dot position, speed, and size float size = 20; float x = 50; float y = 50; // start moving to the left float xSpeed = -1.1; // start moving more slowly upward float ySpeed = 0.6; void moveAndDisplay() { … }

34

slide-35
SLIDE 35

bounce1 (using ball class)

Ball ball = new Ball(); void draw() { background(200); ball.moveAndDisplay(); }

35

slide-36
SLIDE 36

Anatomy of a Class

class MyClass { float x; int i; MyClass() { x = width; i = 99; } void doSomething() { text(i, x, 50); } }

class name fields constructor method

36

slide-37
SLIDE 37

bounce2 (constructor)

class Ball { // dot position, speed, and size float size; float x; float y; float xSpeed; float ySpeed; Ball() { x = 50; y = 50; xSpeed = random(-2, 2); ySpeed = random(-2, 2); size = random(10, 30); } …

37

slide-38
SLIDE 38

bounce2 (three ball objects)

Ball ball1 = new Ball(); Ball ball2 = new Ball(); Ball ball3 = new Ball(); void draw() { background(200); ball1.moveAndDisplay(); ball2.moveAndDisplay(); ball3.moveAndDisplay(); }

38

slide-39
SLIDE 39

bounce3 (many ball objects with array)

Ball[] balls = new Ball[10]; void setup() { for (int i = 0; i < balls.length; i++) { balls[i] = new Ball(); } } void draw() { background(200); for (int i = 0; i < balls.length; i++) { balls[i].moveAndDisplay(); } }

39

slide-40
SLIDE 40

What is printed to the console?

class Thing { int x; Thing(int xIn) { x = xIn; } } Thing t = new Thing(2); void setup() { println(t.x); }

  • A. 0
  • B. 1
  • C. 2
  • D. 3
  • E. 4

40