CST112 Variables Page 1 1 3 4 5 6 7 8 Processing: Variables, Declarations and Types CST112 The Integer Types A whole positive or negative number with no decimal positions May include a sign, e.g. 10, 125, -37, 1584, +3 Numeric data type values may not include: Dollar signs ($), commas (,) percent symbol (%) Stored as is binary equivalent 0000 0000 0110 0001 (binary) = 97 The Floating Point Types A real positive or negative decimal number with the capability of storing decimal positions May include a sign, e.g. 1.0, 150.67, -3.97, 1584.002, +0.3 The Character Type A single character that is enclosed in single quotes (apostrophes), e.g. 'S' Each character is represented in Unicode by two bytes in memory or on disk The Unicode Table The complete Unicode specification can be found at: http://www.ssec.wisc.edu/~tomw/java/unicode.html The character 'A' is: 65 in decimal 0000 0000 0100 0001 in Unicode binary The character 'a' is: 97 in decimal 0000 0000 0110 0001 in Unicode binary The Boolean Type Variable that may store only one of two possible values True or False Useful for data that represents answer to a Yes/No question, e.g. Matriculated? Married? Variables (Page 1) A programmer-defined word used in a program to store a data value Represents the contents of an address in computer memory (RAM) where data is
CST112 Variables Page 2 stored Wherever a variable name is used in a program, it represents the contents (value) of the data stored in that memory location 15 16 17 18 19 Variables (Page 2) We already have used variables in our programs The system variables mousex and mousey store the current x- and y-coordinates of the mouse Those values are stored in RAM memory Whenever those variable names are used in a statement, the current values of the variables are substituted into the execution of that statement, e.g. ellipse(mousex, mousey, 50, 50); Declaring Variables (Page 1) Before being used in a program, all programmer-defined variables must be declared In this process: Names the variable to the program Defines the variable s type Designates RAM area for the variable s storage Declaring Variables (Page 2) type variablelist [= initialvalue]; Examples: int x; int myred, mygreen, myblue; More than one variable may be declared in the same statement double pi = 3.141592653589793; An initial value may be assigned to a variable when declared (the value later may be updated) Types (Page 1) The type (e.g. the data type) indicates the kind of data may be stored within a declared variable This is important so the computer can know how much memory to allocate for the variable s storage All the primitive types in Processing are keywords which can be used to declare the variables Reminder a keyword is a command that is part of the Processing language and has a required syntax Up until now the only type of statements we have used in Processing are functions Types (Page 2) Processing s primitive types:
CST112 Variables Page 3 char: one character stored in 16-bit Unicode representation, e.g. 'A' or 'a' byte: one byte integer between -128 and 127 short: two byte integer between -32,768 to 32,767 int: four byte integer between -2,147,483,648 to 2,147,483,647 long: eight byte integer 20 21 27 28 29 Types (Page 3) Processing s primitive types (con.): float: a floating point decimal value which occupies four bytes with 6 or 7 significant digits, e.g. 3.14159 double: a floating point decimal value which occupies eight bytes with 14 or 15 significant digits, e.g. 3.141592653589793 boolean: true or false Types (Page 4) Strings may be stored in RAM using the class String which declares an object variable (also called a reference type) String color = "red"; Keywords Part of a computer language words that have a specific meaning to the compiler Primitive type names such as int and double, along with the word void are keywords (also known as reserved words) Usage must conform to a required syntax Keywords may not be used as identifiers (programmer-defined words) Always spelled with all lowercase letters Identifiers A variable name is considered an identifier, that is a word that is defined by the programmer Consists of letters, numbers, and underscores (_) Must begin with a letter Must be one word with no blank spaces Are case sensitive like all other Processing words E.g. x, blue_value, myheight Naming Variables Variable names follow the same naming rules as all other identifiers It is best to avoid using Processing function names and keywords as variable names (or programmer-defined names in general) Some valid variable names are: Welcome m_inputfield1 C3PO
CST112 Variables Page 4 31 32 33 43 44 Variable Naming Conventions A convention (also called a standard) is an accepted method/approach used by a majority of programmers in industry If a variable name is one word, all letters are lowercase If a variable name has more than one word Begin the first word with a lowercase letter Every succeeding word has the first letter capitalized, e.g. myheight firstredline Assignment Statements (Page 1) The assignment operator (=) is used to store a value or the result of an expression into memory As stated before the memory location of that value is represented by a variable The assignment variable always must be to the left of the assignment operator (equal sign) variable = value/expression; Assignment Statements (Page 2) Examples: A single value x = 15; The result of arithmetic expression (equation) kilometers = miles * 1.614; When a variable is initially declared int myred = 255; Variable Names on Both Sides of Assignment Operator (Page 1) Examples: myred = myred + 10; x = x + 1; y = y 1; A value or the result of an expression on the right of the assignment operator is stored into the variable on the left Variable Names on Both Sides of Assignment Operator (Page 2) Examples: myred = myred + 10; Value of variable myred before execution 25 Value of variable myred after execution 35 x = x + 1; Value of variable x before execution 10
CST112 Variables Page 5 Value of variable x after execution 11 y = y 1; Value of variable y before execution 29 Value of variable y after execution 28 51 52 53 58 59 System Variables (Page 1) Additional system variables (beyond mousex and mousey) that store data that is commonly needed in Processing sketches include: width the width in pixels of the output window height the height in pixels of the output window framecount the number of frames processed framerate the current rate at which frames are processed per second System Variables (Page 2) Additional system variables (con): screen.width the width in pixels of the entire screen screen.height the height in pixels of the entire screen key the most recent key pressed on the keyboard keycode a numeric code for the most recent key pressed on the keyboard System Variables (Page 3) Additional system variables (con): keypressed a Boolean value (true or false) that indicates whether a key currently is pressed mousepressed a Boolean value (true or false) that indicates whether a mouse button currently is pressed mousebutton value that indicates which mouse button currently is pressed: Left, Right or Center Additional system variables can be found in the Processing reference Randomization (Page 1) Computers have the ability to add unpredictability to applications by introducing randomization The Processing function random() is able to generate and return random values of type float within a specified range Those values assigned to variables can be used to randomize the colors, positions, sizes and other elements of the functions we already have learned Randomization (Page 2) Format 1: random(start, end) start is the lowest value that could be returned end is the highest value that could be returned or any other value in between could be returned
CST112 Variables Page 6 Example 1: float center; center = random(1, 200); ellipse(center, center, 50, 50); A value between 1 and 200 is assigned to center 60 Randomization (Page 3) Format 2: random(end) When no start argument is given, zero (0) is the lowest value that could be returned end is the highest value that could be returned or any other value in between could be returned Example 2: float center; center = random(200); ellipse(center, center, 50, 50); A value between zero (0) and 200 is assigned to center 63 64 65 Casting (Page 1) Because the function random() generates a value of type float, its return value cannot be assigned to a variable of type int For example the following statement: int center; center = random(200); at run-time displays in the message window the error cannot convert from float to int Casting (Page 2) Since many of the functions used in Processing require integer arguments, it frequently would be preferable to store random values as int s This commonly is achieved by a process known as casting a technique in which a value of one type temporarily is converted into another type Casting (Page 3) (type) expression (type) is the type to which the result or return value of the expression is converted expression is the result or return value that is being converted (expression type is not actually changed) int center; center = (int) random(200);
CST112 Variables Page 7 Converts the generated random number to type int before it is assigned to the int variable center 82 83 84 85 86 Images Computer images are files that contain pictures which are nothing more than data that organize pixels onto the monitor (screen) There are common image file formats accepted by Processing and recognized by their file extensions:.gif,.jpg,.png,.tga Processing has the capability to: Display an image as the output window background Manipulate smaller images in the output window The PImage Class (Page 1) PImage is a (data) type in some ways just like int and float However PImage is a class type like String, not a primitive type like an int Declaring a PImage variable creates an object variable (also called a reference type) with the ability to store and display an image As an object variable, it is given certain capabilities, unlike primitive variables The PImage Class (Page 2) Format to declare a PImage object variable is similar to that of a primitive like int: PImage pimagevariable; PImage is the type PImage underwater; The loadimage() Method The loadimage() method stores the image in the PImage variable and creates the object pimagevariable = loadimage("path/filename"); The path/filename goes in quotation marks underwater = loadimage("underwater.jpg"); The preceding is valid if the file underwater.jpg is in the same folder as the.pde file The background() Function Revisited The background of the output window can be set to an image instead of a colored background Image file must have already been loaded into the PImage variable, e.g. a loadimage() function call
CST112 Variables Page 8 background(pimagevariable); background(underwater); The size of the output window and that of the image file must be exactly the same in pixels 89 90 91 The image() Function (Page 1) A smaller image file may be manipulated within the output window Its position as well as size (image can be resized) can be set within the image() function call Image file must have already been loaded into the PImage variable, e.g. a loadimage() function call The image() Function (Page 2) image(pimagevariable, xposition, yposition [, width, height] ); xposition and yposition are the coordinates within the output window where the image is drawn; default is upper-left corner width and height are optional image resizing values image(fish, 230, 170); The imagemode() Function These modes are similar to ellipsemode() and rectmode() which is logical since a PImage object really is a shape with an image inside it: imagemode(corner); image arguments specify upper-left corner, width, and height (the default) imagemode(center); image arguments specify the centerpoint, width, and height imagemode(corners); image arguments specify upper-left and lower-right corners