Title: Software Technology - I
1Software Technology - I
- Tools to do operations.....
2Types of Operators
- Arithmatic
- Relational
- Logical
- Assignment
- Increment/Decrement
- Conditional
- Bitwise
- Special
3Arithmatic Operators
4(No Transcript)
5(No Transcript)
6Mixed-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
7Relational Operators
8Relational Operators (Cont.)
class BuyGood public static void main(String
args) float price 500.0f float
money_with_me 450.0f boolean can_buy
price lt money_with_me if(can_buy)
System.out.println("I can buy the
good") else System.out.println("I
don\'t have enough money")
9Logical Operators
if(age gt 55 salary lt 10000) System.out.println
("Poor fellow!") else System.out.println("Somewh
at OK")
10Assignment 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 2 a a 4 a 2
3 // a is 7 a a (2 3)
11Increment 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(No Transcript)
13Bitwise 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 4th 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) gt
0) System.out.println("4th bit is
set") else System.out.println("4th bit is not
set")
14Special Operators
instanceof Operator Student st1 new
Student() if (st1 instanceof Student) System.o
ut.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(No Transcript)
16Arithmetic Operator Precedence
x 2 3 2 1 What's the value of x?
17Operator Precedence
- High Priority Operators
- , /,
- Low Priority Operators
- , -
- Steps in evaluating an expression
- Perform the high priority operations from left to
right. - Perform the low priority operations from left to
right.
18Operator Precedence (Cont.)
9 - 12 / 3 3 2 - 1 9 - 4 3 2 - 1 9 - 4
6 - 1 5 6 - 1 11 - 1 10
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) (5 - 1) 9 - 12 / 6 (5 - 1) 9
- 12 / 6 4 9 - 2 4 9 - 8 1
19Operator 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(No Transcript)
21Type 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("Fl
owers for a tree " (float)flowers /
trees)
22More 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).
What is the correct order of evaluation?
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 gt x z lt
y) ....// Java Statements
23Common 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(args0)
second Integer.parseInt(args1) catch
(Exception e) System.out.println(Math.pow(fi
rst, second))