1 / 17

CPSC 233 Tutorial

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>

joie
Download Presentation

CPSC 233 Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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’; 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.

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

  7. Common Java Operators / Operator Precedence

  8. Common Java Operators / Operator Precedence

  9. Common Java Operators / Operator Precedence

  10. Common Java Operators / Operator Precedence

  11. Common Java Operators / Operator Precedence

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

  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 • 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

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

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

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

More Related