Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter p 1 Introduction to Computers, p, Programs, g, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Selection Statements Chapter 4 Loops Chapter 5 Methods 19.1-19.3 in Chapter 19 Recursion Chapter C ap e 6 Arrays ays Chapter 23 Algorithm Efficiency and Sorting Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 1
To declare boolean type and write Boolean expressions ( 3.2). To distinguish between conditional and unconditional && and operators ( 3.2.1) ( 3 2 1). To use Boolean expressions to control selection statements ( 3.33.5). To implement selection control using if and nested if statements ( 3.3). To T implement i l selection l i controll using i switch i h statements ( 3.4) ( 3 4). To write expressions using the conditional operator ( 3.5). To display formatted output using the System.out.printf System out printf method and to format strings using the String.format method ( 3.6). To know the rules governing operand evaluation order, operator precedence, and operator associativity ( 3.7-3.8). Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-222158-6 2
Often in a program you need to compare two values, such as whether i is greater than j. Java provides six comparison operators (also known as relational operators) that can be used to compare two values. The result of the comparison is a Boolean value: true or false. boolean b = (1 > 2); int i =0; boolean b2 = i; //compile error rights reserved. 0-13-222158-6 3
Operator Name < less than <= less than or equal to > greater than >= greater than or equal to == equal to!= not equal to rights reserved. 0-13-222158-6 4
Operator Name! not && and or ^ exclusive or rights reserved. 0-13-222158-6 5
p!p true false false true Example!(1 > 2) is true, because (1 > 2) is false.!(1 > 0) is false, because (1 > 0) is true. Truth Table for Operator && p1 p2 p1 && p2 false false false false true false true false false true true true Example (3 > 2) && (5 >= 5) is true, because (3 > 2) and (5 >= 5) are both true. (3 > 2) && (5 > 5) is false, because (5 > 5) is false. rights reserved. 0-13-222158-6 6
p1 p2 p1 p2 false false false false true true true false true true true true Example (2 > 3) (5 > 5) is false, because (2 > 3) and (5 > 5) are both false. (3 > 2) (5 > 5) is true, because (3 > 2) is true. Truth Table for Operator ^ p1 p2 p1 ^ p2 false false false false true true true false true true true false Example (2 > 3) ^ (5 > 1) is true, because (2 > 3) is false and (5 > 1) is true. (3 > 2) ^ (5 > 1) is false, because both (3 > 2) and (5 > 1) are true. rights reserved. 0-13-222158-6 7
System.out.println("Is " + num + " divisible by 2 and 3? " + ((num % 2 == 0) && (num % 3 == 0))); System.out.println("Is " + num + " divisible by 2 or 3? " + ((num % 2 == 0) (num % 3 == 0))); System.out.println("Is " + num + " divisible by 2 or 3, but not both? " + ((num % 2 == 0) ^ (num % 3 == 0))); LeapYear AdditionTutor rights reserved. 0-13-222158-6 8
&&: conditional AND operator, short-circuit AND operator : Ex:(p1) && (p2) p1=true, p2 evaluate p1=false, p2 no evaluate : conditional OR operator, short-circuit OR operator: Ex:(p1) (p2) p1=false, p2 evaluate p1=true, p2 no evaluate -&: unconditional AND operator, : unconditional OR operator Works exactly the same as the, && Always evaluate both operand (x!=0) & (100/x >0) :what happen if x=0//runtime error rights reserved. 0-13-222158-6 9
If x is 1, what is x after this expression? (x > 1) & (x++ < 10) If x is 1, what is x after this expression? (x > 1) && (x++ < 10) If x is 1, How about (1 == x) (10 > x++)? - (1 == x) (10 > x++)? & and, ^ operator can also apply to bitwise operations int x =2, y=6; //010,110 int z = x&y; // 010 z=2 rights reserved. 0-13-222158-6 10
if Statements switch Statementst t Conditional Operators rights reserved. 0-13-222158-6 11
if (booleanexpression) { statement(s); } if (radius >= 0) { area = radius * radius * PI; System.out.println("The t area" + " for the circle of radius " +radius+" + is " + area); } Boolean Expression false (radius >= 0) false true true Statement(s) area = radius * radius * PI; System.out.println("The area for the circle of " + "radius " + radius + " is " + area); (A) (B) rights reserved. 0-13-222158-6 12
Outer parentheses required ( ) Braces can be omitted if the block contains a single statement,{ } if ((i > 0) && (i < 10)) { System.out.println("i is an " + + "integer between 0 and 10"); } (a) Equivalent if ((i > 0) && (i < 10)) System.out.println("i is an " + + "integer between 0 and 10"); (b) rights reserved. 0-13-222158-6 13
Adding a semicolon at the end of an if clause is a common mistake. if (radius >= 0); { Wrong area = radius*radius*pi; } System.out.println( println("the area for the circle of radius " + radius + " is " + area); This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error. This error often occurs when you use the next-line block style. rights reserved. 0-13-222158-6 14
if (booleanexpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; } true Statement(s) for the true case Boolean Expression false Statement(s) for the false case rights reserved. 0-13-222158-6 15
if (radius >= 0) { area = radius * radius * 3.14159; System.out.println( println("the area for the + circle of radius " + radius + " is " + area); } else { System.out.println("Negative input"); } rights reserved. 0-13-222158-6 16
if (score >= 90.0) 0) if (score >= 90.0) 0) grade = 'A'; else if (score >= 80.0) Equivalent grade = 'A'; else if (score >= 80.0) grade = 'B'; grade = 'B'; else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; else แทรก ระหวาง else if ไดหรอไม ไ if.. else.. else if.. else if (score >= 70.0) grade = 'C'; else if (score >= 60.0) grade = 'D'; else grade = 'F'; rights reserved. 0-13-222158-6 17
The else clause matches the most recent if clause in the same block. int i = 1; int j = 2; int k = 3; if (i > j) if (i > k) System.out.println("A"); else System.out.println("B"); (a) Equivalent int i = 1; int j = 2; int k = 3; if (i > j) if (i > k) System.out.println("A"); else System.out.println("B"); (b) rights reserved. 0-13-222158-6 18
Nothing is printed from the preceding statement. To force the else clause to match the first if clause, you must add a pair of braces: int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) System.out.println("A"); }else System.out.println( println("b"); This statement prints B. rights reserved. 0-13-222158-6 19
if (number % 2 == 0) even = true; else even = false; (a) Equivalent boolean even = number % 2 == 0; It is better (b) CAUTION if (even == true) System.out.println( t tl "It is even."); (a) if (even = true) {st;} Equivalent if (even) System.out.println( "It is even."); (b) rights reserved. 0-13-222158-6 20
The US federal personal income tax is calculated based on the filing status and taxable income. There are four filing statuses: single filers, married filing jointly, married filing separately, and head of household. ComputeTaxWithSelectionStatement This example creates a program to teach a first grade child how to learn subtractions. The program randomly generates two single-digit i integers number1 and number2 with number1 > number2 and displays a question such as What is 9 2? to the student, as shown in the figure. After the student types the answer in the input dialog box, the program displays a message dialog box to indicate whether the answer is correct, as shown in figure. SubtractionTour GuessBirthDate rights reserved. 0-13-222158-6 21
The switch-expression must yield a value of char, switch (switch-expression) { byte, short, or int type and must always be enclosed in case const1: statement(s)1; parentheses. break; case const2: statement(s)2; The const1,..., and constn must break; evaluate to the same type as the switch-expression can use. The resulting statements in the case case constn: statement(s)n; statement are executed when the break; value in the case statement matches default: statement(s)-for-default; the value of the switch-expression. } Note that const1,..., and constn are constant expressions, meaning that they cannot contain variables in the expression, such as 1 + x. --หมายถ ง x เป นต วแปรท วไปท ไม ใช ป ไ final int x = 20; เป นต น rights reserved. 0-13-222158-6 22
() (a) int i = 20; int j =20; long k = 100; switch (i) { case j: System.out.println(j); break; case k : System.out.println(k); t } Next statement; int i = 20; short j = 20; byte k = 100; switch (i) { case j: System.out.println(j); break; case k : System.out.println(k); } Next statement; (b) rights reserved. 0-13-222158-6 23
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. The default case, which is optional, can be used to perform actions when none of the specified cases matches the switch-expression. default อย ตรงไหนก ได? switch (switch-expression) { case const1: statement(s)1; break; case const2: statement(s)2; break; case constn: statement(s)n; break; default: statement(s)-for-default; } 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. rights reserved. 0-13-222158-6 24
true Execute Statement(s); break true true Execute Statement(s); Execute Statement(s); break break true Execute Statement(s); break default Default actions Next Statement rights reserved. 0-13-222158-6 25
animation Suppose ch is 'a': switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } rights reserved. 0-13-222158-6 26
animation ch is 'a': switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } rights reserved. 0-13-222158-6 27
animation Execute this line switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } rights reserved. 0-13-222158-6 28
animation Execute this line switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } rights reserved. 0-13-222158-6 29
animation Execute this line switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } rights reserved. 0-13-222158-6 30
animation Execute next statement switch (ch) { case 'a': System.out.println(ch); case 'b': System.out.println(ch); case 'c': System.out.println(ch); } Next statement; rights reserved. 0-13-222158-6 31
animation Suppose ch is 'a': switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } rights reserved. 0-13-222158-6 32
animation ch is 'a': switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } rights reserved. 0-13-222158-6 33
animation Execute this line switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } rights reserved. 0-13-222158-6 34
animation Execute this line switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); break; case 'c': System.out.println(ch); } rights reserved. 0-13-222158-6 35
animation Execute next 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; rights reserved. 0-13-222158-6 36
(booleanexpression)? expression1 : expression2; Ternary operator Binary operator (-, +) Unary operator(!, ++, --) if (x > 0) y = 1 else y = -1; y = (x > 0)? 1 : -1; rights reserved. 0-13-222158-6 37
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 ); rights reserved. 0-13-222158-6 38
JDK 1.5 Feature Use the new JDK 1.5 printf statement. System.out.printf(format, items); A format specifier specifies how an item should be displayed. Each specifier begins with a percent sign. An item may be a numeric value, character, boolean value, or a string. rights reserved. 0-13-222158-6 39
JDK 1.5 Feature Specifier Output Example %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" int count = 5; items double amount = 45.56; System.out.printf("count is %d and amount is %f", count, amount); display - count is 5 and amount is 45.560000 rights reserved. 0-13-222158-6 40
System.out.printf(format, t tf(f t item1, item2,..., itemk) ทดสอบกรณ int i =7; System.out.printf( %f,i);//runtime error String s= null; boolean b=false; System.out.printf( %b%b, s, b); //falsefalse String b1 ="aa"; boolean b2 = false; int b3= 20; System.out.printf("%b %B %b,b1,b2,b3); //true FALSE true rights reserved. 0-13-222158-6 41
String.format(format, item1, item2,..., itemk) -return a formatted string -ต วอย างการใช งาน : เพ อน าไปแสดงใน message dialog box String s = String.format("count is %d and amount is %f", 5, 45.56); JOptionPane.showMessageDialog(null,s); Operator Precedence Howtoevaluate 3+4*4 > 5*(4+3) 1? rights reserved. 0-13-222158-6 42
Highest order var++, var-- +, - (Unary plus and minus), ++var,--var (type) Casting! (Not) *, /, % (Multiplication, division, and remainder) +, - (Binary addition and subtraction) ti <, <=, >, >= (Comparison) ==,!=; (Equality) & (Unconditional AND) ^ (Exclusive OR) (Unconditional OR) && (Conditional AND) Short-circuit AND (Conditional OR) Short-circuit OR =,, +=,, -=,, *=,, /=,, %= (Assignment operator) Lowest order rights reserved. 0-13-222158-6 43
The expression in the parentheses is evaluated first. (Parentheses can be nested, in which case the expression in the inner parentheses is executed first.) 5+ (7/2) 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 it determines the order of evaluation. *, /, % All binary operators except assignment operators are left-associative. int x = 7+8; rights reserved. 0-13-222158-6 44
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. Ex. a b + c d is equivalent to ((a b) + c) d Assignment operators are right-associative. Therefore, the expression Ex.a=b+=c=5isequivalenttoa=(b+=(c= 5)) rights reserved. 0-13-222158-6 45
Applying the operator precedence and associativity rule, the expression 3 + 4 * 4 > 5 * (4 + 3) - 1 is evaluated as follows: 3 + 4 * 4 > 5 * (4 + 3) - 1 3 + 4 * 4 > 5 * 7 1 3 + 16 > 5 * 7 1 3 + 16 > 35 1 19 > 35 1 19 > 34 false (1) inside parentheses first (2) multiplication (3) multiplication (4) addition (5) subtraction (6) greater than rights reserved. 0-13-222158-6 46