Variables%20and%20Operators - PowerPoint PPT Presentation

About This Presentation
Title:

Variables%20and%20Operators

Description:

Computers can only process one piece of ... different types of data ... with several different window sizes to make sure the pattern remains consistent. ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 25
Provided by: rebecc2
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Variables%20and%20Operators


1
Variables and Operators
  • August 24, 2009

2
  • What is a Variable?
  • Computers can only process one piece of
    information at a time
  • 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 name, or identifier

3
  • 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
    variables name
  • int count
  • This declares a variable named count that
    stores an integer (a whole number)

4
  • Variables and Computer Memory
  • All data that a program uses must be stored
    somewhere in the computers 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 data used by a program

5
  • Data Types
  • Variables can hold different types of data
  • In Java, variables can hold primitive data types
    or references to arrays / objects
  • Primitive data types include types for true/false
    data, characters, whole numbers and real numbers
  • For now, were just going to look at primitive
    data types, well look at arrays and objects later

6
  • Primitive Data Types
  • boolean truth values true or false
  • char text characters a-z, A-Z, 0-9, etc.
  • byte very small whole numbers -128 to 127
  • short small whole numbers -32768 to 32767
  • int whole numbers -2147483648 to 2147483647
  • long big whole numbers -263 to 263-1
  • float (small) real numbers 1.4e-45 to 3.4e38
  • double real numbers 4.9e-324 to 1.8e308

7
  • 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 computer to check that we are
    using a variable correctly before we run the
    program

8
  • 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

9
  • 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,
    although they can contain numbers in the middle
    and at the end

10
  • 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

11
  • Initialising 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

12
  • 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
    132.32
  • double d1 132.32
  • // declare a double named d2 initialized to d1
  • double d2 d1
  • // declare a float name z initialized to x y
    15
  • float z xy 15.0f

13
Arithmetic Operators
assignment operator x9 addition 3 4
- subtraction 5 - 7 multiplication 5 5
/ division 14 / 7 modulo 20 7
Increment operator increments a value by 1
-- Decrement operator decrements a value by 1
Compound Assignment Operators -
/
14
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)

15
Operator Precedence
  • Heres another problem. Whats the answer to
    this?
  • x 7 3 6
  • Two Options (depending on the order of
    operations)
  • Perform addition first
  • 7 3 10 ? 10 6 60
  • Perform multiplication first
  • 36 18 ? 718 25
  • Which option is correct?

16
Operator Precedence
Operator precedence represent rules for
evaluating mathematical expressions. Every
programming language has similar rules.
17
  • 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.

18
Color Models
  • colorMode(RGB, 255)
  • // processings 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

19
  • // 5 rectangles arranged in the canvas
  • size(300,300)
  • colorMode(HSB, 360, 100, 100)
  • background(20,20,100)
  • fill(20,20,80)
  • stroke(20,20,20)
  • rectMode(CENTER) / CENTER is a constant /
  • rect(50,50,50,50)
  • fill(20,20,80)
  • rect((width/2-50)/2 50,(width/2-50)/2
    50,50,50)
  • fill(20,20,60)
  • rect(width/2,height/2,50,50)
  • fill(20,20,40)
  • rect(width/2(width/2-50)/2,height/2(width/2-50)/
    2,50,50)
  • fill(20,20,20)
  • rect(width-50,height-50,50,50)

20
  • // 5 rectangles arranged in the canvas
  • size(300,300)
  • colorMode(HSB, 360, 100, 100)
  • int angle 20
  • background(angle, 20,100)
  • fill(angle, 20, 80)
  • stroke(angle, 20, 20)
  • rectMode(CENTER) / CENTER is a constant /
  • rect(50,50,50,50)
  • fill(angle, 20, 80)
  • rect((width/2-50)/2 50,(width/2-50)/2
    50,50,50)
  • fill(angle,20,60)
  • rect(width/2,height/2,50,50)
  • fill(angle,20,40)
  • rect(width/2(width/2-50)/2,height/2(width/2-50)/
    2,50,50)
  • fill(angle,20,20)
  • rect(width-50,height-50,50,50)

21
Examples
  • // 1 colorful rectangle centered in the canvas
  • size(300,300)
  • background(20,20,255)
  • fill(20,255,20)
  • stroke(255,20,20)
  • rectMode(CENTER)
  • /
  • CENTER is a built-in variable that cant be
  • changeda constant
  • /
  • rect(width/2,height/2,50,50)

22
More Examples
  • // 5 small rectangles across the top of the
    canvas
  • size(300,300)
  • int xIncrementwidth/7
  • rect(xIncrement, 10, 20, 20)
  • rect(xIncrement2, 10, 20, 20)
  • rect(xIncrement3, 10, 20, 20)
  • rect(xIncrement4, 10, 20, 20)
  • rect(xIncrement5, 10, 20, 20)

23
More Examples
  • // the mystery program that changes the display
    as the size changes
  • size(950,800)
  • int boxSize width/3
  • int xPos
  • xPosboxSize0
  • fill(xPos17255, xPos11255, xPos4255)
  • // note we are in rectMode(CORNER) by default
  • rect(xPos, 0, boxSize, height)
  • xPosboxSize1
  • fill(xPos17255, xPos11255, xPos4255)
  • rect(xPos, 0, boxSize, height)
  • xPosboxSize2
  • fill(xPos17255, xPos11255, xPos4255)
  • rect(xPos, 0, boxSize, height)

24
In-class Lab
  • Create a pattern of six of the same shape and
    color (such as lines or rectangles).
  • Revise your code to set the positions of the
    shapes with functions of 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
    angle and saturation remain constant.
  • Use a variable to set angle and saturation and
    test with a several values for each.
Write a Comment
User Comments (0)
About PowerShow.com