? c Z E
) ?3?
P¨ZE
)7y??¨ZE
) ?3ZE×
)a
MZE`¥àQ
)
? . B U I ?
? c Z E
) ?3?
P¨ZE
)7y??¨ZE
) ?3ZE×
)a
MZE`¥àQ
)
? . B U I ?
e?y??1
V??f
? bRí
′d b¥ZE?1
f
? Rí′ ??
1 W P J E¥ZE?1
V? b
4.1 7yZE
ZEh
B?f ?/ZEμ/
2
?
e? Rí′ ?? ZE (?
?
V )
{
//ZE8
}
?
e?
^ Vê¥
·?
ZE¥
??
iOá?Ir?
??¨?ZE b
4.1 7yZE
ZEh
B?f ?/ZEμ/
2
?
e? Rí′ ?? ZE (?
?
V )
{
//ZE8
}
Rí′ ??
^ZE
1Rí¥′¥
?
?? b?TZE?
Rí′5Rí ?
?11o3 void b
4.1 7yZE
ZEh
B?f ?/ZEμ/
2
?
e? Rí′ ?? ZE (?
?
V )
{
//ZE8
}
ZE V[μB??
?
V?
ZE¥?S?1?
T?
? b?
ZE$?¨
H?
T?
?¨M
? 9D?tM
?
?1
L=?
? b?
?
^ Vê
¥ b
4.1 7yZE
ZEh
B?f ?/ZEμ/
2
?
e? Rí′ ?? ZE (?
?
V )
{
//ZE8
}
ZE8 ?B??lZES
I
1¥? "? bμRí′
¥ZE1pμ1o3 return
7Rí′ ??1 void¥ZE5
V[
àμ b?TRí′ ?
?1 void¥ZE?
P¨ return
5¨ ???ZE b
4.1 7yZE
public static int max(int num1, int num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
?
e?
?
e?
Rí′ ??
Rí′ ??
ZE
ZE
?
?
?
?
ZEh
Z
Z
E
E
8
8
Rí′
Rí′
4.2 ?¨ZE
7yZE
H1?lZE
^?
I
1¥ b
1
P¨ZEA??¨?¨
? b
?¨ZEμ
?o?
? ?TZERíB?′ZE¥?¨Yèü?T
) ?B?′ b? int larger = max(3, 4);
? ?TZERí voidZE¥?¨A?
^BH?
b? System.out.printlin(“Welcome to Java”);
4.2 ?¨ZE
Example 4.1 ?
k maxZE
?? è0
U8"7y??¨ maxZE b
TestMax Run TestMax
4.2 ?¨ZE
public static void main(String[] args)
{
int num1 = 5;
int num2 = 2;
int num3 = max(num1, num2);
System.out.println("The maximum between
" + num1 + " and " + num2 + " is " +
num3);
}
static int max(int num1, int num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
class TestMax {
}
4.3 ?
?.?
void nPrintln(String message, int n)
{
for (int i=0; i<n; i++)
System.out.println(message);
}
ZE¥ˉ ?
^
?) ??
?¥? ? b
?¨ZE
H314
L?A?DZE
?
??¥??Q?M]'?
?
¨??
¥ b? nPrintln(“Hello”, 3);
?
??′.?
?¨{?
?¥ZE
H
L?¥′ˉ??Z
E??1?′.? bí
?ZE?¥?
?8
"M?ZE?¥
L??
s?Y b
Example 4.2 ?
k?′.?
'??7yB?
?M
oD¥ZE b
TestPassByValue Run TestPassByValue
è0
a
ü
swap(n1, n2)
swap(num1, num2) num1
num2
1
2
n1
n2
1
2
n1
n2
2
2
temp
1
3?ZE swap
?′.?
è0
a
ü
swap(n1, n2)
swap(num1, num2) num1
num2
1
2
n1
n2
1
2
n1
n2
2
2
temp
1
3?ZE swap
?¨ swapZEa num1
ˉ1 1 num2ˉ1 2
e
ì
¥′
àμy1?¨ swapZ
E7?D b
L?s¥
?
?? num1? num2¥i%
bW b n1? n2¥?M??
Y num1? num2¥=? b
?′.?
4.4 ×ZE
)ZE×
^ Java
LC
?¥B?Z
T b
) è 4.1maxZEo?¨? int?
? b?TX1
?
???
??¥Kv
?;$3 %÷E
^7
y
6B?{μ?]?
?¥] ZE b?
double max(double num1, double num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
4.4 ×ZE
è 4.1maxZEo?¨? int?
? b?T
X1?
???
??¥Kv
?;$3 %
÷E
^7y
6B?{μ?]?
?¥]
ZE b?
double max(double num1, double num2)
{
if (num1 > num2)
return num1;
else
return num2;
}
??ZE ?M]?μ?]
?
??
T$?1ZE×
method overloading b?¨Z
E
H Java?"d??
ZE
2
ü %??¨'?ZE b
4.4 ×ZE
Example 4.3 × maxZE
' è7y
??ZEOZE ?M]
1pKv?
?
2pKv
?ú
?
3p??
?ú
??Kv¥
?
TestMethodOverloading Run Class
?T max(2, 2.5)|?¨'?ZE;$
4.5 ZE`
) ZE`
^üZE¥?¨]
LCs ? b
) ???ZE??
LC¥f ?/¨?9 V[
P¨Z
E b?T1?MZE¥
LCo1??MZE
2
ü
4ó¨?¥??ü??
s?Y b¨ ?
aZE¥
LCü^??(0-? b
ZE8
ZE
2
ü
?(0
Vê¥
{?
Vê¥Rí′
4.6 Math ?
) Math ?c
3?'
?D
?
?31¥Z
E b
)c¥ doubleè
– Math.PI
– Math.E
)c¥ZE
– ??f
?ZE
– ·
?ZE
–
eZE? min amax aabs around? random
4.6.1 ??f
?ZE
)c/
???f
?ZE
–public static double sin(double a)
–public static double cos(double a)
–public static double tan(double a)
–public static double acos(double a)
–public static double asin(double a)
–public static double atan(double a)
)?
?V
U[?9
¥? b 1?? 4 /180
? b
4.6.2 ·
?f
?ZE
)exp(double a)
Rí e¥ aQZ
e
a
b
)log(double a)
Rí a¥1?
?
ln(a)=log
e
(a) b
)pow(double a, double b)
Rí a¥ bQZ
a
b
b
)sqrt(double a)
RíüZ? a ? a??1μ
? b
4.6.3 Miscellaneous Methods
)max(a, b)and min(a, b)
Rí
?
?¥Kv′?Kl′ b
)abs(a)
RíB?
?¥ '′ b
)random()
Rí [0.0, 1.0)uW¥ double?
?
? b
)round()
Rí float′
1
??1 int′? double′
1
?
?1 long′ b
4.6.4
P¨ Math ?¥ZE
Example 4.4 9
?ü (′?Sμ
)' èI??
3? 10?
?
?
) ?¨/
?
T9
?ü (′?Sμ b
1
1
2
?
?
=
∑
=
n
meanx
deviation
n
i
i
n
x
mean
n
i
i∑
=
=
1
ComputeMeanDeviation Run
4.7
L èD
Example 4.5 A
U° ?
)' è7yB???A
Uó?M¥° ? b
??4
U¨?
{?M??aA
U?
¥° ? b
)??ù5
{?? au?° ? b
)?u?° ? VéB?s3??B?
?¥?ù