CST112 Variables Page 1

Similar documents
1 Getting started with Processing

Variables. location where in memory is the information stored type what sort of information is stored in that memory

Pick a number. Conditionals. Boolean Logic Relational Expressions Logical Operators Numerical Representation Binary. CS Conditionals 1

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Basic Computer Programming (Processing)

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Full file at

An Introduction to Processing

Chapter 2: Overview of C++

Full file at

IT 374 C# and Applications/ IT695 C# Data Structures

Differentiate Between Keywords and Identifiers

BASIC ELEMENTS OF A COMPUTER PROGRAM

Introduction to Programming EC-105. Lecture 2

Class Notes CN19 Class PImage Page

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Fundamentals of Programming CS-110. Lecture 2

Objectives. In this chapter, you will:

void mouseclicked() { // Called when the mouse is pressed and released // at the same mouse position }

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Fundamentals of Programming

DEPARTMENT OF MATHS, MJ COLLEGE

VARIABLES AND TYPES CITS1001

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

A B C D CS105 03a Interaction

Fundamental of Programming (C)

Using Methods. Methods that handle events. Mairead Meagher Dr. Siobhán Drohan. Produced by: Department of Computing and Mathematics

A First Program - Greeting.cpp

Chapter 2 Working with Data Types and Operators

[ the academy_of_code] Senior Beginners

COMP Primitive and Class Types. Yi Hong May 14, 2015

CS242 COMPUTER PROGRAMMING

VARIABLES AND CONSTANTS

Basic data types. Building blocks of computation

Lab # 02. Basic Elements of C++ _ Part1

Computational Expression

Basics of Java Programming

Lecture 2 Tao Wang 1

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

Reserved Words and Identifiers

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

Creating a C++ Program

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Visual C# Instructor s Manual Table of Contents

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS102: Variables and Expressions

Ex: If you use a program to record sales, you will want to remember data:

Basic Input and Output

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Datatypes, Variables, and Operations

Programming with Java

Miscellaneous Stuff That Might Be Important.

CISC 1600 Lecture 2.2 Interactivity&animation in Processing

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Section 2.2 Your First Program in Java: Printing a Line of Text

Chapter 2: Using Data

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

BITG 1233: Introduction to C++

Software and Programming 1

SFPL Reference Manual

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Chapter 2: Using Data

Computer Programming : C++

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Values and Variables 1 / 30

Declaration and Memory

The Java Language Rules And Tools 3

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Arithmetic Expressions in C

CISC 1600, Lab 2.2: Interactivity in Processing

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

BTE2313. Chapter 2: Introduction to C++ Programming

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

UNIT- 3 Introduction to C++

Program Fundamentals

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Advanced Computer Programming

Programming Lecture 3

Variables and Control Structures. CS 110 Eric Eaton

A Fast Review of C Essentials Part I

3. Java - Language Constructs I

Exercise: Using Numbers

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

Introduction to Programming Using Java (98-388)

Transcription:

CST112 Variables Page 1 1 3 4 5 6 7 8 Processing: Variables, Declarations and Types CST112 The Integer Types A whole positive or negative number with no decimal positions May include a sign, e.g. 10, 125, -37, 1584, +3 Numeric data type values may not include: Dollar signs ($), commas (,) percent symbol (%) Stored as is binary equivalent 0000 0000 0110 0001 (binary) = 97 The Floating Point Types A real positive or negative decimal number with the capability of storing decimal positions May include a sign, e.g. 1.0, 150.67, -3.97, 1584.002, +0.3 The Character Type A single character that is enclosed in single quotes (apostrophes), e.g. 'S' Each character is represented in Unicode by two bytes in memory or on disk The Unicode Table The complete Unicode specification can be found at: http://www.ssec.wisc.edu/~tomw/java/unicode.html The character 'A' is: 65 in decimal 0000 0000 0100 0001 in Unicode binary The character 'a' is: 97 in decimal 0000 0000 0110 0001 in Unicode binary The Boolean Type Variable that may store only one of two possible values True or False Useful for data that represents answer to a Yes/No question, e.g. Matriculated? Married? Variables (Page 1) A programmer-defined word used in a program to store a data value Represents the contents of an address in computer memory (RAM) where data is

CST112 Variables Page 2 stored Wherever a variable name is used in a program, it represents the contents (value) of the data stored in that memory location 15 16 17 18 19 Variables (Page 2) We already have used variables in our programs The system variables mousex and mousey store the current x- and y-coordinates of the mouse Those values are stored in RAM memory Whenever those variable names are used in a statement, the current values of the variables are substituted into the execution of that statement, e.g. ellipse(mousex, mousey, 50, 50); Declaring Variables (Page 1) Before being used in a program, all programmer-defined variables must be declared In this process: Names the variable to the program Defines the variable s type Designates RAM area for the variable s storage Declaring Variables (Page 2) type variablelist [= initialvalue]; Examples: int x; int myred, mygreen, myblue; More than one variable may be declared in the same statement double pi = 3.141592653589793; An initial value may be assigned to a variable when declared (the value later may be updated) Types (Page 1) The type (e.g. the data type) indicates the kind of data may be stored within a declared variable This is important so the computer can know how much memory to allocate for the variable s storage All the primitive types in Processing are keywords which can be used to declare the variables Reminder a keyword is a command that is part of the Processing language and has a required syntax Up until now the only type of statements we have used in Processing are functions Types (Page 2) Processing s primitive types:

CST112 Variables Page 3 char: one character stored in 16-bit Unicode representation, e.g. 'A' or 'a' byte: one byte integer between -128 and 127 short: two byte integer between -32,768 to 32,767 int: four byte integer between -2,147,483,648 to 2,147,483,647 long: eight byte integer 20 21 27 28 29 Types (Page 3) Processing s primitive types (con.): float: a floating point decimal value which occupies four bytes with 6 or 7 significant digits, e.g. 3.14159 double: a floating point decimal value which occupies eight bytes with 14 or 15 significant digits, e.g. 3.141592653589793 boolean: true or false Types (Page 4) Strings may be stored in RAM using the class String which declares an object variable (also called a reference type) String color = "red"; Keywords Part of a computer language words that have a specific meaning to the compiler Primitive type names such as int and double, along with the word void are keywords (also known as reserved words) Usage must conform to a required syntax Keywords may not be used as identifiers (programmer-defined words) Always spelled with all lowercase letters Identifiers A variable name is considered an identifier, that is a word that is defined by the programmer Consists of letters, numbers, and underscores (_) Must begin with a letter Must be one word with no blank spaces Are case sensitive like all other Processing words E.g. x, blue_value, myheight Naming Variables Variable names follow the same naming rules as all other identifiers It is best to avoid using Processing function names and keywords as variable names (or programmer-defined names in general) Some valid variable names are: Welcome m_inputfield1 C3PO

CST112 Variables Page 4 31 32 33 43 44 Variable Naming Conventions A convention (also called a standard) is an accepted method/approach used by a majority of programmers in industry If a variable name is one word, all letters are lowercase If a variable name has more than one word Begin the first word with a lowercase letter Every succeeding word has the first letter capitalized, e.g. myheight firstredline Assignment Statements (Page 1) The assignment operator (=) is used to store a value or the result of an expression into memory As stated before the memory location of that value is represented by a variable The assignment variable always must be to the left of the assignment operator (equal sign) variable = value/expression; Assignment Statements (Page 2) Examples: A single value x = 15; The result of arithmetic expression (equation) kilometers = miles * 1.614; When a variable is initially declared int myred = 255; Variable Names on Both Sides of Assignment Operator (Page 1) Examples: myred = myred + 10; x = x + 1; y = y 1; A value or the result of an expression on the right of the assignment operator is stored into the variable on the left Variable Names on Both Sides of Assignment Operator (Page 2) Examples: myred = myred + 10; Value of variable myred before execution 25 Value of variable myred after execution 35 x = x + 1; Value of variable x before execution 10

CST112 Variables Page 5 Value of variable x after execution 11 y = y 1; Value of variable y before execution 29 Value of variable y after execution 28 51 52 53 58 59 System Variables (Page 1) Additional system variables (beyond mousex and mousey) that store data that is commonly needed in Processing sketches include: width the width in pixels of the output window height the height in pixels of the output window framecount the number of frames processed framerate the current rate at which frames are processed per second System Variables (Page 2) Additional system variables (con): screen.width the width in pixels of the entire screen screen.height the height in pixels of the entire screen key the most recent key pressed on the keyboard keycode a numeric code for the most recent key pressed on the keyboard System Variables (Page 3) Additional system variables (con): keypressed a Boolean value (true or false) that indicates whether a key currently is pressed mousepressed a Boolean value (true or false) that indicates whether a mouse button currently is pressed mousebutton value that indicates which mouse button currently is pressed: Left, Right or Center Additional system variables can be found in the Processing reference Randomization (Page 1) Computers have the ability to add unpredictability to applications by introducing randomization The Processing function random() is able to generate and return random values of type float within a specified range Those values assigned to variables can be used to randomize the colors, positions, sizes and other elements of the functions we already have learned Randomization (Page 2) Format 1: random(start, end) start is the lowest value that could be returned end is the highest value that could be returned or any other value in between could be returned

CST112 Variables Page 6 Example 1: float center; center = random(1, 200); ellipse(center, center, 50, 50); A value between 1 and 200 is assigned to center 60 Randomization (Page 3) Format 2: random(end) When no start argument is given, zero (0) is the lowest value that could be returned end is the highest value that could be returned or any other value in between could be returned Example 2: float center; center = random(200); ellipse(center, center, 50, 50); A value between zero (0) and 200 is assigned to center 63 64 65 Casting (Page 1) Because the function random() generates a value of type float, its return value cannot be assigned to a variable of type int For example the following statement: int center; center = random(200); at run-time displays in the message window the error cannot convert from float to int Casting (Page 2) Since many of the functions used in Processing require integer arguments, it frequently would be preferable to store random values as int s This commonly is achieved by a process known as casting a technique in which a value of one type temporarily is converted into another type Casting (Page 3) (type) expression (type) is the type to which the result or return value of the expression is converted expression is the result or return value that is being converted (expression type is not actually changed) int center; center = (int) random(200);

CST112 Variables Page 7 Converts the generated random number to type int before it is assigned to the int variable center 82 83 84 85 86 Images Computer images are files that contain pictures which are nothing more than data that organize pixels onto the monitor (screen) There are common image file formats accepted by Processing and recognized by their file extensions:.gif,.jpg,.png,.tga Processing has the capability to: Display an image as the output window background Manipulate smaller images in the output window The PImage Class (Page 1) PImage is a (data) type in some ways just like int and float However PImage is a class type like String, not a primitive type like an int Declaring a PImage variable creates an object variable (also called a reference type) with the ability to store and display an image As an object variable, it is given certain capabilities, unlike primitive variables The PImage Class (Page 2) Format to declare a PImage object variable is similar to that of a primitive like int: PImage pimagevariable; PImage is the type PImage underwater; The loadimage() Method The loadimage() method stores the image in the PImage variable and creates the object pimagevariable = loadimage("path/filename"); The path/filename goes in quotation marks underwater = loadimage("underwater.jpg"); The preceding is valid if the file underwater.jpg is in the same folder as the.pde file The background() Function Revisited The background of the output window can be set to an image instead of a colored background Image file must have already been loaded into the PImage variable, e.g. a loadimage() function call

CST112 Variables Page 8 background(pimagevariable); background(underwater); The size of the output window and that of the image file must be exactly the same in pixels 89 90 91 The image() Function (Page 1) A smaller image file may be manipulated within the output window Its position as well as size (image can be resized) can be set within the image() function call Image file must have already been loaded into the PImage variable, e.g. a loadimage() function call The image() Function (Page 2) image(pimagevariable, xposition, yposition [, width, height] ); xposition and yposition are the coordinates within the output window where the image is drawn; default is upper-left corner width and height are optional image resizing values image(fish, 230, 170); The imagemode() Function These modes are similar to ellipsemode() and rectmode() which is logical since a PImage object really is a shape with an image inside it: imagemode(corner); image arguments specify upper-left corner, width, and height (the default) imagemode(center); image arguments specify the centerpoint, width, and height imagemode(corners); image arguments specify upper-left and lower-right corners