L5:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.

31
L5:CSC210 2014-2015 © Dr. Basheer M. Nasef Welcome to Object Oriented With JAVA Lecture #5 By Dr. Basheer M. Nasef

Transcript of L5:CSC210 2014-2015 © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.

  • Slide 1

L5:CSC210 2014-2015 Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef Slide 2 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 2 Control Statements II Slide 3 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 3 F To implement selection control using switch statements. F To write expressions using the conditional operator. F To display formatted output using the System.out.printf method and to format strings using the String.format method. F (GUI) To get user confirmation using confirmation dialogs. Slide 4 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 4 switch Statement Flow Chart Slide 5 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 5 switch Statements Slide 6 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 6 switch Statements Slide 7 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 7 switch Statement Rules switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; case valueN: statement(s)N; break; default: statement(s)-for-default; } The switch-expression must yield a value of char, byte, short, or int type and must always be enclosed in parentheses. Slide 8 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 8 switch Statement Rules switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; case valueN: statement(s)N; break; default: statement(s)-for-default; } The value1,..., and valueN must have the same data type as the value of the switch-expression. The resulting statements in the case statement are executed when the value in the case statement matches the value of the switch- expression. Slide 9 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 9 switch Statement Rules switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; case valueN: statement(s)N; break; default: statement(s)-for-default; } Note that value1,..., and valueN are constant expressions, meaning that they cannot contain variables in the expression, such as 1 + x. Slide 10 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 10 switch Statement Rules The keyword break is optional, but it should be used at the end of each case in order to terminate the remainder of the switch statement. If the break statement is not present, the next case statement will be executed. switch (switch-expression) { case value1: statement(s)1; break; case value2: statement(s)2; break; case valueN: statement(s)N; break; default: statement(s)-for-default; } The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. The case statements are executed in sequential order, but the order of the cases (including the default case) does not matter. However, it is good programming style to follow the logical sequence of the cases and place the default case at the end. Slide 11 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 11 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Suppose ch is 'a': Slide 12 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 12 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } ch is 'a': Slide 13 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 13 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Execute this line Slide 14 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 14 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Execute this line Slide 15 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 15 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Execute this line Slide 16 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 16 Trace switch statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Next statement; Execute next statement Slide 17 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 17 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } Suppose ch is 'a': Slide 18 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 18 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } ch is 'a': Slide 19 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 19 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } Execute this line Slide 20 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 20 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } Execute this line Slide 21 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 21 Trace switch statement switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break ; case 'c': System.out.println(ch); } Next statement; Execute next statement Slide 22 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 22 Using Strings in switch Statements you can use a String object in the switch statement's expression. The following code example, StringSwitchDemo, displays the number of the month based on the value of the String named month:StringSwitchDemo SwitchCaseGrade Slide 23 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 23 Conditional Operator if (x > 0) y = 1 Else y = -1; is equivalent to y = (x > 0) ? 1 : -1; (booleanExpression) ? expression1 : expression2 Slide 24 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 24 Conditional Operator Suppose you want to assign the larger number between variable num1 and num2 to max. You can simply write a statement using the conditional expression: max = (num1 > num2) ? num1 : num2; if (num1>num2) System.out.println(num1 + is max); else System.out.println(num2 + is max); System.out.println( (num1>num2)? num1 + is max : num2 + is max); Slide 25 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 25 Conditional Operator if (num % 2 == 0) System.out.println(num + is even); else System.out.println(num + is odd); System.out.println( (num % 2 == 0)? num + is even : num + is odd); Slide 26 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 26 Formatting Output Use the new JDK 1.5 printf statement. System.out.printf(format, items); Where format is a string that may consist of substrings and format specifiers. A format specifier specifies how an item should be displayed. An item may be a numeric value, character, boolean value, or a string. Each specifier begins with a percent sign. Slide 27 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 27 Frequently-Used Specifiers Specifier OutputExample %b a boolean value true or false %c a character 'a' %d a decimal integer 200 %f a floating-point number 45.460000 %e a number in standard scientific notation 4.556000e+01 %s a string "Java is cool" Slide 28 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 28 Operator Precedence var++, var-- +, - (Unary plus and minus), ++var, --var (type) Casting ! (Not) *, /, % (Multiplication, division, and remainder) +, - (Binary addition and subtraction), >= (Comparison) ==, !=; (Equality) ^ (Exclusive OR) && (Conditional AND) Short-circuit AND || (Conditional OR) Short-circuit OR =, +=, -=, *=, /=, %= (Assignment operator) Slide 29 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 29 Operator Precedence and Associativity The expression in the parentheses is evaluated first. (Parentheses can be nested, in which case the expression in the inner parentheses is executed first.) When evaluating an expression without parentheses, the operators are applied according to the precedence rule and the associativity rule. If operators with the same precedence are next to each other, their associativity determines the order of evaluation. All binary operators except assignment operators are left-associative. Slide 30 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 30 Operator Associativity When two operators with the same precedence are evaluated, the associativity of the operators determines the order of evaluation. All binary operators except assignment operators are left-associative. a b + c d is equivalent to ((a b) + c) d Assignment operators are right-associative. Therefore, the expression a = b += c = 5 is equivalent to a = (b += (c = 5)) Slide 31 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 31 Example Applying the operator precedence and associativity rule, the expression 3 + 4 * 4 > 5 * (4 + 3) - 1 is evaluated as follows: Slide 32 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 32 Operand Evaluation Order Supplement III.A, Advanced discussions on how an expression is evaluated in the JVM. Slide 33 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 33 (GUI) Confirmation Dialogs int option = JOptionPane.showConfirmDialog (null, "Continue"); Slide 34 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 34 Problem: Guessing Birth Day GuessBirthDateUsingConfirmationDialog The program can guess your birth day. Run to see how it works. Slide 35 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 35 Problem What is y after the following switch statement is executed? x = 3; y = 3; switch (x + 3) { case 6: y = 1; default: y += 1; } Slide 36 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 36 Problem Use a switch statement to rewrite the following if statement and draw the flow chart for the switch statement: if (a == 1) x += 5; else if (a == 2) x += 10; else if (a == 3) x += 16; else if (a == 4) x += 34; Slide 37 L5:CSC210 2014-2015 Dr. Basheer M. Nasef 37 Problem: (Sorting three integers) Write a program that sorts three integers. The integers are entered from the input dialogs and stored in variables n1, n2, and n3, respectively. The program sorts the numbers so that n1 n2 n3.