processing numeric types. objectives be able to … explain the difference between a literal, a...

33
PROCESSING Numeric Types

Upload: barbara-roberts

Post on 03-Jan-2016

224 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

PROCESSINGNumeric Types

Page 2: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Objectives

Be able to …• Explain the difference between a literal, a variable and a

constant• Declare numeric variables and constants• Update the value of a variable• Label and use operators and operands in an expression• Write expressions appropriately using precedence and

associativity• Label type conversion• Expand shortcuts

Page 3: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

3

Types• In object-oriented programming:

• All program data are stored as objects.• All objects have specified data types.

• The data type determines:• How an object is represented• How an object is manipulated

Page 4: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

4

Kinds of Objects• In programs, objects can appear as:

• Literals

• Variables

• Constants

• Variables and constants are named using identifiers.

Page 5: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

5

Literals• Literals are object values specified explicitly in the code.

• Examples: 42 3.7

Page 6: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

What does this do?What are the literals?

size(300, 300);background(255);smooth();strokeWeight(4);stroke(0);rect(25, 25, 250, 250);stroke(100, 100, 255);ellipse(150, 150, 250, 250);

Page 7: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

7

Variable Declarations• Variables are used to store values that may change during

program execution.• They can be either initialized or uninitialized.• Pattern:

Type Name [ = Expression ];

Page 8: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

8

Primitive Types: Integers• Integers are whole numbers.

• Processing provides several integer types:byte −128 through 127short −32768 through 32767int −2147483648 through 2147483647long −263 through 263 − 1

• Literal integer expressions:42-220

Page 9: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

9

Primitive Types: Real Numbers• Reals are numbers with decimal points.• Processing provides two real data types: float double

• Literal real expressions:xEn represents x * 10n where:• x is a fixed-point real-number, + or -• n is an integer, negative or positive.

Example: Avagadro’s number is near 6.0221415e23

Page 10: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

10

Variable Declarations• Variables are used to store values that may change during

program execution.• They can be either initialized or uninitialized.• Pattern:

Type Name [ = Expression ] ;

• Examples: int age = 18; double gpa = 3.25;

Page 11: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

With Variables!

int screenWidth = 300; int screenHeight = 300;

size(screenWidth, screenHeight);background(255);smooth();strokeWeight(4);

stroke(0);rect(25, 25, 250, 250);stroke(100, 100, 255);ellipse(150, 150, 250, 250);

Page 12: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

12

Constant Declarations• Constants are used to store values that do not change

during execution.• They must be initialized.• Pattern:

final Type Name = Expression;

• Examples: final int MAX_SCORE = 100; final double SPEED_OF_LIGHT = 2.998e8;

Page 13: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

13

Multiple Declarations • Values of the same type can be declared and/or initialized

in a list• Type is given only once• Examples:

double gpa = 3.25, credits;final int MAX = 100, MIN = 0;

Page 14: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

With Constants and Chaining!

final int SCREEN_WIDTH=300, SCREEN_HEIGHT = SCREEN_WIDTH;

size(SCREEN_WIDTH, SCREEN_HEIGHT);background(255);smooth();strokeWeight(4);

stroke(0);rect(25, 25, 250, 250);

stroke(100, 100, 255); ellipse(150, 150, 250, 250);

Page 15: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

15

Identifiers• A variable or constant is named by an identifier.• Processing identifiers must begin with a letter or

underscore followed by zero or more letters, digits or underscores.

• Identifiers cannot be Processing reserved words e.g. int, double, float

Page 16: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

16

● Choose “good” identifier names.● Follow identifier naming conventions:

– Down-case variable identifiers.

– Camel-case multi-word variables.

– Up-case constant identifiers.

– Underscore multi-word constants.

– Capitalize class identifiers.

Naming Conventions

Page 17: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

17

Expressions• Expressions are sequences of objects (called operands)

and operators that combine to produce a value.• The operands can be variables, constants, literals or method calls.• The operators depend upon the data types of the operands.

• The type of the expression is the type of the value it produces.

Page 18: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Assignment Expressions• The value of a variable can be changed using an

assignment statement: age = 19; credits = hours;

• Pattern: Name = Expression;

Page 19: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Numeric Expressions• Processing provides arithmetic operators for reals and

integers : + - * / %• Integer division produces an integer, real division produces

a real:3/4 ® 3.0/4.0 ® 3.0/4 ® 3/4.0 ®

• Integer division: 3/4 ® (the quotient)

3%4 ® (the remainder)

Page 20: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

With Variable Expressions!!

final int SCREEN_WIDTH= 300, SCREEN_HEIGHT= SCREEN_WIDTH;final int MARGIN =25;

size(SCREEN_WIDTH, SCREEN_HEIGHT);background(255);smooth();strokeWeight(4);stroke(0);

int squareWidth = SCREEN_WIDTH – 2*MARGIN; //margin each side

rect(MARGIN, MARGIN, squareWidth, squareWidth);

stroke(100, 100, 255);

//center the circleellipse(SCREEN_WIDTH/2, SCREEN_HEIGHT/2,

squareWidth, squareWidth);

Page 21: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Practice

size(300, 200);fill(255, 0, 0);rect (0, 0, 150,

200);fill(0, 255, 0);rect(150, 0, 150,

200);

Goal: Change one variable, scaled version is created.

Page 22: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

22

Implicit Type Conversion

• When types are mixed in an expression, the “narrower” type is promoted to the larger type, e.g.,

3.0 / 4

• Possible promotions:

byte short int long float double

char

Page 23: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

23

Type Conversion

• Implicit:

• Using explicit type casting:(double)3 / 4

3.0 / 4

Page 24: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

24

Mathematical Resources• Static constants, e.g.:

PI: 3.1415927 TWO_PI: 6.2831855

• Useful methods, e.g.: abs(x) sqrt(x) max(x,y) exp(x) log(x) pow(x,y)

• Examples: final float C = 2.99792458e8; double e = m * pow(C,2);

Page 25: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Precedence• Question: What is the value of the following expression:

2 + 3 * 4

• Operator precedence governs evaluation order of operations in an expression.

• Parentheses can be used to override default precedence:

(2 + 3) * 4

Page 26: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Operator Precedence

( ) HIGHER

+ (positive), - (negative), ! (NOT)

*, /, %+, -<, <=, >, >===, !=&&|| LOWER

Page 27: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Associativity• Question: What is the value of the following expression:

8 - 4 - 2

• Associativity governs the order of execution of operators that have equal precedence.

• Again, parentheses can be used to override the default.

Page 28: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Syntactic Sugar?

No need to use these, but they make some common tasks more simple:

• Assignment chaining• Assignment shortcuts• Increment and decrement (prefix) and postfix

Page 29: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Assignment Chaining The assignment operator supports expressions like this:

int w, x, y, z;w = x = y = z = 0;

Page 30: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Assignment Shortcuts• Some assignments are so common:

var = var + x; // add x to var var = var - y; // sub y from var

• Processing provides shortcuts for them:var += x; // add x to var var -= y; // sub y from var

Page 31: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

In General• Most arithmetic expressions of the form:

var = var D value;

can be written in the “shortcut” form:var D= value;

• Examples:x *= 2.0; // double x’s valuey /= 2.0; // decrease y by half

Page 32: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Increment and Decrement• Other common assignments include:

var = var + 1; // add 1 to var var = var - 1; // sub 1 from var

• Processing provides shortcuts for them:in postfix form:

var++; // add 1 to varvar--; // sub 1 from var

and in prefix form: ++var --var

Page 33: PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants

Prefix/Postfix Increment• The difference between the forms:

• The prefix form produces the final (incremented) value as its result.• The postfix form produces the original (un-incremented) value as

its result.