Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 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  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 What are the results of the above expressions? 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));

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 What are the values of x, y, w, and z? int x = 20 + 5 * 10; int y = (20 + 5) * 10; boolean w = true && true || false && false; boolean z = true && (true || false) && false;

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 ( ) else 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 ( ) { 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 ( ) else Is equivalent to if ( ) { } else { }

14 Multibranch If-else Statement if ( ) statement 1 else if ( ) statement 2 else if ( ) statement 3 else if ( ) statement 4 else Default Statement

15 If-else Statement Examples int grade = 11; if (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 What is the value of grade in each statement? int grade = 5; if(grade < 5) grade++; grade = 10; int grade = 5; if(grade < 5) { grade++ grade = 10; }

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

18 Dangling else Which if does the else match? Design decision. Java matches it to the most recent if. if ( ) if ( ) else

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 The breaks and the default case label above are optional What happens if we take the breaks out? switch(Controlling_Expression) { case Label1: statement(s); case Label2: statement(s); statement(s); }

21 Switch Statement Examples What are the results of the above statements? 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”); }

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

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 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); ‘A’ < ‘c’ if ( ch1 < ‘A’ && ch2 == 99 ) System.out.println(“Done”); Declaration and Initialization Type conversion between int and char. Comparison returns true because ASCII value of ‘A’ is 65 while that of ‘c’ is 99. 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; }


Download ppt "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."

Similar presentations


Ads by Google