Lab 1 Variables
Lab 1 Variables
Support : See top of page for contact details : support tutor, unit tutors
Learning Objectives
Familiarity with Processing Environment
o Writing and saving a program
o Running a program
o Correcting syntax errors
Java syntax
Simple problem decomposition – break problem into smaller chunks
Program flow (order of commands)
Using Variables and expressions
Program sequence
Resources:
open these and have them available for reference
size(300,300);
Pair programming involves one person writing the code, while the other makes suggestions. The first
coder should get the boxes in the correct places, the 2nd will then take over to get the circles in the
correct places. Draw a box , run the code to check, draw another box and check (repeat)
Your code should be clearly commented to show what each block of commands is drawing.
Exercise 2 : Variables
Exercise 2. Enter the code below precisely (you can copy and paste). Press play
point(0,0);
ellipse(x,250,40,40);
You should have made a ‘point’ (spot) at the origin corner (top left) and a white circle at position 50,250
with width and height of 40 pixels.
1. Make the circle appear in the middle of the screen (alter the value of variable x). Make the
circle appear in the bottom right corner of the screen (we should only see ¼ of it)
2. Make the circle double its original size, then ½ its original size
3. Introduce a new variable
float size = 50;
4. Use the size variable inside the ellipse command, so that, you can more easily alter the size of
the circle, only changing one value in the program (rather than 2). Try this out.
Question : how does the compiler understand the difference between the size variable and the size
command in our program?
Answer : commands are followed by brackets “( )”. These brackets tell the compiler that we are dealing
with a command. We couldn’t use brackets or other symbols in a variable name – actually they should
start with a letter, they are case sensitive, they can contain numbers but not spaces. The usual
convention is camelCase, where each new word start with a Capital letter e.g.
Draw a square of edge 50 pixels on the screen using only the line command, go clockwise. Line takes 4
parameters (2 pairs) as follows
If we want to move the square… introduce a variable x to allow the square to be moved left or right on
the canvas
float x = 20; //x can store floating points or integer numbers, x becomes equal to 20
Finally add a variable called edge that allows you to vary the size of the square and try different values
for edge. Alter x and y as well to ensure your square scales and moves dependent on the variable values.
Exercise 4. Drawing a Stick Person of variable size
Using the 3 variables x,y,size and only the line and ellipse drawing commands, you must draw a stick
person similar to the diagram below (with the same relative sizes and positions of each part) where x,y is
the centre of the head and the value of size the length of the torso (centre of head down). Other sizes
can be seen by counting the number of graph squares relative to size. You SHOULD add extra variables
as required to store suitable starting points for the arms, legs etc.
Your code should be clearly commented to show what each block of commands is drawing.
Experiment with the values of size, x and y to draw different sized people in different places on the
screen (remember that processing will allow you to draw off the screen, so if nothing appears alter x and
y).
size
size