Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.

Slides:



Advertisements
Similar presentations
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.
Advertisements

ECP4136 Java Technology Tutorial 2. Overview Replacement class? Previous tutorials What you’ve learnt Tasks for this tutorial session.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
Chapter Day 4. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 4 Return Signed Contracts Questions from last Class?? Problem set 1 Posted.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Primitive Data Types There are exactly eight primitive data types in Java four of them represent integers: byte (class Byte), short (class Short), int.
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.
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.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Expressions, Data Conversion, and Input
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Chapter 2 part #4 Operator
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
LESSON 6 – Arithmetic Operators
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
Mathematical Calculations in Java Mrs. C. Furman.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
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 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Doing math In java.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
2-1 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string.
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.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Expressions.
Chapter 7: Expressions and Assignment Statements
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Creating Objects & String Class
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Increment and Decrement
Lecture 3 Expressions Richard Gesick.
With Assignment Operator
Arithmetic Expressions & Data Conversions
Expressions and Assignment
Data Types and Expressions
Chapter 4: Expression and Operator
Operator King Saud University
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Expressions Expression Combination of one or more operators and operands Arithmetic operations Binary operations Addition(+), Subtraction(-), multiplication(*), Division(/) Remainder(%)  Example: 17%4 = 1, 3%8 = 8 Unary operations (rarely used) Example: -1; -4; +5

Result of an arithmetic operation If either or both operands Used by an arithmetic operator are floating point Then the result is a floating point Result is floating point value If both or either operands are floating point values = 8.4

Division However, division operation is less intuitive If both operands are integer => integer division 10/4 = 2 If either or both are floating point=> floating point division 10.0/4 and 10/4.0 and 10.0/4.0 are all 2.5

Operator precedence Expressions are evaluated according to operator precedence hierarchy It follows the same rules learned in Algebra Multiplications, divisions and remainder are performed  Prior to addition, and subtraction Precedence can be forced by using parentheses (14+8)/2; Arithmetic operators with the same precedence Are evaluated from left to right

Operator precedence (cont’d) Precedence level OperatorOperation Unary plus Unary minus 2*/%*/% Multiplication Division Remainder Addition Subtraction 4=Assignment Highest priority Lowest priority

Operator Precedence What is the order of evaluation in the following expressions? a + b + c + d + e 1432 a + b * c - d / e 3241 a / (b + c) - d % e 2341 a / (b * (c + (d - e))) 4123

Assignment revisited The assignment operator Has a lower precedence than arithmetic operators First the expression on the right hand side of the = operator is evaluated Then the result is stored in the variable on the left hand side answer = sum / 4 + MAX * lowest; 1432

Assignment revisited (cont’d) The right and left sides of an assignment Can contain the same variable First, one is added to the original value of count Then the result is stored back into count (overwriting the original value) count = count + 1;

Example Program (TempConverter) Converts a particular Celsius temperature value To its equivalent Fahrenheit value using the expression See TempConverter.java

Increment and decrement a variable There are three ways To increment or decrement a variable, it may appear On both the left-hand side and the right-hand side  count = count +1; or count = count – 1; The left-hand side of an increment(++) or decrement(--) operator (postfix form)  count++; or count–-; The right-hand side of an increment(++) or decrement(--) operator (prefix form)  ++count; or -–count;

Increment and decrement operators The increment and decrement operators use only One operand The increment operator (++) adds one to its operand As such, count++; count = count + 1; The decrement operator (--) subtracts one from operand As such, count--; count = count - 1; Increment and decrement operators can be used In postfix form : count++; or Prefix form: ++count;

Postfix and prefix forms When used alone the prefix and postfix forms are equivalent It doesn’t matter if you write  count++; or ++count; In a larger expression they can yield different results Total = count++; Total = ++count;

Assignment operators: main idea Often we perform An operation on a variable, and then Store the result back into that variable Java provides assignment operators To simplify that process For instance num += count;  num = num + count;

Assignment operators Many assignment operators are defined in JAVA += performs addition Total += 5; is equivalent to Total = Total + 5; -= performs subtraction result -= a + b; result = result – (a + b); /= performs division highest /= 4; highest = highest/4; *= performs multiplication

Behavior of assignment operators The behavior of some assignment operators Depends on the types of the operands If the operands to the += Are strings => operator performs string concatenation The behavior of an assignment operator (+=) is always consistent with the behavior of the corresponding operator (+)

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Data conversion Sometimes, It is convenient to convert data from one type to another For example in a particular situation we may want to treat An integer as a floating point value These conversions Do not change the type of a variable nor the value stored in it They only convert a value as part of a computation

Data conversion Widening conversions It is safe to convert from a byte type to a short type Since byte is stored in 8 bits whereas short in 16 bits There is no loss of information, and the numeric value is preserved exactly FromTo byteshort,int,long, float,double shortint, long, float, double intlong, float, or double longfloat or double floatdouble

Data conversion (cont’d) Narrowing conversions Go from one type to a type that uses less space As such, some of the information may be compromised In general, they must be avoided FromTo shortbyte intbyte, short longbyte, short, int floatbyte, short, int, long doublebyte, short, int, long, float

Conversion techniques In JAVA, conversion can occur in three ways Assignment conversion When a value of one type is assigned to a variable of another type Promotion When operators need to modify their operands in order to perform the right operation Casting The most general form of conversion

Assignment conversion Assignment conversion occurs When a value of one type is assigned to a variable of another type if money is a float variable and dollars is an int variable  money = dollars => dollars converted to float Only widening conversions can be accomplished Via an assignment Note that the value or type of dollars did not change

Conversion via promotion It occurs automatically When certain operators need to convert their operands to perform operations If sum is a float and count is an integer The value of count is converted to float to perform calculation  result = sum/count;

Conversion using casting It is the most general form of conversion in JAVA a JAVA operator Specified by a type name in parentheses: (float) for instance placed in front of the value to be converted float money = 84.69; int dollars; dollars = (int) money; => dollars = 84

Casting: analysis Casting is the most powerful and dangerous Both widening and narrowing conversions Can be accomplished by casting a value To cast The type is put in parentheses in front of the value Being converted

Conversion using casting (cont’d) It is helpful To treat a value temporarily as another type For example, if total and count are integers But we want a floating point result when dividing them  int total, count; float result; result = (float) total / count;

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes