170 likes | 271 Views
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 UNIX Submit submit - c < course number> -a <assignment number> <name of file or files to be submitted>
E N D
CPSC 233 Tutorial Xin Jan 24, 2011
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
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
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
Switch statement char x = ‘a’; intval; 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.
Operator precedence • An example • intv = 92 • intx = 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
x ++ vs. ++x public class IncOperators { public static void main (String [] args) { intx = 8; System.out.println("x = " + x); intreturnedValue = ++ x; System.out.println("the returned value is: " + returnedValue); System.out.println("x after the operation is: " + x); } }
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); } }
type cast • Double int • float x = 5; // OK • float x = 5.0;// Illegal • float x = 5.f; // OK • intx = 5.0; // Illegal • intx = (int) 5.f; // OK • double x = 5 / 2; // x = 2 • double x = 5 / (double)2; // x = 2.5 • char int • char c = 97; // OK • intx = ‘a’; // OK • char c = ‘a’ + 1; // OK
bitwise operations • ~ & | ^ << >> 0101 & 0110 ------ = 0100 0101 | 0110 ------ = 0111 0101 ^ 0110 ------ = 0011 ~ 0101 ------ = 1010 1101 >> 1 ------ = 1110 0101 >> 1 ------ = 0010 1101 << 1 ------ = 1010
Showing integers in binary public class ShowBinary { public static void main (String [] args) { showInt(3); } public static void showInt(intx) { for (inti = 0; i < 32; i ++) { if ((x & 0x80000000) == 0) System.out.print("0"); else System.out.print("1"); x = x << 1; } } }
Experiments with the program public static void main (String [] args) { intx = 3; System.out.println("x = " + x + " in binary: "); showInt(x); System.out.println(); inty = -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(); }