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

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

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.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
1 Chapter 3 Arithmetic Expressions. 2 Chapter 3 Topics l Overview of Java Data Types l Numeric Data Types l Declarations for Numeric Expressions l Simple.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Expressions, Data Conversion, and Input
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Types and Expressions. 3.2 Primitive and Reference Types Each data value has a type The type must be declared for all variables & constants The compiler.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Primitive Data Types and Operations Identifiers, Variables, and Constants Primitive Data Types Byte, short, int, long, float, double, char, boolean Casting.
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
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.
Operations Making Things Happen. Our Scuba Program #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Chapter 2: Using Data.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
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.
1 Operations Making Things Happen (Chap. 3) Expressions.
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.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
1 Operations Chapter 4 & Section Expressions As we've seen, in a C++ program, any finite sequence of objects and operations that combine to produce.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
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.
Operations Making Things Happen.
Lecture 3 Java Operators.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Identifiers - symbolic names
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
IDENTIFIERS CSC 111.
Lecture 3 Expressions Richard Gesick.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Numerical Data Types.
Expressions and Assignment
Data Types and Expressions
Primitive Types and Expressions
Chapter 2 Primitive Data Types and Operations
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

PROCESSING Numeric Types

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

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

4 Kinds of Objects In programs, objects can appear as: Literals Variables Constants Variables and constants are named using identifiers.

5 Literals Literals are object values specified explicitly in the code. Examples:

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

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

8 Primitive Types: Integers Integers are whole numbers. Processing provides several integer types: byte −128 through 127 short −32768 through int − through long −2 63 through 2 63 − 1 Literal integer expressions:

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 * 10 n where: x is a fixed-point real-number, + or - n is an integer, negative or positive. Example: Avagadro’s number is near e23

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;

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

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;

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

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

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

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

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.

Assignment Expressions The value of a variable can be changed using an assignment statement: age = 19; credits = hours; Pattern: Name = Expression;

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)

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 circle ellipse(SCREEN_WIDTH/2, SCREEN_HEIGHT/2, squareWidth, squareWidth);

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.

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

23 Type Conversion Implicit: Using explicit type casting: (double)3 / / 4

24 Mathematical Resources Static constants, e.g.: PI: TWO_PI: Useful methods, e.g.: abs(x) sqrt(x) max(x,y) exp(x) log(x) pow(x,y) Examples: final float C = e8; double e = m * pow(C,2);

Precedence Question: What is the value of the following expression: * 4 Operator precedence governs evaluation order of operations in an expression. Parentheses can be used to override default precedence: ( 2 + 3) * 4

Operator Precedence ( ) HIGHER + (positive), - (negative), ! (NOT) *, /, % +, -, >= ==, != && || LOWER

Associativity Question: What is the value of the following expression: Associativity governs the order of execution of operators that have equal precedence. Again, parentheses can be used to override the default.

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

Assignment Chaining The assignment operator supports expressions like this: int w, x, y, z; w = x = y = z = 0;

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

In General Most arithmetic expressions of the form: var = var  value; can be written in the “shortcut” form: var  = value; Examples: x *= 2.0; // double x’s value y /= 2.0; // decrease y by half

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 var var--; // sub 1 from var and in prefix form : ++var --var

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.