250 likes | 401 Views
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
E N D
CS 180 Recitation 9/20/07 - 9/21/07
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
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;
Boolean Operators • Boolean expressions can be combined using boolean operators • And && • Or || • Not !
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
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?
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
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?
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
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
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; }
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> }
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
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”);
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?
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?
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.
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
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?
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?
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’ ?
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 */
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.
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; } }