Software Technology - I Tools to do operations.....

Slides:



Advertisements
Similar presentations
Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment=
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
JAVA PROGRAMMING PART II.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Floating point variables of different lengths. Trade-off: accuracy vs. memory space Recall that the computer can combine adjacent bytes in the RAM memory.
© 2001 by Ashby M. Woolf Revision 3 Using Java Operators The Basic Toolkit Pliers, Screwdriver, Hammer and Drill.
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Department of Computer Engineering Computer Programming for International Engineers I NTERNATIONAL S CHOOL OF E NGINEERING C HULALONGKORN U NIVERSITY.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
CPS120: Introduction to Computer Science Operations Lecture 9.
Mixing integer and floating point numbers in an arithmetic operation.
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 Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Mathematical Calculations in Java Mrs. C. Furman.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
GUJARAT KNOWLEDGE VILLAGE Faculty Name:- Prof H.M.Patel Students Name:- SONI VISHAL THAKKER BHAVIN ZALA JAYDIP ZALA.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Operators.
1 CS161 Introduction to Computer Science Topic #6.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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.
Programming Principles Operators and Expressions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
Prepared By: K. U. Khimani Assistant Professor IT Department.
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.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CompSci 230 S Programming Techniques
Lecture 3 Java Operators.
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Primitive Data, Variables, Loops (Maybe)
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
OPERATORS (2) CSC 111.
Numerical Data Types.
Java Tokens & Data types
Chapter-3 Operators.
Fundamentals 2.
Expressions and Assignment
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 2: Java Fundamentals
Chapter 3 Operators and Expressions
Chapter 2 Programming Basics.
Building Java Programs
Primitive Types and Expressions
OPERATORS in C Programming
Building Java Programs
OPERATORS in C Programming
Presentation transcript:

Software Technology - I Tools to do operations.....

Types of Operators ● Arithmatic ● Relational ● Logical ● Assignment ● Increment/Decrement ● Conditional ● Bitwise ● Special

Arithmatic Operators

Integer Arithmetic Both operands are integers. int a = 14; int b = 4; int c = 0; c = a - b;// c is 10 c = a + b; // c is 18 c = a * b;// c is 56 c = a / b;// c is 3 c = a % b; // c is 2 The sign of the result of modulo division is alwas the sign of the first operand. Examples: c = -14 % 3;// c is -2 c = -14 % -3; // c is -2 c = 14 % -3;// c is 2 a % b = {a - (a/b) * b}

Real Arithmetic All the operands are real. Result may not be exact! float a = 20.5f, b = 6.4f; float c = 0; c = a + b;// c is 26.9 c = a % b; // c is 1.3 a % b = {a - (a/b) * b} (a/b) : a and b are treated as real numbers and then the result is truncated to get an integer

Mixed-mode Arithmetic When an integer and a floating-point (or double) number are used as operands to a single arithmetic operation, the result is floating point. The integer is implicitly converted to a floating-point number before the operation takes place. int a = 15; System.out.println(a/10.0);// Result is 1.5 System.out.println(a/10);// Result is 1

Relational Operators

Relational Operators (Cont.) class BuyGood { public static void main(String[] args) { float price = 500.0f; float money_with_me = 450.0f; boolean can_buy = price < money_with_me; if(can_buy) { System.out.println("I can buy the good"); } else { System.out.println("I don\'t have enough money"); }

Logical Operators if(age > 55 && salary < 10000) System.out.println("Poor fellow!"); else System.out.println("Somewhat OK");

Assignment Operators int a = 5; a += 1;// Now a is 6 a = a + 1 a -= 2;// a is 4 a = a - 2 a *= 3;// a is 12 a = a * 3 a /= 2;// a is 6 a = a / 2 a %= 4;// a is 2a = a % 4 a += 2 + 3;// a is 7a = a + (2 + 3)

Increment and Decrement Operators class Test { public static void main(String[] args) { int x = 5; int y = 0; y = x++; System.out.println("x is: " + x + ", y is: " + y);// x = 6, y = 5 y = ++x; System.out.println("x is: " + x + ", y is: " + y);// x = 7, y = 7 }

Conditional Operator result = exp1 ? exp2 : exp3; Example: a = 15; b = 10; x = ((a+b) > 10) ? a : a + 10; if((a + b) > 10) { x = a; } else { x = b; }

Bitwise Operators Used to manipulate data types at bit level. Cannot be applied to float of double data types. Suppose you want to check whether the 4 th bit of the value stored in a variable is set (ie. 1) or unset (ie. 0). Here is the code to do it: byte word = 0xAD; if((word & 0x08) > 0) System.out.println("4 th bit is set"); else System.out.println("4 th bit is not set");

Special Operators instanceof Operator Student st1 = new Student(); if (st1 instanceof Student) System.out.println("st1 is an instance of Student class"); else System.out.println("st1 is not an instance of Student class"); Dot Operator Dot operator is used to reference variables and methods of classes and objects. Example: p.setAge(25);

Arithmetic Expressions An expression is a combination of variables, constants and operators which produces a value. AlgebraicJava a - bca - b * c ab/ca * b / c x 2 + 2x + 1x * x + 2 * x + 1 You can use spaces as you wish to improve the readability

Arithmetic Operator Precedence x = * 2 + 1; What's the value of x?

Operator Precedence High Priority Operators: *,/,% Low Priority Operators: +,- Steps in evaluating an expression: 1.Perform the high priority operations from left to right. 2.Perform the low priority operations from left to right.

Operator Precedence (Cont.) Using paranthesis we can change the order in which the expression is evaluated. Expressions inside paranthesis have the highest priority level: / * * / (3 + 3) * (5 - 1) / 6 * (5 - 1) / 6 * *

Operator Precedence (Cont.) If nested paranthesis is used, evaluation starts from the innermost and spreads to the outermost set of paranthesis. 9 - (12 / (3 + 3) * 2) (12 / 6 * 2) (2 * 2)

Type Conversion If different type of operands are involved in an operation, the "lower" type is automatically converted to "higher" type before the opration is done. The result is of the "higher" type. Table 5.9 in the text book shows the type of the result for different types of operands. Why do two operands of byte type produce an integer type result? int x = 50; float y = 245f; int z; z = (int) y / x;// z is 4 float k; k = y / x;// k is 4.9 y / x is a float

Type Casting (Again) Care must be taken when dealing with different types of operands. For example the first output statement may not be the expected result in a scientific research: int flowers = 32245; int trees = 230; System.out.println("Flowers for a tree: " + flowers / trees); System.out.println("Flowers for a tree: " + (float)flowers / trees);

More on Operator Precedence Table 5.11 in the text book provides a complete list of operators with their precedence level and the associativity (ie. the direction of evaluation of the operation). int x = 20; int y = 50; int z = 38; // say // We want to execute some statements // only if z is greater than x and less than y. if(z > x && z < y) {....// Java Statements } What is the correct order of evaluation? What is the correct order of evaluation?

Common Mathematical Functions java.lang.Math class has some methods (static) that perform common mathematical operations. Table 5.12 in the text book summarizes some of them. public class MathTest { public static void main(String[] args) { int first = 0, second = 0; try { first = Integer.parseInt(args[0]); second = Integer.parseInt(args[1]); } catch (Exception e) {} System.out.println(Math.pow(first, second)); }