Presentation is loading. Please wait.

Presentation is loading. Please wait.

Variables and Operators

Similar presentations


Presentation on theme: "Variables and Operators"— Presentation transcript:

1 Variables and Operators

2 What is a Variable? Variables allow a program to store data at one point and refer back to it later A 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 definition For example: Characters Strings Integers Colors Any Java class

4 Creating a Variable Before a variable can be used in a program it must be declared Variables are declared by first stating the type of the data to be stored, followed by the variable’s name int 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 memory Each 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 Types We can store different types of data In Java, variables can hold primitive data types or references to objects Primitive data types include types for true/false data, characters, whole numbers and real numbers For 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 numbers byte (-128 to 127) short ( to 32767) int ( to ) long ( to ) Floating values -- decimals float ( 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 used The 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

9 Weakly Typed Languages
JavaScript and ActionScript are weakly typed languages, they do not require variables have their data type defined in advance The advantage of weakly typed languages is that we can program quickly without having to worry about declaring variables first

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 space Variable 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 word e.g. width, rect4, fillColour, isFilled Variables that refer to constant data typically use all uppercase letters and underscores between each word e.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 initialize int y1; // assign the value 5 to the variable y1 y1 = 5; A variable can be initialized at the time it is declared // declare an int named y1 initialized to 5 int 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 to double d1 = ; // declare a double named d2 initialized to d1 double d2 = d1; // declare a float name z initialized to x * y + 15 float 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 1 Compound Assignment Operators = -= *= /= %=

15 Integer Operations +, -, *, /, and % 5 / 2 yields an integer 2.
5.0 / 2 yields a double value 2.5 5 % 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 = 60 Perform multiplication first: 3*6 =18  7+18 = 25 Which option is correct?

17 Operator Precedence Operator precedence represent rules for evaluating mathematical expressions. Every programming language has similar rules.

18 Built-In Variables Processing 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 applications colorMode(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 variable that 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 changes size(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 default rect(xPos, 0, boxSize, height) ; xPos=boxSize*1 ; xPos=boxSize*2 ;

23 In-class Lab Create 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.


Download ppt "Variables and Operators"

Similar presentations


Ads by Google