CPSC 233 Tutorial Xin Jan 24, 2011. Assignment 1 Due on Jan 28 at 4:00 PM Part I Assignment Box on...

17
CPSC 233 Tutorial Xin Jan 24, 2011

Transcript of CPSC 233 Tutorial Xin Jan 24, 2011. Assignment 1 Due on Jan 28 at 4:00 PM Part I Assignment Box on...

Page 1: 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.

CPSC 233 TutorialXin

Jan 24, 2011

Page 2: 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.

Assignment 1 Due on Jan 28 at 4:00 PM

Part I Assignment Box on 2nd floor

Part II Submitted electronically on UNIX Submit

submit -c <course number> -a <assignment number> <name of file or files to be submitted>

eg. submit -c 233 -a 3 README.txt List submitted files

showstuff -c <course number> -a <assignment number>

eg. showstuff -c 233 -a 3 Submit early and update

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

Switch statementchar 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.

Page 6: 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.

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

Page 7: 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.

Common Java Operators / Operator Precedence

Precedence level

Operator Description Associativity

1 expression++

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

Page 8: 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.

Common Java Operators / Operator Precedence

Precedence level

Operator Description Associativity

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

Page 9: 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.

Common Java Operators / Operator Precedence

Precedence level

Operator Description Associativity

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 AND Left to right

9 ^ Bitwise exclusive OR Left to right

Page 10: 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.

Common Java Operators / Operator Precedence

Precedence level

Operator Description Associativity

10 | Bitwise OR Left to right

11 && Logical AND Left to right

12 || Logical OR Left to right

Page 11: 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.

Common Java Operators / Operator Precedence

Precedence level

Operator Description Associativity

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

Page 12: 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.

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

Page 13: 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.

x ++ vs. ++xpublic 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); }}

Page 14: 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.

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

Page 15: 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.

bitwise operations ~ & | ^ << >>

~ 0101------= 1010

0101& 0110------= 0100

0101| 0110------= 0111

0101^ 0110------= 0011

1101 << 1------= 1010

1101 >> 1------= 1110

0101 >> 1------= 0010

Page 16: 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.

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

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

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