Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Software Technology - I Tools to do operations....."— Presentation transcript:

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

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

3 Arithmatic Operators

4 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}

5 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

6 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

7 Relational Operators

8 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"); }

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

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

11 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 }

12 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; }

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

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

15 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

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

17 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.

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

19 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) - 1 9 - (12 / 6 * 2) - 1 9 - (2 * 2) - 1 9 - 4 - 1 5 - 1 4

20 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

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

22 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?

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


Download ppt "Software Technology - I Tools to do operations....."

Similar presentations


Ads by Google