Lesson Two: Everything You Need to Know

Slides:



Advertisements
Similar presentations
Lesson One: The Beginning Chapter 3: Interaction Learning Processing: by Daniel Shiffman Presentation by Donald W. Smith Graphics from text.
Advertisements

Variables and Operators
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Lesson Three: Organization
Basic Elements of C++ Chapter 2.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Chapter 2: Using Data.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Lesson Two: Everything You Need to Know
Copyright Curt Hill Variables What are they? Why do we need them?
Variables Art &Technology, 3rd Semester Aalborg University Programming David Meredith
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Lesson Two: Everything You Need to Know
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
COMP Primitive and Class Types Yi Hong May 14, 2015.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 2. Program Construction in Java. 01 Java basics.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Variables. Something to mention… void setup(){ size(200, 200); background(255); smooth(); } void draw() { stroke(0); strokeWeight(abs(mouseX-pmouseX));
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter # 2 Part 2 Programs And data
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Topics Designing a Program Input, Processing, and Output
Chapter 2 Basic Computation
Data Types and Expressions
Chapter 2: Introduction to C++
Chapter 3 Syntax, Errors, and Debugging
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Multiple variables can be created in one declaration
Type Conversion, Constants, and the String Object
Chapter 2 Basic Computation
Basic Elements of C++ Chapter 2.
IDENTIFIERS CSC 111.
Building Java Programs Chapter 2
2.1 Parts of a C++ Program.
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Chapter # 2 Part 2 Programs And data
Introduction to Java, and DrJava
Chapter 2: Java Fundamentals
Chapter 2: Introduction to C++.
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Primitive Types and Expressions
Chapter 4, Variables Brief Notes
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Variables and Operators
Variables and Constants
Presentation transcript:

Lesson Two: Everything You Need to Know Chapter 4: Variables Learning Processing Daniel Shiffman Presentation by Donald W. Smith Graphics from text

Lesson Two: Everything You Need to Know 4: Variables What are they? Declaring and initializing variables Common uses for variables Variables you get “for free” in Processing Aka: Built-in variables Using random values for variables 5: Conditionals 6: Loops Learning Processing: Slides by Don Smith

What is a variable? Storage Analogies: Different sizes Like a bucket Like a locker Graph Paper/Spreadsheet Many ‘storage’ places, all with unique locations (x,y) In Excel, E3 could be named ‘Billys Score’ Billy’s score can change! A B C D E F 1 2 3 4 5 Learning Processing: Slides by Don Smith

What is a variable? In Algebra: In Programming: x = y * z Named with single letters to represent some number In Programming: We use longer, more descriptive names Variables refer to ‘memory locations’ Stored in ‘RAM’ Random Access Memory Have a ‘size’ How many bytes of memory ‘wide’ Learning Processing: Slides by Don Smith

Variables must be ‘Declared’ What is a Declaration? Programmer tells the compiler: Type Name ; What is a Type? Each type requires a specific amount of storage ‘Primitive’ types include three main categories Integers – Whole Numbers (positive and negative), no fractions Floating point – Numbers with fractional parts and exponents Characters – One letter that you can type A, B, C, a, b, c, 1, 2, 3, %, &…. Learning Processing: Slides by Don Smith

All Primitive Types Integer Types byte: A very small number (-127 to +128) short: A small number (-32768 to +32767) int: A large number (-2,147,483,648 to +2,147,483,647) long: A huge number Floating Point Types float: A number with decimal places, 4 bytes double: Much more precise, for heavy math, 8 bytes Other Types boolean: true or false char: One symbol in single quotes ‘a’ Learning Processing: Slides by Don Smith

Primitive Type Storage Integer Types byte: short: int: long: Floating Point Types float: double: Other Types boolean: char: Learning Processing: Slides by Don Smith

Primitive Type Examples Integer Types byte: 123 short: 1984 int: 1784523 long: 234345456789 Floating Point Types float: 4.0 double: 3.14159, 1.0e+23, 2.4e-20 Other Types boolean: true char: ‘a’ Learning Processing: Slides by Don Smith

Numeric versus ‘Character’ types How do you decide if you need a numeric or a character type? If you plan on doing ‘math’ on the variable, then you MUST use a numeric type What is the letter ‘a’ times the letter ‘c’? Notice the single quotes around characters If you plan on using “Strings” (later), they are just words made up of characters. “Bob” and “Mary” are Strings (of characters) Notice the double quotes around strings What is the string “Bob” times the string “Mary”? Learning Processing: Slides by Don Smith

Declaring and Initializing Variables What is Initialization? Setting an initial value into the contents of the variable Pseudocode: Set NumPlayers to 5 Can be done in two ways: During Declaration: On one line int count = 50; // declare and initialize After declaration: On two lines int count; // declare the variable count = 50; // initialize the value Can also be initialized with a calculation! int max = 100; int min = 10; int count = max – min; // calculation Learning Processing: Slides by Don Smith

Pong Variables What variables would be required to play? Scores? Paddle locations? Ball location? Net location Screen Size? Learning Processing: Slides by Don Smith

Naming Variables There are some ‘rules’ and some ‘best practices’ Letters, Digits and underscore ( _ ) are OK to use Cannot start with a digit ( 0,1,…9 ) Cannot use reserved words mouseX, int, size.. Best Practices Use descriptive names boolean moreToDo; Use ‘camelHump’ notation Start with lower case Each new word starts with Upper Case Learning Processing: Slides by Don Smith

Declaration and Initialization Examples Type name (optional initialization) ; int count = 0; // Declare an int , initialized to 0 char letter = 'a'; // Declare a char, initialized to 'a' double d = 132.32; // Declare a double, initialized to 132.32 boolean happy = false; // Declare a boolean, initialized to false float x = 4.0; // Declare a float, initialized to 4.0 float y; // Declare a float (no assignment) float z = x * y + 15.0; // Declare a float, initialize it to // x times y plus 15.0. After declaration Assignments: count = 1; letter = ‘b’; happy = true; y = x + 5.2; // Assign the value of x plus 5.2 Learning Processing: Slides by Don Smith

Declaration and Initialization Issues You can only initialize a variable to a value of the same, or compatible type: Which initializations are compatible? int count = ‘a’; char letter = 0; double deposit = “Fred”; boolean happy = 1; float feet = 6; int inches = feet * 12; long giant = feet * 3.0; Learning Processing: Slides by Don Smith

Declaration and Initialization Issues You can only initialize a variable to a value of the same, or compatible type. Which initializations are compatible? int count = ‘a’; char letter = 0; double deposit = “Fred”; boolean happy = 1; float feet = 6; int inches = feet * 12; long giant = feet * 3.0; Learning Processing: Slides by Don Smith

Where to Declare Variables Remember that your code is in ‘blocks’ Variables can go inside or outside these blocks For now, we will put them ‘outside’ (before) blocks

Varying Variables: Example 4.3 Remember that processing calls draw()in a loop If you want the variable to change every time: Declare and initialize it outside of draw()! Change it inside draw()! Moves as circleX increases 

What happens in example 4.3? circleX Is initialized to 0 Used first time draw()is called ellipse(circleX, circleY, 50,50); Then circleX = circleX + 1; Next time draw()is called, circleX is 1 Next time draw()is called, circleX is 2 .. Until circleX is over 200, and the circle just moves off the right side of the screen, never to be seen again! It is often useful to make a ‘table’ of all of the variables that are being changed every time through a ‘loop’ Call to draw() circleX 1 2 3 4 5 … 200 199 Learning Processing: Slides by Don Smith

Using Many Variables Make things more interesting using more variables! Declare and initialize them outside of draw()! Change them inside draw()! Call to draw() circleX circleY circleW circleH 1 50 100 2 0.5 50.5 100.5 3 1.0 51 101 10 4.5 54.5 104.5 30 14.5 64.5 114.5 49.5 99.5 149.5 200 199.5

Operators on numerical variables Addition: + Increment: x = x+1, ++x Subtraction: - Decrement: x = x-, --x Multiplication: * Division: / Pay attention to integer and float divisions Remainder: % 5%3  2, 2%12  2, 12%3  0 Assignment: = Learning Processing: Slides by Don Smith

Precedence of operators Parentheses have the highest precedence *, / , %  +, -  = If not sure, use parentheses. Learning Processing: Slides by Don Smith

Examples Convert temperatures in F and C Extract the two digits from a two-digit number. Learning Processing: Slides by Don Smith

System Variables Processing provides many ‘built-in’ variables: These are not for you to play with, but may change! mouseX , mouseY, pmouseX and pmouseY width: Width (in pixels) of sketch window height: Height (in pixels) of sketch window frameCount: Number of frames processed frameRate: Rate (per sec.) that frames are processed screen.height, screen.width: Entire screen key: Most recent key pressed on keyboard keyCode: Numeric code for which key pressed mousePressed: True or false (pressed or not?) mouseButton: Which button (left, right, center) Learning Processing: Slides by Don Smith

Random: Variety is the spice of life Processing (and all programming languages) provide a way to get a random value when your program runs. random() is a ‘function’ that returns a float You can assign this number to a variable and use it! Some examples: Learning Processing: Slides by Don Smith

What is that (int)and (1,100)stuff? Since random()returns a floating point number and w is an int, we need to change the type from float to int. This is called ‘casting’ random(1,100) ; (1,100) are ‘parameters’ which tell the random function the range of numbers to return w will be between 1 and 99 (not quite 100) Learning Processing: Slides by Don Smith

Make Zoog Move! Use variables to make Zoog move and change! 1) Make Zoog rise from below the screen Need variables for the Zoog’s X and Y locations Note: Locations of ALL of Zoog’s parts need to use them If left eye is left 30 and down 50 from the center of the head: ellipse( zoogX – 30, zoogY + 50, 20, 30); 2) Change his eye color randomly as he moves Need variables for Zoog’s eye color (R,G and B) Learning Processing: Slides by Don Smith

Lab06: Moving Zoog Use variables and random() to make a Zoog move from upper left to lower right of the screen. Starting point is provided (Example 4.8) Plan! Declare variables Use variables Use Random Learning Processing: Slides by Don Smith

Summary Variables have names and types You must declare your variables There are rules and best practices for naming variables You must declare your variables Determines the ‘type’, size of storage, and maximum values You can initialize variables in different ways Variables have many uses to programmers processing provides ‘system’ (built-in) variables You can use random numbers to make your program run differently every time Learning Processing: Slides by Don Smith