1 / 34

CS 200 Branches

Stay updated with CS200 branches through weekly updates, Piazza notes, consulting hours, team lab activities, pre-exam preparation, and lecture materials.

pascual
Download Presentation

CS 200 Branches

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. CS 200 Branches Jim Williams, PhD

  2. This Week • Piazza: notes • Consulting Hours: Request Help Queue • Team Lab: take paper, draw pictures • Thursday: • 3 parts to P3 due • Pre-Exam 1: bring WISC ID, #2 Pencils • 6 questions, 15 minutes • Lecture: Using Objects, Branches

  3. Read In Values Using Scanner Whitespace: space, tab, newline Token: a sequence of non-whitespace characters. 14 23.0 hello This, is a sentence. 2nd line 3rd line 43

  4. Some Scanner Methods • Read next token: nextInt(), nextDouble(), next() • Read through next newline and return everything read prior to newline. nextLine()

  5. What is the value in name? String note = "1 \n2\nAlex two words"; Scanner input = new Scanner( note); int num1 = input.nextInt(); int num2 = input.nextInt(); String words = input.nextLine(); String name = input.next();

  6. What are values of variables? String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); String major = input.nextLine();

  7. What are values of variables? String note = "Minsub\n22\nCS"; Scanner input = new Scanner( note); String name = input.nextLine(); int age = input.nextInt(); input.nextLine(); //read and ignore rest of line String major = input.nextLine();

  8. What are values of variables? String note = "20 25\nscores"; Scanner input = new Scanner( note); int score1 = input.nextInt(); int score2 = input.nextInt(); String title = input.nextLine();

  9. What is value in str3? String note = " C S\n2 \n33\n end\n"; Scanner input = new Scanner( note); String str1 = input.nextLine(); int num1 = input.nextInt(); String str2 = input.next(); String str3 = input.nextLine();

  10. String class • A reference type (instantiable class), not a primitive data type. final String TITLE_PREFIX = "Week "; String title = TITLE_PREFIX + 3; title = "new title"; System.out.println( title); • Draw a picture

  11. String String name2 = "Alex"; name2.toUpperCase(); String name3 = name2 + " Johnson"; System.out.print( name3); • instances of String are immutable(cannot change) • methods in String classthat appear may change strings actuallyreturn new strings.

  12. What does aChar contain? String strA = "This is a string"; char aChar = strA.charAt( 3);

  13. Relational and Equality Operators < <= > >= == != int n = 6; boolean result = n != 5;

  14. Logical Operators && (AND) both sides true to be true, else false || (OR) either side true then true, else false ! (NOT) if true then false, and vice-versa Is !(a && b) equivalent to !a || !b ?

  15. What is returned value? static boolean something(boolean flag) { return !flag != false; } Truth Table:

  16. !(a && b) equivalent? !a || !b Truth Table:

  17. null Literal null • a literal that can be assigned to any reference variable. Means no-reference or refers to nothing. • Example: String str = null;

  18. What happens? String str = null; char ch = str.charAt(1);

  19. Comparing: == vs equals() • Primitive data types • use == for comparing primitive values • No equals() method for primitive data types • Reference data types • use == for comparing references • use equals() for comparing instance/object contents • The meaning of equals() depends on the class it is defined in.

  20. What is printed out? int i = 6; int j = 6; System.out.println( i == j); System.out.println( i.equals( j)); Integer k = new Integer( 7); Integer m = new Integer( 7); System.out.println( k == m); System.out.println( k.equals( m));

  21. Values of a1 and a2? char ch1 = 'H'; String str = "Hello"; char ch2 = str.charAt(0); boolean a1 = ch1 == ch2; boolean a2 = ch1.equals(ch2);

  22. String: == vs equals() String str1 = "hello"; String str2 = "hello"; String str3 = new String("hello"); String str4 = "hello"; System.out.println( str1 == "hello" ); System.out.println( str1 == str2); System.out.println( str1.equals("hello")); System.out.println( str3 == str1); System.out.println( str3.equals( str2));

  23. Thursday • preExam, lecture Branches • Sit in columns with space between (if possible)

  24. Eclipse IDE

  25. Are these equivalent? boolean tired = true; if ( tired) { //take break tired = false; } if ( !tired) { //keep working } boolean tired = true; if ( tired) { //take break tired = false; } else { //keep working }

  26. Floating Down a River http://www.thehiltonorlando.com/discover/pools-and-lazy-river/

  27. Side Trip, maybe multiple side trips boolean tired = true; if ( tired) { System.out.println(“take break”); }

  28. One side of the Island or the other boolean sunny = false; if ( sunny) { System.out.print(“sunny”); } else { System.out.print(“not sunny”); } System.out.println( “ and back together”);

  29. Equivalent? char chr = //any valid char out = 'W'; if ( chr == 'A') { out = 'X'; } if ( chr == 'B') { out = 'Y'; } if ( chr != 'A' || chr != 'B') { out = 'Z'; } char chr = //any valid char out = 'W'; if ( chr == 'A') { out = 'X'; } else if ( chr == 'B') { out = 'Y'; } else { out = 'Z'; }

  30. Switch: What is printed out? char choice = 'a'; switch (choice) { case 'a': System.out.print("a"); case 'b': System.out.print("b"); break; default: System.out.print("other"); break; }

  31. What is the value of msg? boolean flag = true; String msg = "before if"; if ( flag) { msg = "" + flag; }

  32. Scanner Scanner input = new Scanner("1 \ntwo \n 2\n\n"); int a = input.nextInt(); if ( input.hasNextInt()) { int b = input.nextInt(); } else { input.nextLine(); } String line = input.nextLine(); System.out.println("#" + line + "#");

  33. Scanner String note = "Hmm\na \na\n3\n\nline note."; Scanner input = new Scanner( note); int num = 0; String str1 = input.nextLine(); String str2 = input.next(); if ( input.hasNextInt()) { num = input.nextInt(); } String str4 = input.nextLine();

  34. Debugging with Print statements See what is going on. Divide and conquer.

More Related