Presentation is loading. Please wait.

Presentation is loading. Please wait.

Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon.

Similar presentations


Presentation on theme: "Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon."— Presentation transcript:

1 Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon and Umesh Patel Centre for Information and Communication Technology Box Hill Institute of TAFE

2 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Contents at a glance Operators Operators Casting Casting java.lang.System class java.lang.System class in in out out err err Control programming constructs Control programming constructs

3 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Operators Operators perform manipulations on data within expressions, to obtain a result Operators perform manipulations on data within expressions, to obtain a result Operators may be: Operators may be: object, object, arithmetic, arithmetic, logical, logical, comparison and comparison and conversion or cast conversion or cast

4 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Object Operators new eg Student s1 = new Student( ) ; new eg Student s1 = new Student( ) ; dot operator as in ObjectReference. member [. method() ] dot operator as in ObjectReference. member [. method() ] object operators can only access non-private members of another class object operators can only access non-private members of another class

5 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Arithmetic Operators unary +, -, ++, - - unary +, -, ++, - - binary +, -, *, /, % binary +, -, *, /, % precedence BODMAS [ or BOMDAS ] precedence BODMAS [ or BOMDAS ] Beware of division results eg 7/4 = 1 but 7.0/4.0 = 1.75 7 % 3 = 1 while 17.35 % 3.2 = 1.35 Beware of division results eg 7/4 = 1 but 7.0/4.0 = 1.75 7 % 3 = 1 while 17.35 % 3.2 = 1.35 operands in modulo operations should have no sign. Result is –ve if left operand is –ve. operands in modulo operations should have no sign. Result is –ve if left operand is –ve.

6 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Prefix and Postfix Increment/Decrement x++ is postfix (L to R association) x++ is postfix (L to R association) ++x is prefix (R to L association) ++x is prefix (R to L association) ++ and -- takes precedence before other operators eg int x=5, y=7, z ; z = x * ++y; //results in z = 40 ++ and -- takes precedence before other operators eg int x=5, y=7, z ; z = x * ++y; //results in z = 40

7 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Comparison Operators Object type - instanceof Object type - instanceof Equality - == != Equality - == != Ordinal - > >= >= < <= result is boolean literal - true or false

8 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Logical Operators Boolean data type values: true / false Boolean data type values: true / false (5 >= 7) && (3 != -3) F (5 >= 7) && (3 != -3) F (5 >= 7) & (3 != -3) F (5 >= 7) & (3 != -3) F (5 < 7) || (3 != -3) T (5 < 7) || (3 != -3) T (5 < 7) | (3 == -3) T (5 < 7) | (3 == -3) T (5 < 7) ^ (3 == -3) T (5 < 7) ^ (3 == -3) T with s/c without s/c and&&& or||| not! ExOr^

9 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Bitwise Operators All bitwise operators work upon 32-bit numbers. All bitwise operators work upon 32-bit numbers. & | ^ ~ data & mask  data; bit test, bit clearing data | mask  data; bit set, ~data  data ; bit complement data ^ mask  data; selected bit complement & | ^ ~ data & mask  data; bit test, bit clearing data | mask  data; bit set, ~data  data ; bit complement data ^ mask  data; selected bit complement > >>> data > n ; shift data n bits right - sign extend (arith shift) data >>> n ; shift data n bits left - 0 fill (logical shift) > >>> data > n ; shift data n bits right - sign extend (arith shift) data >>> n ; shift data n bits left - 0 fill (logical shift)

10 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute bitwise examples b = 0x05; 0000 0101 (dec 5 ) ( only the 8 lsb shown) b << 2 ; 0001 0100 ( dec 20 ) b = 0x05; 0000 0101 (dec 5 ) ( only the 8 lsb shown) b << 2 ; 0001 0100 ( dec 20 ) int v = -32; v = 0xFFFFFFE0 ; 1 --- 1 1110 0000 (a) v >> 1; 1 – 1 1111 0000 ( -16) (b) v >>> 1; 01 - - -1111 0000 (32,752) int v = -32; v = 0xFFFFFFE0 ; 1 --- 1 1110 0000 (a) v >> 1; 1 – 1 1111 0000 ( -16) (b) v >>> 1; 01 - - -1111 0000 (32,752)

11 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Casting Casting converts one data type into another Casting converts one data type into another Casting that widens is implicit and OK Casting that widens is implicit and OK Casting that shortens must be explicit Casting that shortens must be explicit short a, b, c; a = 5; b = 2; c = (short) (a + b); short a, b, c; a = 5; b = 2; c = (short) (a + b); NOT short a, b, c; a = 5; b = 2; c = a + b; NOT short a, b, c; a = 5; b = 2; c = a + b;

12 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Character data type Uses Unicode eg char ch = ‘a’; [ \u0061 ] Uses Unicode eg char ch = ‘a’; [ \u0061 ] glyphs \n, \t, \r, \”, \\ glyphs \n, \t, \r, \”, \\ char ch = ‘a’; ch +=2; // ch = ‘c’ char ch = ‘a’; ch +=2; // ch = ‘c’ char a=‘A’, b=‘B’, c; c = a + b; //fails but c = (char) (a + b); // is OK char a=‘A’, b=‘B’, c; c = a + b; //fails but c = (char) (a + b); // is OK char c1 = “Z”; // fails char c1 = “Z”; // fails

13 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute java.lang.System Class java core class java core class static (class or shared) objects: static (class or shared) objects: in in out out err err

14 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute java.lang.System Class objectmethodsdevice System.in read() throws exception keyboard System.outprint()println()flush()monitor System.errprint()flush()monitor

15 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute System Class Output stream System.out.print(“ argument to print ”); or System.out.println(“ argument to print ”); Output stream System.out.print(“ argument to print ”); or System.out.println(“ argument to print ”); Input stream System.in.read( ); returns the ordinate (int) value of the pressed key Input stream System.in.read( ); returns the ordinate (int) value of the pressed key

16 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Assignment operators Identical to C Identical to C =, +=, -=, *=, /=, %= =, +=, -=, *=, /=, %= e.g. x *=3; [ triples the value of x ] e.g. x *=3; [ triples the value of x ]

17 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Control Structures Used only within methods of a class Used only within methods of a class Used to control the sequence of operations at runtime Used to control the sequence of operations at runtime The 3 structure types are: The 3 structure types are: sequence sequence decision aka selection or branching decision aka selection or branching repetition aka looping repetition aka looping All program algorithms may be organised as some combination of the 3 structures All program algorithms may be organised as some combination of the 3 structures Nesting is OK Nesting is OK

18 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill InstituteRepetition 3 repetition constructs 3 repetition constructs for for while while do while do while for and while test before loop execution for and while test before loop execution do-while tests after loop execution do-while tests after loop execution Same as in C

19 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Repetition syntax for (int j= 0; j <5; j += 2) System.out.println(“value of j is: ” + j + “”) ; for (int j= 0; j <5; j += 2) System.out.println(“value of j is: ” + j + “”) ; int j = 0; int j = 0; while ( j < 5) { System.out.println(“value of j is: ” + j + “”) ; j += 2; }

20 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Repetition syntax int j = 0; do{ System.out.println(“value of j is: ” + j + “”) ; j += 2; } while ( j < 5) ;

21 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute break; continue; label: break - exits a statement construct break - exits a statement construct continue - returns to the control statement helps with loop control continue - returns to the control statement helps with loop control label: - defines a point in the code sometimes used with break and continue statements, which helps with nested loop control, but should be avoided. label: - defines a point in the code sometimes used with break and continue statements, which helps with nested loop control, but should be avoided.

22 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill InstituteDecision if, if–else, if–else–if, switch, conditional statement if, if–else, if–else–if, switch, conditional statement if (mark > 90) result = “Distinction” ; if (mark >= 75) result = ‘C’; if (mark >= 50) result = ‘P’; else result = ‘N’; result = (mark > 50) ? ‘P’ : ‘N’ ; result = (mark > 50) ? ‘P’ : ‘N’ ;

23 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Decision – switch statement switch (expr) { case val1: statements; break; case val2: statements; break; Default: statements; }

24 December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute switch example 1 switch (month) 1 switch (month) 2 { 2 { 3 case 1: 3 case 1: 4 case 3: 4 case 3: 5 case 5: 5 case 5: 6 case 7: 6 case 7: 7 case 8: 7 case 8: 8 case 10: 8 case 10: 9 case 12: 9 case 12: 10 // months with 31 days 10 // months with 31 days 11 numDays = 31; 11 numDays = 31; 12 break; 12 break; 13 case 4: 13 case 4: 14 case 6: 14 case 6: 15 case 9: 15 case 9: 16 case 11: 16 case 11: 17 // months with 30 days 17 // months with 30 days 18 numDays = 30; 18 numDays = 30; 19 break; 19 break; 20 case 2: 20 case 2: 21 // check for leap year 21 // check for leap year 22 if ( ((year % 4 == 0) && !(year % 100 == 0)) 22 if ( ((year % 4 == 0) && !(year % 100 == 0)) 23 || (year % 400 == 0) ) 23 || (year % 400 == 0) ) 24 numDays = 29; 24 numDays = 29; 25 else 25 else 26 numDays = 28; 26 numDays = 28; 27 break; 27 break; 28 } 28 }


Download ppt "Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon."

Similar presentations


Ads by Google