java slides
TRANSCRIPT
-
7/24/2019 Java slides
1/64
1Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Chapter 3 Control Statements
Chapter 1 Introduction to Computers, Programs,and Java
Chapter 2 Primitive Data Types and Operations
Chapter !oops
Chapter " #rrays
Chapter $ %ethods
&asic computer s'ills such as using (indo)s,
Internet *+plorer, and %icrosot (ord
--1./101./3 in Chapter 1. ecursion
Chapter 23 #lgorithm *iciency and Sorting
Chapter 3 Selection Statements
-
7/24/2019 Java slides
2/64
2Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
OectivesTo declare oolean type and )rite &oolean e+pressions 4-3/25/
To distinguish et)een conditional and unconditional 66 and 77operators 4-3/2/15/
To use &oolean e+pressions to control selection statements 4-3/30
3/$5/
To implement selection control using i and nested i statements4-3/35/
To implement selection control using s)itch statements 4-3/5/
To )rite e+pressions using the conditional operator 4-3/$5 /
To display ormatted output using the System/out/print methodand to ormat strings using the String/ormat method 4-3/"5/
To 'no) the rules governing operand evaluation order, operator
precedence, and operator associativity 4--3/803/95 /
-
7/24/2019 Java slides
3/64
3Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
The booleanType and Operators
Oten in a program you need to compare t)ovalues, such as )hether i is greater than / Java
provides si+ comparison operators 4also 'no)n
as relational operators5 that can e used to
compare t)o values/ The result o the
comparison is a &oolean value: true or alse/
boolean b = (1 > 2);
-
7/24/2019 Java slides
4/64
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Comparison Operators
Operator Name
< less than
greater than
>= greater than or e;ual to
== e;ual to
!= not e;ual to
-
7/24/2019 Java slides
5/64
$Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
&oolean Operators
Operator Name
! not
&& and|| or
^ e+clusive or
-
7/24/2019 Java slides
6/64
"Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Truth Tale or Operator Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rig
hts reserved. 0!"#!222"$%!&
*+amples
System/out/println4AIs A B num B A divisile y 2 and 3 A B
44num 2 ?? >5 66 4num 3 ?? >555E
System/out/println4AIs A B num B A divisile y 2 or 3 A B 44num 2 ?? >5 77 4num 3 ?? >555E
System/out/println4AIs A B num B
A divisile y 2 or 3, ut not oth A B
44num 2 ?? >5 @ 4num 3 ?? >555E
-
7/24/2019 Java slides
11/6411Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rig
hts reserved. 0!"#!222"$%!&
*+ample: Determining !eap Fear
LeapYearLeapYear RunRun
This program irst prompts the user to enter a year as
an int value and chec's i it is a leap year/
# year is a leap year i it is divisile y ut not y
1>>, or it is divisile y >>/
4year ?? >66 year 1>>
-
7/24/2019 Java slides
12/6412Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rig
hts reserved. 0!"#!222"$%!&
*+ample: # Simple %ath !earning Tool
AdditionTutorAdditionTutor RunRun
This e+ample creates a program to let a irst grader practice additions/The program randomly generates t)o single0digit integers numer1
and numer2 and displays a ;uestion such as G(hat is 8 B .H to the
student, as sho)n elo)/ #ter the student types the ans)er in the
input dialog o+, the program displays a message dialog o+ to
indicate )hether the ans)er is true or alse/
http://html/LearnAddition.htmlhttp://var/www/apps/conversion/tmp/scratch_3/html%5CLearnAddition.bathttp://var/www/apps/conversion/tmp/scratch_3/html%5CLearnAddition.bathttp://var/www/apps/conversion/tmp/scratch_3/html%5CLearnAddition.bathttp://html/LearnAddition.htmlhttp://html/LearnAddition.html -
7/24/2019 Java slides
13/6413Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
The 6 and 7 Operators
&&: conditional AND operator
&: unconditional AND operator
||: conditional OR operator
|: unconditional OR operator
exp1 && exp2
(1 < x) && (x < 100)
(1 < x) & (x < 100)
-
7/24/2019 Java slides
14/641Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
The 6 and 7 Operators
If x is 1, what is x after this
expression
(x > 1) & (x < 1")
If x is 1, what is x after this
expression
(1 > x) && ( 1 > x)
#ow about (1 == x) | (1" > x)
(1 == x) || (1" > x)
-
7/24/2019 Java slides
15/641$Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Selection Statements
ifStatements
swit$hStatements
Conditional Operators
-
7/24/2019 Java slides
16/641"Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
SimpleifStatements
&oolean
*+pression
true
Statement4s5
alse4radius =? >5
true
area ? radius radius PIE
System/out/println4AThe area or the circle o A B
Aradius A B radius B A is A B area5E
alse
4#5 4&5
i 4oolean*+pression5 statement4s5E
K
i 4radius =? >5
area ? radius radius PIE System/out/println4AThe areaA
B A or the circle o radius A
B radius B A is A B area5E
K
-
7/24/2019 Java slides
17/6418Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Lote
if ((i > ") && (i < 1"))%
'steoutprintln(*i is an * !
! *inte+er between " an, 1"*);
-
4a5
*;uivalent
45
if ((i > ") && (i < 1"))
'steoutprintln(*i is an * !
! *inte+er between " an, 1"*);
Outer parentheses re;uired &races can e omitted i the loc' contains a single
statement
-
7/24/2019 Java slides
18/6419Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Caution
#dding a semicolon at the end o an i clause is a common
mista'e/i 4radius =? >5E
area ? radiusradiusPIE
System/out/println4
AThe area or the circle o radius A B
radius B A is A B area5E
KThis mista'e is hard to ind, ecause it is not a compilation error
or a runtime error, it is a logic error/
This error oten occurs )hen you use the ne+t0line loc' style/
(rong
-
7/24/2019 Java slides
19/64
-
7/24/2019 Java slides
20/64
2>Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
ifelse*+ample
if (raius >= ") %area = raius 0 raius 0 1134;
'steoutprintln(*5he area for the 6
6$ir$le of raius * raius * is * area);
-
else %
'steoutprintln(*7e+ati8e input*);
-
-
7/24/2019 Java slides
21/64
21Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
%ultiple #lternative i Statements
if (s$ore >= 4"")+ra,e = 9:9;
else
if (s$ore >= "")
+ra,e = 99;
else
if (s$ore >= "")
+ra,e = 99;
else
if (s$ore >= ?"")
+ra,e = 9@9;
else
+ra,e = 9A9;
*;uivalent
if (s$ore >= 4"")
+ra,e = 9:9;
else if (s$ore >= "")
+ra,e = 99;
else if (s$ore >= "")
+ra,e = 99;
else if (s$ore >= ?"")
+ra,e = 9@9;
else
+ra,e = 9A9;
aniation
-
7/24/2019 Java slides
22/64
22Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace i0else statement
i 4score =? .>/>5
grade ? M#ME
else i 4score =? 9>/>5
grade ? M&ME
else i 4score =? 8>/>5 grade ? MCME
else i 4score =? ">/>5
grade ? MDME
else grade ? MNME
Suppose score is 8>/> The condition is alse
aniation
aniation
-
7/24/2019 Java slides
23/64
23Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace i0else statement
i 4score =? .>/>5
grade ? M#ME
else i 4score =? 9>/>5
grade ? M&ME
else i 4score =? 8>/>5 grade ? MCME
else i 4score =? ">/>5
grade ? MDME
else grade ? MNME
Suppose score is 8>/> The condition is alse
aniation
aniation
-
7/24/2019 Java slides
24/64
2Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace i0else statement
i 4score =? .>/>5
grade ? M#ME
else i 4score =? 9>/>5
grade ? M&ME
else i 4score =? 8>/>5 grade ? MCME
else i 4score =? ">/>5
grade ? MDME
else grade ? MNME
Suppose score is 8>/> The condition is true
aniation
aniation
-
7/24/2019 Java slides
25/64
2$Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace i0else statement
i 4score =? .>/>5
grade ? M#ME
else i 4score =? 9>/>5
grade ? M&ME
else i 4score =? 8>/>5 grade ? MCME
else i 4score =? ">/>5
grade ? MDME
else grade ? MNME
Suppose score is 8>/> grade is C
aniation
aniation
-
7/24/2019 Java slides
26/64
2"Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace i0else statement
i 4score =? .>/>5
grade ? M#ME
else i 4score =? 9>/>5
grade ? M&ME
else i 4score =? 8>/>5 grade ? MCME
else i 4score =? ">/>5
grade ? MDME
else grade ? MNME
Suppose score is 8>/> *+it the i statement
aniation
-
7/24/2019 Java slides
27/64
28Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
LoteThe else clause matches the most recent i clause in the same
loc'/int i = 1;
int B = 2;
int C = ;
if (i > B)
if(i > C)'steoutprintln(*:*);
else
'steoutprintln(**);
4a5
*;uivalent
4b5
int i = 1;
int B = 2;
int C = ;
if (i > B)
if(i > C)'steoutprintln(*:*);
else
'steoutprintln(**);
-
7/24/2019 Java slides
28/64
29Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Lote, cont/
Lothing is printed rom the preceding statement/ To orce
the else clause to match the irst i clause, you must add apair o races:
int i = 1;
int B = 2;
int C = ;
if (i > B) {
if (i > C)
'steoutprintln(*:*);
}
else
'steoutprintln(**);
This statement prints &/
-
7/24/2019 Java slides
29/64
2.Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
TIP
if (nuber D 2 == ")e8en = true;
else
e8en = false;
4a5
*;uivalent boolean e8en= nuber D 2 == ";
45
-
7/24/2019 Java slides
30/64
3>Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
C#TIOL
if (e8en == true)&'steoutprintln(
*It is e8en*);
4a5
*;uivalent if (e8en)&'steoutprintln(
*It is e8en*);
45
-
7/24/2019 Java slides
31/64
31Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
*+ample: Computing Ta+es
The S ederal personal income ta+ is calculated ased on
the iling status and ta+ale income/ There are our ilingstatuses: single ilers, married iling ointly, married iling
separately, and head o household/ The ta+ rates or 2>>2
are sho)n in Tale 3/1/
-
7/24/2019 Java slides
32/64
32Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
*+ample: Computing Ta+es, cont/
!oputeTax"it#$election$tateent!oputeTax"it#$election$tateent Run
if (status == ") %
EE opute tax for sin+le filers
-
else if (status == 1) %
EE opute tax for arrie file Bointl'-
else if (status == 2) %
EE opute tax for arrie file separatel'
-
else if (status == ) %
EE opute tax for hea of househol
-
else %
EE @ispla' wron+ status
-
http://html/ComputeTaxWithSelectionStatement.htmlhttp://html/ComputeTaxWithSelectionStatement.htmlhttp://var/www/apps/conversion/tmp/scratch_3/html%5CComputeTax.bathttp://html/ComputeTaxWithSelectionStatement.htmlhttp://html/ComputeTaxWithSelectionStatement.htmlhttp://var/www/apps/conversion/tmp/scratch_3/html%5CComputeTax.bathttp://var/www/apps/conversion/tmp/scratch_3/html%5CComputeTax.bathttp://var/www/apps/conversion/tmp/scratch_3/html%5CComputeTax.bathttp://html/ComputeTaxWithSelectionStatement.htmlhttp://html/ComputeTaxWithSelectionStatement.html -
7/24/2019 Java slides
33/64
33Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
*+ample: #n Improved %ath !earning Tool
This e+ample creates a program to teach a irst grade child
ho) to learn sutractions/ The program randomlygenerates t)o single0digit integers numer1 and numer2)ith numer1 = numer2 and displays a ;uestion such asG(hat is . 2H to the student, as sho)n in the igure/#ter the student types the ans)er in the input dialog o+,the program displays a message dialog o+ to indicate)hether the ans)er is correct, as sho)n in igure/
$u%tractionTutor$u%tractionTutor Run
http://html/SubtractionTutor.htmlhttp://var/www/apps/conversion/tmp/scratch_3/html%5CSubtractionTutor.bathttp://var/www/apps/conversion/tmp/scratch_3/html%5CSubtractionTutor.bathttp://html/SubtractionTutor.htmlhttp://html/SubtractionTutor.html -
7/24/2019 Java slides
34/64
3Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
*+ample: Quessing &irth Date
ue''irt#Dateue''irt#Date Run
The program can guess your irth date/ unto see ho) it )or's/
http://html/GuessBirthDate.htmlhttp://var/www/apps/conversion/tmp/scratch_3/html%5CGuessBirthDate.bathttp://var/www/apps/conversion/tmp/scratch_3/html%5CGuessBirthDate.bathttp://html/GuessBirthDate.htmlhttp://html/GuessBirthDate.html -
7/24/2019 Java slides
35/64
3$Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
swit$hStatementss)itch 4status5
case >: compute ta+es or single ilersE rea'E
case 1: compute ta+es or married ile ointlyE
rea'E
case 2: compute ta+es or married ile separatelyE
rea'E
case 3: compute ta+es or head o householdE
rea'E
deault: System/out/println4A*rrors: invalid statusA5E
System/e+it4>5E
K
-
7/24/2019 Java slides
36/64
3"Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
swit$hStatement Nlo) Chart
status is >Compute ta+ or single ilers rea'
Compute ta+ or married ile ointly rea'
status is 1
Compute ta+ or married ile separatly rea'status is 2
Compute ta+ or head o household rea'
status is 3
Deault actionsdeault
Le+t Statement
i h St t t l
-
7/24/2019 Java slides
37/64
38Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
swit$hStatement ules
s)itch 4s)itch0e+pression5 case value1: statement4s51E
rea'E
case value2: statement4s52E
rea'E
R
case valueL: statement4s5LE
rea'E
deault: statement4s50or0deaultE
K
The s)itch0e+pression mustyield a value o char, yte,
short, or int type and mustal)ays e enclosed in
parentheses/
The value1, ///, and valueL must
have the same data type as thevalue o the s)itch0e+pression/
The resulting statements in the
case statement are e+ecuted )hen
the value in the case statement
matches the value o the s)itch0
e+pression/ Lote that value1, ///,and valueL are constant
e+pressions, meaning that they
cannot contain variales in the
e+pression, such as 1 B +/
it h St t t l
-
7/24/2019 Java slides
38/64
39Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
swit$hStatement ules
The 'ey)ord rea' is optional,
ut it should e used at the end o
each case in order to terminate the
remainder o the s)itch statement/
I the rea' statement is not
present, the ne+t case statement
)ill e e+ecuted/
s)itch 4s)itch0e+pression5 case value1: statement4s51E
rea'E
case value2: statement4s52E
rea'E
R
case valueL: statement4s5LE
rea'E
deault: statement4s50or0deaultE
K
The deault case, )hich is
optional, can e used to perorm
actions )hen none o the
speciied cases matches thes)itch0e+pression/ The case statements are e+ecuted in se;uential
order, ut the order o the cases 4including the
deault case5 does not matter/ o)ever, it is good
programming style to ollo) the logical se;uence o
the cases and place the deault case at the end/
aniation
-
7/24/2019 Java slides
39/64
3.Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
case9b9F 'steoutprintln($h); case9$9F 'steoutprintln($h);
-
Suppose ch is MaM:
aniation
-
7/24/2019 Java slides
40/64
>Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
case9b9F 'steoutprintln($h); case9$9F 'steoutprintln($h);
-
ch is MaM:
aniation
-
7/24/2019 Java slides
41/64
1Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
case9b9F 'steoutprintln($h); case9$9F 'steoutprintln($h);
-
*+ecute this line
aniation
-
7/24/2019 Java slides
42/64
2Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
case9b9F 'steoutprintln($h); case9$9F 'steoutprintln($h);
-
*+ecute this line
aniation
-
7/24/2019 Java slides
43/64
3Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
case9b9F 'steoutprintln($h); case9$9F 'steoutprintln($h);
-
*+ecute this line
aniation
-
7/24/2019 Java slides
44/64
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
case9b9F 'steoutprintln($h); case9$9F 'steoutprintln($h);
-
Le+t statementE
*+ecute ne+t statement
aniation
-
7/24/2019 Java slides
45/64
$Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
break; case9b9F 'steoutprintln($h);
breakE
case9$9F 'steoutprintln($h);
-
Suppose ch is MaM:
aniation
-
7/24/2019 Java slides
46/64
"Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
break; case9b9F 'steoutprintln($h);
breakE
case9$9F 'steoutprintln($h);
-
ch is MaM:
aniation
-
7/24/2019 Java slides
47/64
8Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
break; case9b9F 'steoutprintln($h);
breakE
case9$9F 'steoutprintln($h);
-
*+ecute this line
aniation
-
7/24/2019 Java slides
48/64
9Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
break; case9b9F 'steoutprintln($h);
breakE
case9$9F 'steoutprintln($h);
-
*+ecute this line
aniation
-
7/24/2019 Java slides
49/64
.Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Trace s)itch statement
switch($h) %
case9a9F 'steoutprintln($h);
break; case9b9F 'steoutprintln($h);
breakE
case9$9F 'steoutprintln($h);
-
Le+t statementE
*+ecute ne+t statement
C di i l O
-
7/24/2019 Java slides
50/64
$>Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Conditional Operator
i 4+ = >5
y ? 1else
y ? 01E
is e;uivalent to
y ? 4+ = >5 1 : 01E4oolean*+pression5 e+pression1 : e+pression2
Ternary operator&inary operatornary operator
-
7/24/2019 Java slides
51/64
$1Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Conditional Operator
if (nu D 2 == ")
'steoutprintln(nu 6is e8enG);
else
'steoutprintln(nu 6is oG);
'steoutprintln(
(nu D 2 == ") nu 6is e8enG F nu 6is oG);
-
7/24/2019 Java slides
52/64
$2Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Conditional Operator, cont/
(boolean.xp) exp1 F exp2
N tti O t tJD 1/$
-
7/24/2019 Java slides
53/64
$3Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Normatting Output
se the ne) JD 1/$ print statement/
System/out/print4ormat, items5E
(here ormat is a string that may consist o sustrings and
ormat speciiers/ # ormat speciier speciies ho) an item
should e displayed/ #n item may e a numeric value,
character, oolean value, or a string/ *ach speciier egins
)ith a percent sign/
Neature
N tl d S ii
JD 1/$
-
7/24/2019 Java slides
54/64
$Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Nre;uently0sed SpeciiersNeature
Specifier Output Example
Db a boolean 8alue true or false
D$ a $hara$ter 9a9
D a e$ial inte+er 2""
Df a floatin+/point nuber 3?""""
De a nuber in stanar s$ientifi$ notation 33?"""e"1
Ds a strin+ *Ha8a is $ool*
int $ount = 3;
ouble aount = 33?;
'steoutprintf(*$ount is D an aount is Df*, $ount, aount);
ispla' $ount is 3 an aount is 33?""""
items
C i N d S i
-
7/24/2019 Java slides
55/64
$$Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Creating Normatted Strings
System/out/print4ormat, item1, item2, ///, itemk5
String/ormat4ormat, item1, item2, ///, itemk5
String s ? String/ormat4Acount is d and amount is A, $, $/$"55E
O P d
-
7/24/2019 Java slides
56/64
$"Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Operator Precedence
o) to evaluate 3 B = $ 4 B 35 1
O t P d
-
7/24/2019 Java slides
57/64
$8Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Operator Precedence 8ar, 8ar//
, / (nar' plus an inus), 8ar,//8ar (t'pe) astin+
! (7ot)
0,E,D (Jultipli$ation, i8ision, an reainer)
,/ (inar' aition an subtra$tion)
= (oparison)
==,!=; (.Kualit')
& (n$onitional :7@)
^ (.x$lusi8e LM)
| (n$onitional LM)
&& (onitional :7@) hort/$ir$uit :7@
|| (onitional LM) hort/$ir$uit LM
=,=,/=,0=,E=,D= (:ssi+nent operator)
O t P d d # i ti it
-
7/24/2019 Java slides
58/64
$9Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Operator Precedence and #ssociativity
The e+pression in the parentheses is evaluated irst/
4Parentheses can e nested, in )hich case the e+pressionin the inner parentheses is e+ecuted irst/5 (henevaluating an e+pression )ithout parentheses, theoperators are applied according to the precedence rule and
the associativity rule/
I operators )ith the same precedence are ne+t to eachother, their associativity determines the order o
evaluation/ #ll inary operators e+cept assignmentoperators are let0associative/
O t # i ti it
-
7/24/2019 Java slides
59/64
$.Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Operator #ssociativity
(hen t)o operators )ith the same precedenceare evaluated, the associativity o the operatorsdetermines the order o evaluation/ #ll inaryoperators e+cept assignment operators are left-
associative/ a B c d is e;uivalent to 44a 5 B c5 d
#ssignment operators are right-associative/
Thereore, the e+pression a ? B? c ? $ is e;uivalent to a ? 4 B? 4c ? $55
* l
-
7/24/2019 Java slides
60/64
">Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
*+ample
#pplying the operator precedence and associativity rule,
the e+pression 3 B = $ 4 B 35 0 1 is evaluated asollo)s:
0 > 3 0 ( ) / 1
0 > 3 0 N 1
1? > 3 0 N 1
1? > 3 N 1
14 > 3 N 1
14 >
false
1 inside arentheses irst
425 multiplication
435 multiplication
45 addition
4$5 sutraction
4"5 greater than
O d * l ti O d
-
7/24/2019 Java slides
61/64
"1Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Operand *valuation Order
The precedence and associativity rulesspeciy the order o the operators, ut do not
speciy the order in )hich the operands o a
inary operator are evaluated/ Operands are
evaluated rom let to right in Java/
The left-hand operand of a binary operator is
evaluated before any part of the right-hand
operand is evaluated.
O d * l ti O d t
-
7/24/2019 Java slides
62/64
"2Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
Operand *valuation Order, cont/
I no operands haveside effectsthat change the valueo a variale, the order o operand evaluation isirrelevant/ Interesting cases arise )hen operands dohave a side eect/ Nor e+ample,xecomes 1 in theollo)ing code, ecause a is evaluated to > eore ++ais evaluated to 1/
int a ? >E
int + ? a B 4BBa5E
&utxecomes 2 in the ollo)ing code, ecause ++ais evaluated to 1, then ais evaluated to 1/
int a ? >E
int + ? BBa B aE
l * l ti * i
-
7/24/2019 Java slides
63/64
"3Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. A rights reserved. 0!"#!222"$%!&
ule o *valuating an *+pression
ule 1: *valuate )hatever sue+pressions you can
possily evaluate rom let to right/
ule 2: The operators are applied according to their
precedence/
ule 3: The associativity rule applies or t)o operators
ne+t to each other )ith the same precedence/
l * l ti * i
-
7/24/2019 Java slides
64/64
ule o *valuating an *+pression
#pplying the rule, the e+pression 3 B = $ 4 B 35 0 1is evaluated as ollo)s:
! 0 > 3 0 ( ! ) / 1
! 1? > 3 0 ( ! ) / 1
14 > 3 0 ( ! ) / 1
14 > 3 0 / 1
14 > 3 N 1
14 >
false
415 I is the irst sue+pression that can
e evaluated rom let/
425 3 B 1" is evaluated no)/
435 B 3 is no) the letmost sue+pression
that should e evaluated/
45 $ I 8 is evaluated no)/
4$5 3$ P 1 is evaluated no)/
4"5 1. = 3 is evaluated no)/