1 / 35

CS 200 Branches

CS 200 Branches. Jim Williams, PhD. This Week. Piazza: notes Consulting Hours: Request Help Queue Team Lab: take paper, draw pictures Thursday: P3 due Pre-Exam 1: bring WISC ID, #2 Pencils 6 questions, 15 minutes Lecture: Using Objects, Branches. Primitive vs Reference Data Types.

arleenl
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: • P3 due • Pre-Exam 1: bring WISC ID, #2 Pencils • 6 questions, 15 minutes • Lecture: Using Objects, Branches

  3. Primitive vs Reference Data Types Reference • Store a reference to another location in memory that contains value Integer num; num = new Integer(9); Primitive • Store value int i = 5; num i 5 9

  4. Wrapper Classes • Wrapper classes contain helpful methods.

  5. Data Type Conversions String weekNum = 3; String weekNum = "" + 3; String numStr = Integer.toString( 4); int num = Integer.parseInt( numStr);

  6. Wrapper Classes • Boxing: Putting primitive into instance of wrapper class • Unboxing: retrieve primitive from instance • auto-boxing/unboxing: when compiler inserts code to box/unbox.

  7. Primitive vs Reference Variables int num1; num1 = 5; Integer num2; num2 = new Integer(7); Integer num3 = 9; int num4 = num3; //use Java Visualizer, options Show String/Integer objects checkbox

  8. What will show when d3 is printed? Double d1 = new Double(10); double d2 = d1; d1 = 14.1; Double d3 = d1; d1 = d2; System.out.println( d3);

  9. String class • A reference (class), not a primitive data type. • Frequently used final String TITLE_PREFIX = "Welcome to Week "; System.out.println( TITLE_PREFIX + 4);

  10. String String name2 = "Alex"; name2 = name2.toLowerCase(); String name3 = name2 + " Johnson"; System.out.print( name3); • instances of String are immutable (cannot change) • methods in String class that one might guess to change strings actually return new strings with changes.

  11. Calling String methods String strA = "This is a string"; char aChar = strA.charAt( 3);

  12. java.util.Random Random randGen; //Declare reference variable randGen = new Random(); //create instance // randGen.setSeed( 123); //set state int valueA = randGen.nextInt( 5); //get a value int valueB = randGen.nextInt( 5); int valueC = randGen.nextInt( 5);

  13. What is the answer? What expression to get a value between 2 and 10, inclusive of both 2 and 10? Assume: Random rand = new Random(); int x;

  14. Best statement? What is the best statement to get a value between 2 and 10, inclusive of both 2 and 10? Assume: Random rand = new Random(); int x; //x = ???

  15. What are values of variables? 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();

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

  17. Eclipse IDE

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

  19. 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 ?

  20. What is result? boolean result = flag = false || flag; Truth Table:

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

  22. 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 }

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

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

  25. 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”);

  26. 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'; }

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

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

  29. Comparing: == vs equals() • Primitive data types • use == for comparing primitive values • No equals() method for primitive data types • (but there is equals() method in wrapper classes) • 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.

  30. 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));

  31. 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);

  32. 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));

  33. 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 + "#");

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

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

More Related