Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 233 Tutorial Xin Jan 24, 2011. Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on.

Similar presentations


Presentation on theme: "CPSC 233 Tutorial Xin Jan 24, 2011. Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on."— Presentation transcript:

1 CPSC 233 Tutorial Xin Jan 24, 2011

2 Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on UNIX Submit submit -c -a eg. submit -c 233 -a 3 README.txt List submitted files showstuff -c -a eg. showstuff -c 233 -a 3 Submit early and update

3 Keyboard Input Again Scanner.nextLine() will pick up any leftovers If Scanner.nextInt() was called previously, nextLine() usually pickup a carriage-return (CR) use an extra nextLine() to get rid of the CR Experiments with MyInput.java Input 20 and see the results Input 20 abc and see the results

4 Pick up chars String str = in.nextLine(); char c = str.charAt(0); What other methods String has? Search in Google with class String java API using documents provided by oracle.com

5 Switch statement char x = ‘a’; int val; switch (x) { case ‘a’: val = 0; // fall through case ‘b’: val = 1; break; case ‘c’: // fall through case ‘C’: // do the same for ‘c’ and ‘C’ val = 3; break; default: val = 4; } Fall through: Without an explicit break statement, the execution continues on the next case!!! This can be used to do the same thing for a few values.

6 Operator precedence An example int v = 92 int x = 100 | 25 & 36 << 2 + 12 & 55 * ++ v; = 100 | ((25 & (36 << (2 + 12))) & (55 * (++ v))) Extremely confusing Sometimes results are dependent on the specific complier (for c/c++) Avoid by all means Clarify with parentheses until it is easy to understand by common humans

7 Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 1expression++ expression-- Post-increment Post-decrement Right to left 2++expression --expression + - ! ~ (type) Pre-increment Pre-decrement Unary plus Unary minus Logical negation Bitwise complement Cast Right to left

8 Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 3*/%*/% Multiplication Division Remainder/modulus Left to right 4+-+- Addition or String concatenation Subtraction Left to right 5<< >> Left bitwise shift Right bitwise shift Left to right

9 Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 6< <= > >= Less than Less than, equal to Greater than Greater than, equal to Left to right 7= != Equal to Not equal to Left to right 8&Bitwise ANDLeft to right 9^Bitwise exclusive ORLeft to right

10 Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 10|Bitwise ORLeft to right 11&&Logical ANDLeft to right 12||Logical ORLeft to right

11 Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 13= += -= *= /= %= &= ^= |= <<= >>= Assignment Add, assignment Subtract, assignment Multiply, assignment Division, assignment Remainder, assignment Bitwise AND, assignment Bitwise XOR, assignment Bitwise OR, assignment Left shift, assignment Right shift, assignment Right to left

12 x ++ vs. ++x public class IncOperators { public static void main (String [] args) { int x = 8; System.out.println("x = " + x); int returnedValue = ++ x; System.out.println("the returned value is: " + returnedValue); System.out.println("x after the operation is: " + x); }

13 x ++ vs. ++x public class Order2 { public static void main (String [] args) { int num1; int num2; num1 = 5; num2 = ++num1 * num1++; System.out.println("num1=" + num1); System.out.println("num2=" + num2); }

14 type cast Double  int float x = 5; // OK float x = 5.0;// Illegal float x = 5.f; // OK int x = 5.0; // Illegal int x = (int) 5.f; // OK double x = 5 / 2; // x = 2 double x = 5 / (double)2; // x = 2.5 char  int char c = 97; // OK int x = ‘a’; // OK char c = ‘a’ + 1; // OK

15 bitwise operations ~ & | ^ > ~ 0101 ------ = 1010 0101 & 0110 ------ = 0100 0101 | 0110 ------ = 0111 0101 ^ 0110 ------ = 0011 1101 << 1 ------ = 1010 1101 >> 1 ------ = 1110 0101 >> 1 ------ = 0010

16 Showing integers in binary public class ShowBinary { public static void main (String [] args) { showInt(3); } public static void showInt(int x) { for (int i = 0; i < 32; i ++) { if ((x & 0x80000000) == 0) System.out.print("0"); else System.out.print("1"); x = x << 1; }

17 Experiments with the program public static void main (String [] args) { int x = 3; System.out.println("x = " + x + " in binary: "); showInt(x); System.out.println(); int y = -2011; System.out.println("y = " + y + " in binary: "); showInt(y); System.out.println(); System.out.println("x AND y in binary: "); showInt(x & y); System.out.println(); }


Download ppt "CPSC 233 Tutorial Xin Jan 24, 2011. Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on."

Similar presentations


Ads by Google