5. variables
TRANSCRIPT
From last time…• Libraries, Functions, Active Mode, Interaction!
• Check the site for a list of inspiration sources!
• We have a Teaching Assistant:!
! Yifan Wang, [email protected]!
! Office Hours: CSE309, 2:00pm–3:00pm
CAP 3032
Variables
What is a variable?• An address in memory: 0x0000883A!
• Variables have a type and a name! int x; int y; float weight; string introduction;
Memory
int x;
The Primitivesboolean!! ! true or false!char! ! ! ! ‘a’ ‘b’ ‘c’!byte! ! ! ! -128 to 127 short! ! ! -32,768 to 32,767!int ! ! ! -2147483648 to 2147483647!long! ! ! ! larger numbers!!float! ! ! 3.14159!double! ! ! float with more decimal places
Common Primitives
boolean!! ! true or false!int ! ! ! -2147483648 to 2147483647!float! ! ! 3.14159
Using Variables
1. Declare the variable! int xposition; // type name;
2. Initialize the variable! xposition = 10; // name = value;
Using Variables
1 & 2. Declare & Initialize the variable! int xposition = 10; // type name = value;
Demo Variables
System VariablesmouseX, mouseY
pmouseX, pmouseY
width, height // sketch dimensions
frameCount, frameRate
displayWidth, displayHeight // screen dimensions
key, keyCode, keyPressed
mousePressed // is it pressed?
mouseButton // left/center/right
Demo!System Variables
random() & castingrandom(j) // from 0 to j
random(j,k) // from j to k
float r = random(24);
int r = random(24); // needs to be cast!
int r = int(random(24)); // int() method
int r = (int) random(24); // Java cast
For next time…• Monday is Martin Luther King, Jr. Day!
• Read Shiffman, p. 59–70 (Conditionals I)!
• Project Proposals due on Wed, January 22
Quiz• Closed book, 25 minutes, good luck!