1 / 25

CS 180 Recitation 9/20/07 - 9/21/07

CS 180 Recitation 9/20/07 - 9/21/07. Announcements. Exam 1 is Tuesday, September 25 th Rooms: Sec 0101-0401 WTHR 104 Sec 0501-0801 MATH 175 Time: 7:00 pm If you have a conflict with the exam, contact Dr. Van Zandt Sign the Academic Integrity Policy!! Mentoring with Armand Navabi

brent-ford
Download Presentation

CS 180 Recitation 9/20/07 - 9/21/07

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 180 Recitation 9/20/07 - 9/21/07

  2. Announcements • Exam 1 is Tuesday, September 25th • Rooms: • Sec 0101-0401 WTHR 104 • Sec 0501-0801 MATH 175 • Time: 7:00 pm • If you have a conflict with the exam, contact Dr. Van Zandt • Sign the Academic Integrity Policy!! • Mentoring with Armand Navabi • Mondays 7-9 pm in LWSN B134

  3. Boolean Expressions • boolean is a primitive data type • Two values: true or false • Compares two values using a relational operator • <, >, ==, <=, >=, != • Examples • boolean isOK = true; • boolean isLarge = num >= 100;

  4. Boolean Operators • Boolean expressions can be combined using boolean operators • And && • Or || • Not !

  5. Boolean Operators • bool1 && bool2 • true if bool1 AND bool2 are true • false if either bool1 or bool2 is false • bool1 || bool 2 • true if either bool1 OR bool2 is true • false if both bool1 AND bool2 are false • !bool1 • true if bool1 is false • false if bool1 is true

  6. Boolean Operator Examples int x = 20; boolean isValid = x > 0 && x < 100; int x = 5; boolean isValid = x > 0 || x < -100; int x = 5; int y = 10; boolean isDouble = (y == (x * 2)); What are the results of the above expressions?

  7. Operator Precedence • Parenthesis should be used to indicate the order of operations • When there are no parenthesis, operator precedence is followed • Higher precedence preformed before lower precedence • Equal precedence performed left-to-right, except for unary operations which are performed right-to-left

  8. Operator Precedence Rules

  9. Operator Precedence Examples int x = 20 + 5 * 10; int y = (20 + 5) * 10; boolean w = true && true || false && false; boolean z = true && (true || false) && false; • What are the values of x, y, w, and z?

  10. Selection Statements • Selection statements change the flow of control in a program • Use when the action to be preformed is dependent on the answer to a question • E.g. If the value of x is less than zero, print an error message. Otherwise add 1

  11. If-else Statement • A branching statement used to choose between two actions if( <Boolean expression> ) <then block> else <else block> • The else branch is executed only when the if condition evaluates to false • It is optional

  12. If-else Statement • To enclose multiple statements in a branch, enclose the branch in braces if( <Boolean expression> ) { line 1; line 2; line 3; } else { line 4; line 5; }

  13. If-else Statement • Single line blocks are not required to be enclosed in braces, but it is a good idea if( <Boolean expression> ) <then block> else <else block> Is equivalent to if( <Boolean expression> ) { <then block> } else { <else block> }

  14. Multibranch If-else Statement if( <Boolean Expression 1> ) statement 1 elseif ( <Boolean Expression 2> ) statement 2 else if ( <Boolean Expression 3> ) statement 3 else if ( <Boolean Expression 4> ) statement 4 else Default Statement

  15. If-else Statement Examples int grade = 11; if (grade < 5 || grade > 10) grade++; else grade--; int count = 70; if (count >= 90 && count < 100) System.out.println(“A”); else if(count >= 80) System.out.println(“B”); else if(count >= 70) System.out.println(“C”); else if(count >= 60) System.out.println(“D”); else System.out.println(“F”);

  16. If-else Statement Examples int grade = 5; if(grade < 5) grade++; grade = 10; int grade = 5; if(grade < 5) { grade++ grade = 10; } • What is the value of grade in each statement?

  17. If-else Statement Examples int grade = 5; if(grade < 5) ; grade = 10; int grade = 5; if(grade > 0) grade++; if(grade >= 5) grade++; else grade = 0; • What is the value of grade in each statement?

  18. Dangling else if( <Boolean expression> ) <then block> if( <Boolean expression> ) <then block> else <else block> • Which if does the else match? • Design decision. Java matches it to the most recent if.

  19. Switch Statements • Switch statements are a multiway branch which makes its decision based on an integer expression • char, byte, short, or int • A list of cases is searched until a match is found • If no match is found, the default case is executed • Optional

  20. Switch Statement Syntax switch(Controlling_Expression) { case Label1: statement(s); <break;> case Label2: statement(s); <break;> <default:> statement(s); } • The breaks and the default case label above are optional • What happens if we take the breaks out?

  21. Switch Statement Examples int section = 7; int room = 0; switch(section) { case 1: room = 101; break; case 7: room = 102; break; case 5: room = 103; break; default: System.out.println(“invalid”); } int section = 7; int room = 0; switch(section) { case 1: room = 101; break; case 7: room = 102; case 5: room = 103; default: System.out.println(“invalid”); } • What are the results of the above statements?

  22. Switch Statement Examples char gender = ‘f’ switch(gender) { case ‘f’: case ‘F’: System.out.println(“female”); break; case ‘m’: case ‘M’: System.out.println(“male”); break; } • What is the result of the above statement? • Notice the empty case bodies • What if gender = ‘x’ ?

  23. Unicode Encoding • The Unicode Worldwide Character Standard (Unicode) supports the interchange, processing, and display of the written texts of diverse languages. • A UNICODE character takes up two bytes. ASCII characters take up one byte. char ch1 = ‘X’; System.out.println(ch1); /* output X */ System.out.println((int) ch1); /* output 88 */

  24. Character Processing Declaration and Initialization char ch1, ch2 = ‘X’; System.out.println(“ASCII code of character X is “ + (int) ‘X’); System.out.println(“Character with ASCII code 88 is “ + (char) 88); Type conversion between int and char. Comparison returns true because ASCII value of ‘A’ is 65 while that of ‘c’ is 99. ‘A’ < ‘c’ if( ch1 < ‘A’ && ch2 == 99 ) System.out.println(“Done”); Can compare characters and numbers.

  25. compareTo method for 2D points private double myx, myy; public static int compareTo(double x, double y) { double mydistance = Math.sqrt(Math.pow(myx, 2) + Math.pow(myy, 2)); double distance = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); if(mydistance < distance) { return -1; } else if(mydistance == distance) { return 0; } else { return 1; } }

More Related