2 What is a Variable?Variables allow a program to store data at one point and refer back to it laterA variable is container for data with an associated type and name, or identifier
3 Type For example: Characters Strings Integers Colors Any Java class “A number of … things having in common … characteristics that distinguish them as a … class.”Free Dictionary definitionFor example:CharactersStringsIntegersColorsAny Java class
4 Creating a VariableBefore a variable can be used in a program it must be declaredVariables are declared by first stating the type of the data to be stored, followed by the variable’s nameint count;This declares a variable named “count” that stores an integer (a whole number)
5 Variables and Computer Memory All data that a program uses must be stored somewhere in the computer’s memoryEach piece of data is stored in a specific location, referred to as the “address”A variable is a name assigned to the address that contains the data
6 Data TypesWe can store different types of dataIn Java, variables can hold primitive data types or references to objectsPrimitive data types include types for true/false data, characters, whole numbers and real numbersFor now, we’re just going to look at primitive data types, we’ll look at objects later
7 Primitive Data Types boolean — truth values — true or false char — characters — ’a’, ’G’, ’#’, ’3’Integer values – whole numbersbyte (-128 to 127)short ( to 32767)int ( to )long ( to )Floating values -- decimalsfloat ( e-45 to e+38)double ( e-324 to e+308)
8 Strongly-Typed Languages Java is a strongly typed language, we have to define what kind of data we want to hold in a variable before it can be usedThe advantage of a strongly typed language is that we can get the compiler to check that we are using a variable correctly before we run the program
10 Naming Variables There are some restrictions on variable names: Variable names can only include a limited range of characters: a-z, A-Z, 0-9, _Variable names cannot contain spaces, the underscore character “_” is often used instead of a spaceVariable names must not start with a number
11 Variable Naming Conventions In Java programs, variable names typically begin with a lowercase letter and use a capital letter to start each new worde.g. width, rect4, fillColour, isFilledVariables that refer to constant data typically use all uppercase letters and underscores between each worde.g. PI, CENTER, MAX_VALUE
12 Initializing Variables A variable can be declared with no value and then assigned a value later// declare an int named y1 but do not initializeint y1;// assign the value 5 to the variable y1y1 = 5;A variable can be initialized at the time it is declared// declare an int named y1 initialized to 5int y1 = 5;
13 Initializing Variables A variable can be initialized with a value, the value of another variable,or by evaluating an expression// declare a char named letter initialized to ‘a’char letter = ‘a’;// declare a double named d1 initialized todouble d1 = ;// declare a double named d2 initialized to d1double d2 = d1;// declare a float name z initialized to x * y + 15float z = x*y f;
14 Arithmetic Operators = assignment operator x=9 + addition 3 + 4 - subtraction* multiplication 5 * 5/ division / 7% modulo % 7++ Increment operator; increments a value by 1-- Decrement operator; decrements a value by 1Compound Assignment Operators = -= *= /= %=
15 Integer Operations +, -, *, /, and % 5 / 2 yields an integer 2. 5.0 / 2 yields a double value 2.55 % 2 yields 1 (the remainder of the division)
16 Operator Precedence Here’s another problem. What’s the answer to this? x = * 6;Two Options (depending on the order of operations):Perform addition first:7 + 3 = 10 10 * 6 = 60Perform multiplication first:3*6 =18 7+18 = 25Which option is correct?
17 Operator PrecedenceOperator precedence represent rules for evaluating mathematical expressions.Every programming language has similar rules.
18 Built-In VariablesProcessing has some built-in variables that are quite useful. Since they are built-in, these variables should not be declared, initialized or assigned; they should just be read.width / height :: The dimensions of the window.mouseX / mouseY:: The current coordinates of the mouse.frameCount :: The number of frames that have been drawn since the program started.
19 Color Models colorMode(RGB, 255); // processing’s default color model // used almost exclusively in// computer science applicationscolorMode(HSB, 360, 100, 100);// hue, saturation, value(brightness)// used predominately in art, available// in most graphics and animation packages
20 Examples // 1 colorful rectangle centered in the canvas size(300,300); // colorMode(HSB,255,255,255);background(20,20,255);fill(20,255,20);stroke(255,20,20);rectMode(CENTER);/*CENTER is a built-in variablethat can’t be changed—a constant*/rect(width/2,height/2,50,50);
21 More Examples // 5 small rectangles across // the top of the canvas size(300,300);int xIncrement=width/7;rect(xIncrement, 10, 20, 20);rect(xIncrement*2, 10, 20, 20);rect(xIncrement*3, 10, 20, 20);rect(xIncrement*4, 10, 20, 20);rect(xIncrement*5, 10, 20, 20);
22 More Examples// the mystery program that changes the display as the size changessize(950,800);int boxSize = width/3 ;int xPos ;xPos=boxSize*0 ;fill(xPos*17%255, xPos*11%255, xPos*4%255);// note we are in rectMode(CORNER) by defaultrect(xPos, 0, boxSize, height) ;xPos=boxSize*1 ;xPos=boxSize*2 ;
23 In-class LabCreate a pattern of six objects of the same shape and color (such as lines or rectangles).Revise your code to set the positions of the shapes with functions of the width and height variables.Test your code with several different window sizes to make sure the pattern remains consistent.Keeping a monochromatic color palette using the HSB color model, modify your code so that each of your shapes is a different brightness while the hue and saturation remain constant.Use a variable to set hue and saturation and test with a several values for each.