Variables and Operators

Slides:



Advertisements
Similar presentations
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Chapter 2: Using Objects Part 1. To learn about variables To understand the concepts of classes and objects To be able to call methods To learn about.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CIS 234: Using Data in Java Thanks to Dr. Ralph D. Westfall.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Third Edition
String Escape Sequences
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.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Primitive Variables.
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
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Copyright © – Curt Hill Types What they do.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Data Tonga Institute of Higher Education. Variables Programs need to remember values.  Example: A program that keeps track of sales needs to remember.
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.
CPS120: Introduction to Computer Science Variables and Constants.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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.
Fundamentals 2.
Java Variables and Types
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Primitive and Reference Data Values
Lesson Two: Everything You Need to Know
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Variables ICS2O.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
Chapter 2: Java Fundamentals
Primitive Types and Expressions
Unit 3: Variables in Java
Variables and Operators
Chapter 2 Primitive Data Types and Operations
Variables and Constants
Presentation transcript:

Variables and Operators

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

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

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)

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

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

Primitive Data Types boolean — truth values — true or false char — characters — ’a’, ’G’, ’#’, ’3’ Integer values – whole numbers byte (-128 to 127) short (-32768 to 32767) int (-2147483648 to 2147483647) long (-9223372036854775808 to 9223372036854775807) Floating values -- decimals float (1.401298e-45 to 3.402823e+38) double (4.94065645841246e-324 to 1.79769313486231e+308)

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

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

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

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

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;

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 = x*y + 15.0f;

Arithmetic Operators = assignment operator x=9 + 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 += -= *= /= %=

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)

Operator Precedence Here’s another problem. What’s 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: 3*6 =18  7+18 = 25 Which option is correct?

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

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.

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

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);

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);

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 ;

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.