Lecture 3 - Variables: Asking Computer To
Lecture 3 - Variables: Asking Computer To
What is a variable?
2
What is a variable?
3
What is a variable?
4
What is a variable?
5
Data type
6
Data type
7
Properties of variables
1. A variable should have a value
Example 1:
two rectangles are drawn in two
different ways
8
Properties of variables
1. A variable should have a value
First rectangle:
9
Properties of variables
1. A variable should have a value
Second rectangle:
rect ( 20 , 150, a, a );
Draw Topleft
width height
rectangle coordinates
10
Properties of variables
1. A variable should have a value
Second rectangle:
rect ( 20 , 150, a, a );
Draw Topleft
width height
rectangle coordinates
11
Properties of variables
2. The value of a variable can be changed
Example 2:
the second and third assignments
change the value of the variable
12
Properties of variables
2. The value of a variable can be changed
Example 2:
the second assignment changes
the value of the variable
13
Properties of variables
3. We can have more than one variable
Example 3:
two rectangles with different
width and height are drawn
14
Properties of variables
4. Value of one variable can be calculated from others
Example 4:
the width and height of the third
rectangle is calculated by that
of the previous two rectangles
15
What youll learn today?
What is a variable?
16
4 steps to create and use a variable
1. Variable Declaration
a. Tell the computer what
type of data you want to store
asking the computer for some
space to store the values
b. Give names to refer to the
storage space
17
1. Variable Declaration
18
1. Variable Declaration
How to name a
variable?
Ann
Any restrictions on
the names?
19
1. Variable Declaration
How much space to ask for?
You: Can you give me a bag to hold something?
Me: What type of things you want to hold?
I will give you a bag of suitable type
20
1. Variable Declaration
Data Types
You: Can you give me some space to store some data?
Computer: What type of data you want to store?
I will give you space of suitable type
21
1. Variable Declaration
Data Types
Data Type Description Range / example Size
boolean true / false 1 bit
char A character a, B, c, 1 byte
byte A small number -128 to 127 1 byte
short A large number -32768 to 32767 2 bytes
-2147483648 to
int An integer 4 bytes
2147483647
long A really huge number
float A decimal number e.g. 3.14159 4 bytes
A decimal number
double with a lot more 8 bytes
decimal places
23
How to name a variable?
Test your understanding
Can these be variable names? If not, why?
score
aVariableName
my name
4thRectangle
temp2
money^
color_R
setup
abc
RectWidth
24
1. Variable Declaration
What if you forget this step?
Forget to declare Variable declaration is to tell the
the variables computer what type of data you
want to hold asking the computer
for some space to hold some
values / information
25
4 steps to create and use a variable
2. Variable Initialization
26
2. Variable Initialization
27
2. Variable Initialization
What if you forget this step?
If we forget to assign an initial
value to a variable
Forget to give
initial values to
the variables
28
4 steps to create and use a variable
29
4 steps to create and use a variable
30
Assigning value to a variable
value expression
another
variable
32
Arithmetic Operators
Example
Value of
Operators Meaning
c
+ Add c = a + b; 21
- Subtract c = a - b; 13
* Multiply c = a * b; 68
Divide
/ c = a / b; 4
(get the quotient in division)
Modulus
% (get the remainder c = a % b; 1
in division)
33
What youll learn today?
What is a variable?
34
LETS MAKE THE ROBOT MOVE
Using Variables
35
How do you want the robot to move?
36
Lets try making the robot move
Modify the program
as follow
Things your write
after // are remarks
1. Ask the computer or comments
to remember the
robots position
as x and y
2. Specify where is
the robot initially
inside setup()
37
How does the program work?
x y
38
How does the program work?
x 300 y 500
39
How does the program work?
x 300 y 500
40
How does the program work?
x 300 y 500 - 1
500
499
41
How does the program work?
x 300 y 499
42
How does the program work?
x 300 y 499 - 1
499
498
43
How does the program work?
x 300 y 498
44
Lets try making the robot move
from different initial positions
45
Lets try making the robot move
in different directions
46
Lets try other initial positions and directions
47
What youll learn today?
What is a variable?
48
Recap: moving the robot with mouse
49
Mouse & Keyboard Control
Put different types of instructions to
different sections / functions
mousePressed() handles mouse clicks
keyPressed() handles key presses
Be careful of the
UPPER / lower
case letters
50
Lets try drawing more robots
when mouse is pressed
Modify the source program
robotFollowMouse.pde
Add the
mousePressed()
function and display
the robot here
51
Lets try drawing more robots
when mouse is pressed
52
Lets try clearing the robot
when the keyboard is pressed
At the end of the program,
add the keyPressed() function and
display the background image there
53
How mousePressed() and keyPressed() work?
54
Class Exercise
55
Challenge & Bonus
Display the robot with a fixed size when the program starts
Make the robot bigger every time user presses the keyboard
56
Supplementary:
Print out the value of a variable
We can use println to print out some text and/or
the value of a variable in the text area
Example
Output
57
Supplementary:
Print out the value of a variable
We can use println to print out some text and/or
the value of a variable in the text area
Example
Output
58
Summary
We can use variables to
remember some values
e.g. position of the robot
Variable data types
int (integer)
float (floating point number)
char (character)
String (some characters)
PImage (image)
Be careful of the
UPPER / lower
case letters
60
References