What is a variable? a named location in the computer s memory. mousex mousey. width height. fontcolor. username

Similar documents
Chapter 5. Condi.onals

What can we do with Processing? Let s check. Natural Language and Dialogue Systems Lab Guest Image. Remember how colors work.

Bits and Bytes. How do computers compute?

What is a variable? A named locajon in the computer s memory. A variable stores values

Final Exam Winter 2013

Name CMPS 5J Final March 17, 2009 This is a closed notes, closed book exam.

Name CMPS 5J Final March 17, 2010 This is a closed notes, closed book exam. Each problem is worth 1 point unless indicated otherwise.

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Chapter 8. Classes and Objects

Processing Assignment Write- Ups

5.1. Examples: Going beyond Sequence

A B C D CS105 03a Interaction

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Watch the following for more announcements

We will start our journey into Processing with creating static images using commands available in Processing:

CS 101 Functions. Lecture 15

Functions. Functions. nofill(); point(20, 30); float angle = map(i, 0, 10, -2, 2); parameters return values

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Basic Computer Programming (Processing)

Interaction Design A.A. 2017/2018

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

Kimberly Nguyen Professor Oliehoek Introduction to Programming 8 September 2013

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Chapter 12: Functions Returning Booleans and Collision Detection

Recall that creating or declaring a variable can be done as follows:

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

Basic Input and Output

1 Getting started with Processing

Art, Randomness, and Functions

Introduction to Processing. Sally Kong

Interaction Design A.A. 2017/2018

Conditional Events. Mouse events and Operators. Dr. Siobhán Drohan Mairead Meagher. Produced by:

(Inter)Ac*ve Scripts. Sta*c Program Structure 1/26/15. Crea+ve Coding & Genera+ve Art in Processing 2 Ira Greenberg, Dianna Xu, Deepak Kumar

[ the academy_of_code] Senior Beginners

Interaction Design A.A. 2017/2018

Variables One More (but not the last) Time with feeling

CISC 1600 Lecture 3.1 Introduction to Processing

Exploring Processing

Computer Graphics. Interaction

CISC 1600, Lab 3.1: Processing

CISC 1600, Lab 2.3: Processing animation, objects, and arrays

Variables. location where in memory is the information stored type what sort of information is stored in that memory

The way I feel about music is that there is no right and wrong. Only true and false. Fiona Apple. true false false

CISC 1600, Lab 2.1: Processing

If the ball goes off either the right or left edge, turn the ball around. If x is greater than width or if x is less than zero, reverse speed.

Design Programming DECO2011

Kimberly Nguyen Professor Oliehoek Assignment 3. 1 A // Declare frequented variables int h = 20; void setup() { size(400, 200); smooth(); }

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut

The Junior Woodchuck Manuel of Processing Programming for Android Devices

EP486 Microcontroller Applications

Variables and Control Structures. CS 110 Eric Eaton

Basic Input and Output

Basic Input and Output

Basic Input and Output

Iteration in Programming

Lesson Two. Everything You Need to Know. 4 Variables 5 Conditionals 6 Loops

CST112 Variables Page 1

CISC 1600, Lab 2.2: Interactivity in Processing

Class #1. introduction, functions, variables, conditionals

if / if else statements

Module 01 Processing Recap. CS 106 Winter 2018

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

In this chapter: What is an array? Declaring an array. Initialization. Array operations using the for loop with an array. Arrays of objects.

Repetition is the reality and the seriousness of life. Soren Kierkegaard

Module 05 User Interfaces. CS 106 Winter 2018

1 Getting started with Processing

ACTIVE PROCESSING Summary Learning Objectives How to Proceed Check your Understanding Learning Objectives 412

CISC 1600, Lab 3.2: Interactivity in Processing

Evaluating Logical Expressions

1. Complete these exercises to practice creating user functions in small sketches.

INTRODUCTION TO PROCESSING. Alark Joshi, Amit Jain, Jyh-haw Yeh and Tim Andersen

Introduction to Processing

Solution Notes. COMP 151: Terms Test

Midterms Save the Dates!

Processing/Java Syntax. Classes, objects, arrays, and ArrayLists

+ Questions about Assignment 5?

Practice Written Examination, Fall 2016 Roger B. Dannenberg, instructor

CS 106 Winter Lab 03: Input and Output

CSE120 Wi18 Final Review

Module 01 Processing Recap

The Processing language

University of Cincinnati. P5.JS: Getting Started. p5.js

CST112 Looping Statements Page 1

2D Shapes. Creative Coding & Generative Art in Processing 2 Ira Greenberg, Dianna Xu, Deepak Kumar

CS110 Introduction to Computing Fall 2016 Practice Exam 1

All program statements you write should be syntactically correct. Partial credit is not guaranteed with incorrect use of syntax.

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

Program Fundamentals

Question 1 (10 points) Write the correct answer in each of the following: a) Write a Processing command to create a canvas of 400x300 pixels:

CS110 Introduction to Computing Fall 2016 Practice Exam 1 -- Solutions

Computational Expression

Using Methods. Writing your own methods. Dr. Siobhán Drohan Mairead Meagher. Produced by: Department of Computing and Mathematics

Appendix: Common Errors

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Write a program that draws this image. The ligle squares are 5x5. The screen is the default 100x100. It doesn t change.

CMPT-166: Sample Midterm

Arrays. Array an ordered collec3on of similar items. An array can be thought of as a type of container.

Methods (cont.) Chapter 7

Transcription:

What is a variable? a named location in the computer s memory mousex mousey width height fontcolor username

Variables store/remember values can be changed must be declared to store a particular kind of value (e.g. whole number, fraction, character, color, image, boolean) should have a descriptive name start with letter then also include numbers no spaces

void setup() { size(500,500); int diameter = 0; void draw() { ellipse(width/2, height/2, diameter,diameter); diameter = diameter + 5; What does this display? A) many nested circles (like a target) growing in size B) a white circle growing in size C) a black circle growing in size D) a pulsing images of circles growing and shrinking E) nothing (a circle with diameter 0)

primitive types (Java) boolean - true or false char - a, b, c, byte - small integer -128 to 127 short - bigger integer -32768 to 32767 int - even bigger integer +/- 2 billion long - really big integer float - numbers with fractional parts 3.1415 double - like float but more precision

declaring vs initializing vs assigning int carfront; // declare int carfront = 100; // declare and initialize carfront = carfront - 1; // assign

void setup() { size(200, 200); void draw() { background(255); int xpos = 0; ellipse(xpos, height/2, 20, 20); xpos = xpos + 1; What does this draw? A) circle moving across the screen left to right B) circle moving across the screen right to left C) circle moving across the screen top to bottom D) circle moving across the screen bottom to top E) half-circle on the left edge not moving

System Variables (Processing) mousex, mousey pmousex, pmousey width, height framecount

void setup() { size(400, 400); void draw() { fill(255-abs(mousex-pmousex)); rect(pmousex, pmousey, mousex, mousey);

void setup() { size(400, 400); // position A // position B void draw() { // position C fill(255-abs(mousex-pmousex)); rect(pmousex, pmousey, mousex, mousey); // position D Where should the line background(255); be placed so that the sketch shows just a single moving rectangle? Choose option E if it would work with either C or D.

Making Choices If you wish to defrost, press the defrost button; otherwise press the full power button. Let the dough rise in a warm place until it has doubled in size. If the ball reaches the side of the display change it s direction.

Boolean Expressions Any expression that evaluates to true or false. Relational operators, <, <=, >, >=. Equality operators, ==,!=. For example: int i = 3, j = 4; 5 < 6 i == j (j + 2) <= 6

Expressions and Statements Expression statements are formed by adding a semicolon to the end of certain types of expressions. An assignment expression is an expression involving an assignment. area = width * height; A method call expression has no assignment. rect( );

Non-statements Not all expressions can be turned into statements. The following are syntax errors. x+y; width > 20; The above do not make sense as statements. They don t DO anything. Statements must DO something. assign a new value to a variable cause some output to occur (println(), rect()) change some internal state (background (255), nostroke())

Blocks Several statements can be grouped into a block using {. { int x = 20, y = 30, size = 40; ellipsemode(corner); fill(255, 0, 0); rect(x, y, size, size); fill(0, 255, 0); ellipse(x, y, size, size); // x, y, and size above cannot be used // here

The if statement if ( BooleanExpression ) Statement true BooleanExpr Statement false

int count = 0; void setup() { framerate(2); void draw() { background(120); int x = 20, y = 30, size = 40; if (count % 2 == 0) { ellipsemode(corner); fill(255, 0, 0); rect(x, y, size, size); fill(0, 255, 0); ellipse(x, y, size, size); count = count + 1;

int ballx, balldia = 50; void setup() { size(400,400); ballx = -balldia/2; void draw() { background(120); if (ballx > width+balldia/2) ballx = -balldia/2; ellipse(ballx, height/2, balldia, balldia); ballx = ballx + 1; A. Ball moves across jumping back to left edge as soon as it touches the right edge. B. Ball moves across moving off the right edge then moves back in from the left edge. C. Ball moves across until half way off the right edge (showing just a half circle) then reappears as a half circle on the left edge. D. Ball moves across then disappears and doesn t come back. E. Ball moves left to right then right to left after reaching the right edge, and repeats.

The if-else statement if ( BooleanExpression ) Statement1 else Statement2 true BooleanExpr false Statement1 Statement2

void draw() { background(120); int x = 20, y = 30, size = 40; ellipsemode(corner); if (count % 2 == 0) { fill(255, 0, 0); rect(x, y, size, size); fill(0, 255, 0); ellipse(x, y, size, size); else { fill(0, 255, 0); rect(x, y, size, size); fill(255, 0, 0); ellipse(x, y, size, size); count = count + 1;

int ballx, balldia = 50, speed = 1; void setup() { size(400,400); ballx = balldia/2; void draw() { background(120); if (ballx > width-balldia/2) { speed = -1; else { speed = 1; ellipse(ballx, height/2, balldia, balldia); ballx = ballx + speed; A. Leave it blanks (as is). B. if (ballx > balldia/2) C. if (ballx < balldia/2) D. (ballx > balldia/2) E. (ballx < balldia/2) What goes in the blank so that the ball moves back and forth, reversing direction whenever it reaches the edge?

Semicolons and the if statement if (carx < 0) carx = width; // draw car... if (carx < 0) { carx = width; cary = cary + 10;// move down each time it wraps around

Semicolons and common error void draw() { if (carx < 0); carx = width; // draw car...

Logical Operators Operators that take boolean values as operands. x && y - true if x AND y are both true x y - true if either x OR y are true, or both!x - true if x is false - read NOT x

void setup() { size(200, 200); rectmode(corners); int boxleft = 50, boxright = 150, boxtop = 50, boxbottom = 150; void draw() { if (mousex > boxleft && mousex < boxright && { else { mousey > boxtop && mousey < boxbottom ) fill(255,0,0); fill(0,255,0); rect(boxleft, boxtop, boxright, boxbottom);

int circlex, circley, dia = 40; void setup() { circlex = width/2; circley = height/2; void draw() { if ( ) fill(255,0,0); else fill(0,255,0); ellipse(circlex, circley, dia, dia); Which expression completes this program so that it shows a red circle when the mouse is inside of the circle and a green circle when the mouse is outside of the circle? A. dist(mousex, mousey, circlex, circley) <= dia/2 B. dist(mousex, mousey, circlex, circley) >= dia/2 C. dist(mousex, mousey, circlex, circley) < dia D. dist(mousex, mousey, circlex, circley) > dia E. abs(mousex-circlex) < dia/2 && abs(mousey-circley) < dia/2

Bouncing Ball pos t+1 = pos t + velocity velocity t+1 = velocity t + acceleration gravity provides constant acceleration downward

float velocity = 2; float ypos = 0; int ballradius = 10; void draw() { background(255); // if hit the ground reverse the velocity if (ypos > height-ballradius) { velocity = -velocity; // adjust position based on velocity ypos = ypos + velocity; // draw the ball ellipse(width/2, ypos, ballradius*2, ballradius*2);

float velocity = 2, ypos = 0; int ballradius = 10; float gravity = 0.1; void draw() { background(255); // if hit the ground reverse the velocity if (ypos > height-ballradius) { velocity = -velocity; // adjust position based on velocity ypos = ypos + velocity; // adjust the velocity - increasing due to gravity velocity = velocity + gravity; // draw the ball ellipse(width/2, ypos, ballradius*2, ballradius*2);

void draw() { background(255); // if hit the ground reverse the velocity if (ypos > height-ballradius) { velocity = -velocity; // adjust position based on velocity ypos = ypos + velocity; // adjust the velocity - increasing due to gravity velocity = velocity + gravity; // add some drag velocity = velocity*0.99; // draw the ball ellipse(width/2, ypos, ballradius*2, ballradius*2);

Recap boolean valued expressions using relational operators: <, >, <=, >=, ==,!= boolean operators &&,,! if (booleanexpression) { sequence of statements else { sequence of statements