際際滷

際際滷Share a Scribd company logo
Ch動董ng 2
JAVA CN 京畉N
M畛c ti棚u
 Vi畉t 動畛c ch動董ng tr狸nh Java theo 炭ng c炭 ph叩p
2
N畛i dung
2.1. Gi畛i thi畛u Java
2.2. C畉u tr炭c ch動董ng tr狸nh Java
2.3. T畛ng quan l畉p tr狸nh Java
2.3.1. Ki畛u d畛 li畛u
2.3.2. Bi畉n, h畉ng
2.3.3. To叩n t畛, bi畛u th畛c
2.3.4. C叩c c畉u tr炭c l畛nh (c畉u tr炭c i畛u khi畛n, l畉p)
2.3.5. Vi畉t ph動董ng th畛c trong Java
2.3.6. M畉ng
2.4. Ngo畉i l畛 (Exception)
2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
3
2.1. Gi畛i thi畛u Java
Java l g狸?
 Java: l ng担n ng畛 l畉p tr狸nh h動畛ng 畛i t動畛ng do Sun MicroSystem 動a
ra vo gi畛a th畉p ni棚n 90
 Java: 畛c l畉p n畛n (ph畉n c畛ng v h畛 i畛u hnh)
 Java: v畛a bi棚n d畛ch (compiler) v畛a th担ng d畛ch (interpreter)
4
2.1. Gi畛i thi畛u Java
Qu叩 tr狸nh d畛ch ch動董ng tr狸nh Java
Write once, run anywhere
5
2.1. Gi畛i thi畛u Java
C叩c phi棚n b畉n ch鱈nh c畛a Java
 Standard Edition (Java SE ho畉c J2SE)
o C嘆n g畛i l ng担n ng畛 l畉p tr狸nh Java
o D湛ng 畛 vi畉t: 畛ng d畛ng desktop, applets, java FX, 畛ng d畛ng web kh担ng
c畉n Java EE
 Enterprise Edition (Java EE ho畉c J2EE)
o L java ch畉y tr棚n c叩c 畛ng d畛ng server
o D湛ng 畛 vi畉t: Servlet, JSP, JSF, Strut, EJB, Spring, Hibernate,
o V鱈 d畛: Google home page, gmail, Google Maps, Google Docs
 Micro Edition (Java ME)
o L java ch畉y tr棚n thi畉t b畛 di 畛ng v nh炭ng
o 畛ng d畛ng vi畉t cho: TDT, PDA, TV set-top box, m叩y in
6
2.1. Gi畛i thi畛u Java
JDK  Java Development Kit
 JDK l m畛t m担i tr動畛ng ph叩t tri畛n d湛ng 畛 x但y d畛ng c叩c 畛ng d畛ng,
applet v c叩c component b畉ng ng担n ng畛 l畉p tr狸nh Java.
 Bao g畛m:
o Development tools (bin/)
o Runtime environment (jre/)
o Additional libraries (lib/)
o Java DB (db/)
o Source code (src.zip)
o 
7
2.2. C畉u tr炭c ch動董ng tr狸nh Java
8
/*
* Created on Jul 14, 2005
*
* First Java Program
*/
package com.jds.sample;
import java.util.*;
/**
* @author JDS
*/
public class JavaMain {
public static void main(String[] args) {
// print a message
System.out.println("Welcome to Java!");
}
}
class Extra {
/*
* class body
*/
}
1. Single Line Comment
// insert comments here
2. Block Comment
/*
* insert comments here
*/
3. Documentation Comment
/**
* insert documentation
*/
Comments
2.2. C畉u tr炭c ch動董ng tr狸nh Java
9
/*
* Created on Jul 14, 2005
*
* First Java Program
*/
package com.jds.sample;
import java.util.*;
/**
* @author JDS
*/
public class JavaMain {
public static void main(String[] args) {
// print a message
System.out.println("Welcome to Java!");
}
}
class Extra {
/*
* class body
*/
}
1. Package declaration
Used to organize a collection of related classes.
2. Import statement
Used to reference classes.
3. Class declaration
A Java source file can have several
classes but only one public class is
allowed.
A file can have more classes, but
the file name must match with one in it
Declaration order
2.3. T畛ng quan l畉p tr狸nh Java
 Ki畛u d畛 li畛u
 Bi畉n, h畉ng
 To叩n t畛, bi畛u th畛c
 C叩c c畉u tr炭c l畛nh (c畉u tr炭c i畛u khi畛n, l畉p)
 Vi畉t ph動董ng th畛c trong Java
 M畉ng
10
2.3. T畛ng quan l畉p tr狸nh Java
Ki畛u d畛 li畛u
 C坦 hai ki畛u d畛 li畛u:
o Ki畛u d畛 li畛u c董 s畛 (Primitive data type)
o Ki畛u d畛 li畛u tham chi畉u - d畉n xu畉t (Reference data type)
11
2.3. T畛ng quan l畉p tr狸nh Java
Ki畛u d畛 li畛u c董 s畛
 C坦 8 ki畛u d畛 li畛u c董 s畛:
Ki畛u c董 s畛
Ki畛u lu畉n l箪
boolean
Ki畛u s畛
Ki畛u nguy棚n Ki畛u th畛c
Ki畛u k箪 t畛
char
byte short int long float double 12
2.3. T畛ng quan l畉p tr狸nh Java
Ki畛u d畛 li畛u c董 s畛
 B畉ng gi叩 tr畛 c畛a c叩c ki畛u c董 s畛
Type Bits Lowest Value Highest Value
boolean (n/a) false true
char 16 'u0000' [0] 'uffff' [216-1]
byte 8 -128 [-27] +127 [27-1]
short 16 -32,768 [-215] +32,767 [215-1]
int 32 -2,147,483,648 [-231] +2,147,483,647 [231-1]
long 64
-9,223,372,036,854,775,808 [-
263]
+9,223,372,036,854,775,807 [263-1]
float 32 賊1.40129846432481707e-45 賊3.40282346638528860e+38
double 64 賊4.94065645841246544e-324 賊1.79769313486231570e+308 13
2.3. T畛ng quan l畉p tr狸nh Java
Ki畛u d畛 li畛u tham chi畉u
 Ki畛u d畛 li畛u tham chi畉u bi畛u di畛n cho 畛i t動畛ng (object)
 Java c坦 3 ki畛u d畛 li畛u tham chi畉u:
o Class
o Array
o Interface
14
2.3. T畛ng quan l畉p tr狸nh Java
Bi畉n, h畉ng: C叩ch 畉t t棚n
 B畉t 畉u b畉ng k箪 t畛, k箪 t畛 g畉ch d動畛i (_) hay k箪 t畛 $
 Sau 坦 l c叩c k箪 t畛, k箪 s畛 hay _, $. Kh担ng d湛ng c叩c k箪 t畛 kh叩c nh動:
kho畉ng tr畛ng, k箪 hi畛u ph辿p to叩n
 T棚n c坦 t鱈nh ch畉t ph但n bi畛t ch畛 th動畛ng ch畛 hoa (case-sensitive)
15
2.3. T畛ng quan l畉p tr狸nh Java
Bi畉n: Khai b叩o
 Khai b叩o v s畛 d畛ng bi畉n
o Bi畉n l m畛t gi叩 tr畛 c坦 th畛 thay 畛i khi ch動董ng tr狸nh th畛c thi
o C叩ch khai b叩o:
o V鱈 d畛:
int total;
int count, temp, result;
Ki畛u d畛 li畛u T棚n bi畉n
int sum = 0;
int base = 32, max = 149;
<ki畛u_d畛_li畛u> <t棚n_bi畉n>;
<ki畛u_d畛_li畛u> <t棚n_bi畉n> = <gi叩_tr畛>;
16
2.3. T畛ng quan l畉p tr狸nh Java
Bi畉n: Khai b叩o
 Khai b叩o bi畉n c坦 ki畛u d畛 li畛u c董 s畛:
int age = 0;
 Khai b叩o bi畉n c坦 ki畛u d畛 li畛u tham chi畉u (khai b叩o object):
String name = new String("Jason");
Date bday = new Date(2018, 12, 24);
17
t棚n
bi畉n
ki畛u
c董 s畛
gi叩 tr畛
kh畛i t畉o
t棚n 畛i
t動畛ng
ki畛u tham
chi畉u
gi叩 tr畛 kh畛i t畉o
2.3. T畛ng quan l畉p tr狸nh Java
Bi畉n: c畛c b畛, ton c畛c
 Bi畉n c畛c b畛: ph畉i 動畛c kh畛i t畉o gi叩 tr畛 tr動畛c khi s畛 d畛ng
 Bi畉n ton c畛c: ph畉i 動畛c khai b叩o b棚n ngoi c叩c hm v b棚n trong
class, v鱈 d畛:
class Example {
static int x = 10;
public static void main(String args[ ]) {
System.out.println(x);
}
}
18
2.3. T畛ng quan l畉p tr狸nh Java
H畉ng
 Khai b叩o v s畛 d畛ng h畉ng (constant)
o Gi叩 tr畛 h畉ng kh担ng 動畛c ph辿p thay 畛i trong ch動董ng tr狸nh, n坦 ch畛 動畛c
g叩n gi叩 tr畛 m畛t l畉n 畛 d嘆ng khai b叩o h畉ng
o Trong Java, d湛ng final 畛 khai b叩o h畉ng, v鱈 d畛:
final int MIN_HEIGHT = 69;
19
2.3. T畛ng quan l畉p tr狸nh Java
To叩n t畛, bi畛u th畛c
 C叩c to叩n t畛 s畛 h畛c:
 To叩n t畛 tng/gi畉m
o To叩n t畛 tng (++)
o To叩n t畛 gi畉m (--)
o C但u l畛nh count++; t動董ng 動董ng v畛i count = count + 1;
C畛ng
Tr畛
Nh但n
Chia
Chia l畉y s畛 d動
+
-
*
/
%
20
2.3. T畛ng quan l畉p tr狸nh Java
To叩n t畛, bi畛u th畛c
 To叩n t畛 so s叩nh (quan h畛)
== b畉ng
!= kh担ng b畉ng
< nh畛 h董n
> l畛n h董n
<= nh畛 h董n ho畉c b畉ng
>= l畛n h董n ho畉c b畉ng
 To叩n t畛 lu畉n l箪
! : not
&& : and
|| : or
21
2.3. T畛ng quan l畉p tr狸nh Java
To叩n t畛, bi畛u th畛c
 To叩n t畛 i畛u ki畛n
o C炭 ph叩p: i畛u_ki畛n ? bi畛u_th畛c1 : bi畛u_th畛c2
o N畉u i畛u_ki畛n l true, bi畛u_th畛c1 動畛c th畛c thi; N畉u l false, bi畛u_th畛c2 動畛c
th畛c thi
o V鱈 d畛:
int x = 10, y = 20;
int Z = (x<y) ? 30 : 40;
 C叩c bi畛u th畛c
o M畛t bi畛u th畛c l m畛t s畛 k畉t h畛p gi畛a c叩c to叩n t畛 v c叩c to叩n h畉ng
o N畉u trong bi畛u th畛c c坦 ch畛a s畛 th畛c th狸 k畉t qu畉 tr畉 v畛 s畛 th畛c
o L動u 箪 畛 動u ti棚n c畛a c叩c to叩n t畛 trong 1 bi畛u th畛c
22
2.3. T畛ng quan l畉p tr狸nh Java
C叩c c畉u tr炭c l畛nh tr棚n Java
 C畉u tr炭c i畛u khi畛n  R畉 nh叩nh
C畉u tr炭c if
if (Condition)
{
Statements;
}
else
{
Statement;
}
C畉u tr炭c switch
switch (Expression)
{ case Cons1: Statements;
break;
case Cons2: Statements;
break;
. . .
default : Statements;
} 23
2.3. T畛ng quan l畉p tr狸nh Java
C叩c c畉u tr炭c l畛nh tr棚n Java
 C畉u tr炭c i畛u khi畛n  R畉 nh叩nh
o Th動畛ng m畛t l畛nh break 動畛c d湛ng 畛 cu畛i danh s叩ch l畛nh c畛a m畛i case.
M畛t c畉u tr炭c switch c坦 th畛 c坦 m畛t case default
o K畉t qu畉 c畛a Expression trong switch ph畉i l ki畛u s畛 nguy棚n (byte, short,
int, long) ho畉c char. Kh担ng th畛 l boolean ho畉c s畛 th畛c (float, double)
24
2.3. T畛ng quan l畉p tr狸nh Java
C叩c c畉u tr炭c l畛nh tr棚n Java
 C畉u tr炭c l畉p
while condition)
{
Statements;
}
do
{
Statements;
}while (condition);
for (VarInit; Condition; Statements)
{
Statements1;
} 25
2.3. T畛ng quan l畉p tr狸nh Java
Vi畉t ph動董ng th畛c trong Java
 C炭 ph叩p:
 V鱈 d畛 1: Vi畉t ch動董ng tr狸nh t鱈nh t畛ng c叩c s畛 nguy棚n ch畉n t畛 1-> N. N
動畛c nh畉p t畛 bn ph鱈m. Y棚u c畉u vi畉t ph動董ng th畛c t鱈nh t畛ng.
 V鱈 d畛 2: Vi畉t ch動董ng tr狸nh t鱈nh t畛ng c叩c s畛 nguy棚n t畛 t畛 1-> N. N 動畛c
nh畉p t畛 bn ph鱈m. Y棚u c畉u vi畉t ph動董ng th畛c ki畛m tra nguy棚n t畛,
ph動董ng th畛c t鱈nh t畛ng.
26
ReturnType methodName (Type agr1, Type arg2,)
{

return (somethingOfReturnType);
}
2.3. T畛ng quan l畉p tr狸nh Java
M畉ng
 L m畛t c畉u tr炭c d畛 li畛u cho ph辿p l動u m畛t t畉p c叩c gi叩 tr畛 c湛ng ki畛u
27
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 98 87 81 74 91
scores
The entire array
has a single name
Each value has an index
2.3. T畛ng quan l畉p tr狸nh Java
Khai b叩o m畉ng
 C炭 ph叩p:
 V鱈 d畛 khai b叩o m畉ng:
o int[] scores = new int[100];// m畉ng scores ch畛a 100 ph畉n t畛 s畛 nguy棚n
o String[] names;
names = new String[20]; // m畉ng names ch畛a 20 chu畛i
 V鱈 d畛 khai b叩o v g叩n gi叩 tr畛 c湛ng l炭c:
o int[] smallPrimes = { 2, 3, 5, 7, 11, 13 };
o char[] letterGrades = {'A', 'B', 'C', 'D', F'};
28
DataType[] arrayName = new DataType[size];
DataType[] arrayName; // only declares the variable
arrayName = new DataType[size];
2.3. T畛ng quan l畉p tr狸nh Java
S畛 d畛ng m畉ng
 Duy畛t m畉ng a:
 V鱈 d畛:
o for (int i=0; i < scores.length; i++)
System.out.println(scores[i]);
o for (int score : scores)
System.out.println(score);
o for (String n : names)
System.out.println(n);
 Bi t畉p: t畉o m畉ng s畛 nguy棚n c坦 10 ph畉n t畛, g叩n gi叩 tr畛 b畉t k畛 v xu畉t m畉ng.
29
for (int i=0; i < a.length; i++)
// process a[i]
for (Type x : a)
// process x
length field
returns the number
of elements
2.4. Ngo畉i l畛
 Exception l m畛t s畛 ki畛n x畉y ra trong qu叩 tr狸nh th畛c thi ch動董ng tr狸nh,
ph叩 v畛 lu畛ng b狸nh th動畛ng c畛a ch動董ng tr狸nh
 V鱈 d畛:
30
2.4. Ngo畉i l畛
Ph但n lo畉i
 Trong Java c坦 2 lo畉i exception l Checked Exception v
Unchecked Exception
o Checked Exception: l c叩c exception x畉y ra t畉i th畛i i畛m compile. Nh畛ng
exception ny th動畛ng li棚n quan 畉n l畛i c炭 ph叩p (syntax) v b畉t bu畛c
ch炭ng ta ph畉i "b畉t" (catch) n坦.
o Unchecked Exception: l c叩c exception x畉y ra t畉i th畛i i畛m runtime.
Nh畛ng exception ny th動畛ng li棚n quan 畉n l畛i logic v kh担ng b畉t bu畛c
ch炭ng ta ph畉i "b畉t" (catch) n坦.
31
2.4. Ngo畉i l畛
X畛 l箪 ngo畉i l畛 trong Java
 Keyword
o try  catch
o finally
o throw
o throws
32
2.4. Ngo畉i l畛
S畛 d畛ng try...catch
 Kh畛i try ... catch": Ph但n t叩ch o畉n ch動董ng tr狸nh th担ng th動畛ng v
ph畉n x畛 l箪 ngo畉i l畛
o ExceptionType: l t棚n l畛p ngo畉i l畛 c畉n x畛 l箪, n畉u kh担ng c畉n x叩c 畛nh c畛
th畛 th狸 s畛 d畛ng class Exception
33
try {
// o畉n l畛nh c坦 kh畉 nng x畉y ra ngo畉i l畛
}
catch (ExceptionType e) {
// x畛 l箪 ngo畉i l畛 (th担ng b叩o l畛i ho畉c n辿m ngo畉i l畛 ra b棚n ngoi)
}
2.4. Ngo畉i l畛
S畛 d畛ng try...catch: V鱈 d畛 1
34
public class Zero {
public static void main (String[] args) {
try {
int tu = 10;
int mau = 0;
System.out.println (tu/mau);
System.out.println ("This text will not be printed.");
}
catch (ArithmeticException e) {
System.out.println ("Kh担ng th畛 chia cho 0"); // tb l畛i
}
}
}
2.4. Ngo畉i l畛
S畛 d畛ng try...catch: Nhi畛u kh畛i catch
 M畛t o畉n m達 c坦 th畛 x畉y ra nhi畛u ngo畉i l畛: S畛 d畛ng nhi畛u kh畛i catch
 ExceptionType1 ph畉i l l畛p con ho畉c ngang hng v畛i ExceptionType2
(trong c但y ph但n c畉p k畉 th畛a) 35
try {
// o畉n m達 c坦 th畛 x畉y ra nhi畛u ngo畉i l畛
}
catch (ExceptionType1 e1) {
// X畛 l箪 ngo畉i l畛 1
}
catch (ExceptionType2 e2) {
// X畛 l箪 ngo畉i l畛 2
} ...
2.4. Ngo畉i l畛
S畛 d畛ng try...catch: V鱈 d畛 2
36
import java.util.Scanner;
class Zero2 {
public static void main (String args[]){
int no1, no2;
try {
Scanner sc = new Scanner (System.in);
System.out.print("Input no1: "); no1 = sc.nextInt();
System.out.print("Input no2: "); no2 = sc.nextInt();
System.out.println("Division result is: " + no1/no2);
}
catch (ArithmeticException e) {
System.out.println("Cannot divide by zero"); }
catch (InputMismatchException e) {
System.out.println("You must input an integer"); }
}
}
2.4. Ngo畉i l畛
Kh畛i finally
 畉m b畉o th畛c hi畛n t畉t c畉 c叩c c担ng vi畛c c畉n thi畉t d湛 ngo畉i l畛 c坦 x畉y ra
hay kh担ng
o 坦ng file, 坦ng socket, connection
o Gi畉i ph坦ng ti nguy棚n (n畉u c畉n)...
37
try {
// o畉n m達 c坦 th畛 g但y ra ngo畉i l畛
}
catch (ExceptionType e) {
// X畛 l箪 ngo畉i l畛
}
finally {
/* Th畛c hi畛n t畉t c畉 c叩c c担ng vi畛c d湛 ngo畉i l畛 c坦 x畉y ra hay kh担ng */
}
2.4. Ngo畉i l畛
S畛 d畛ng throw
 S畛 d畛ng throw khi ng動畛i l畉p tr狸nh mu畛n t畛 ph叩t sinh ra ngo畉i l畛, vd:
38
public static void main (String[] args) {
try {
final int MIN = 25, MAX = 40;
Scanner scan = new Scanner (System.in);
System.out.print ("Enter an integer value between " + MIN + " and " + MAX + ", inclusive: ");
int value = scan.nextInt();
if (value < MIN || value > MAX)
throw new Exception(); // make an exception
else
System.out.println("Gia tri nhap hop le");
System.out.println ("End of main method."); // may never be reached
}
catch( Exception x ) { // catch the exception
System.out.println("Number is not valid.");
}
2.4. Ngo畉i l畛
S畛 d畛ng throws
 Trong tr動畛ng h畛p ph動董ng th畛c c坦 ph叩t sinh ra ngo畉i l畛, n畉u kh担ng
mu畛n b畉t n坦 ngay (nh動 trong v鱈 d畛 tr動畛c) th狸 s畛 d畛ng throws 畛 b叩o
cho ch動董ng tr狸nh bi畉t l ph動董ng th畛c ny s畉 n辿m ra ngo畉i l畛
39
method1() {
try {
invoke method2;
}
catch (Exception ex) {
Process exception;
}
}
method2() throws Exception {
if (an error occurs) {
throw new Exception();
}
}
catch exception throw exception
declare exception
2.4. Ngo畉i l畛
S畛 d畛ng throws: V鱈 d畛
40
public class DelegateExceptionDemo {
public static void main(String args[]){
try {
int num = calculate(9,3);
System.out.println(Lan 1:  + num);
num = calculate(9,0);
System.out.println(Lan 2:  + num);
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
static int calculate(int tu, int mau) throws ArithmeticException {
if (mau== 0)
throw new ArithmeticException("Khong the chia cho 0!");
int num = tu/ mau;
return num;
}
}
2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
L畛p Scanner
 N畉m trong g坦i java.util
 C担ng d畛ng: 畛c d畛 li畛u t畛 bn ph鱈m
 C叩ch d湛ng:
o T畉o 畛i t動畛ng 畛 畛c t畛 bn ph鱈m:
 Scanner scan = new Scanner (System.in);
o D湛ng c叩c ph動董ng th畛c c畛a Scanner 畛 nh畉p d畛 li畛u t畛 bn ph鱈m, nh動:
 string answer = scan.nextLine();  Nh畉p m畛t chu畛i k箪 t畛
 int a = scan.nextInt();  Nh畉p m畛t s畛 nguy棚n
 double d = scan.nextDouble();  Nh畉p m畛t s畛 th畛c
 
41
2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
L畛p Scanner: v鱈 d畛
import java.util.*;
public class Cong2so {
public static void main (String[] args) {
Scanner nhap = new Scanner(System.in);
int x,y;
System.out.print("Nhap so thu nhat: ");
x = nhap.nextInt();
System.out.print("Nhap so thu hai: ");
y = nhap.nextInt();
int tong = x+y;
System.out.println ("The sum is: "+ tong);
}
}
際際滷 42
2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
L畛p Random
 L畛p Random n畉m trong g坦i java.util
 C担ng d畛ng: D湛ng 畛 ph叩t sinh s畛 ng畉u nhi棚n
 C叩ch d湛ng:
o T畉o 畛i t動畛ng:
 Random rd = new Random();
o Ph叩t sinh s畛 ng畉u nhi棚n c坦 mi畛n gi叩 tr畛 thu畛c [0, n-1]
 int a = rd.nextInt(n);
o Ph叩t sinh s畛 ng畉u nhi棚n c坦 mi畛n gi叩 tr畛 thu畛c [0.0, 1.0)
 float a = rd.nextFloat();
 Vd/ Ph叩t sinh 3 s畛 a,b,c thu畛c [0,50], t狸m s畛 l畛n nh畉t.
43
2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
L畛p Math
 L畛p Math n畉m trong g坦i java.lang (g坦i m畉c 畛nh)
 Ch畛a c叩c ph動董ng th畛c c坦 ch畛c nng t鱈nh to叩n v畛 to叩n h畛c:
o pow: L滴y th畛a
o sqrt: Cn b畉c 2
o abs: Tr畛 tuy畛t 畛i
o 
 C叩c ph動董ng th畛c trong l畛p Math l nh畛ng ph動董ng th畛c t挑nh (static
methods). V狸 v畉y, ch畛 c畉n g畛i tr畛c ti畉p ph動董ng th畛c th担ng qua t棚n l畛p
m kh担ng c畉n t畉o 畛i t動畛ng
o V鱈 d畛: double value = Math.sqrt(25.0);
44
2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
畛nh d畉ng k畉t qu畉 xu畉t ra mn h狸nh
 L畛p NumberFormat cho ph辿p 畛nh d畉ng gi叩 tr畛 theo ki畛u ti畛n t畛 ho畉c
ph畉n trm
 L畛p DecimalFormat cho ph辿p 畛nh d畉ng gi叩 tr畛 theo m畉u 畛nh d畉ng
cho tr動畛c
 C畉 2 n畉m trong g坦i java.text
45
2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
畛nh d畉ng k畉t qu畉 xu畉t ra mn h狸nh
import java.text.NumberFormat;
import java.util.*;
public class Price {
public static void main (String[] args) {
final double TAX_RATE = 0.06; // 6% sales tax
int quantity;
double subtotal, tax, totalCost, unitPrice;
Scanner scan = new Scanner(System.in);
System.out.print ("So luong: ");
quantity = scan.nextInt();
System.out.print ("Don gia: ");
unitPrice = scan.nextDouble();
subtotal = quantity * unitPrice;
tax = subtotal * TAX_RATE;
totalCost = subtotal + tax;
// Print output with appropriate formatting
NumberFormat money = NumberFormat.getCurrencyInstance();
NumberFormat percent = NumberFormat.getPercentInstance();
System.out.println ("Thanh tien: " + money.format(subtotal));
System.out.println ("Thue: " + money.format(tax) + " at " + percent.format(TAX_RATE));
System.out.println ("Tong tien: " + money.format(totalCost));
}
}
So luong: 5
Don gia: 3.87
Thanh tien: $19.35
Thue: $1.16 at 6%
Tong tien: $20.51
46
2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n
畛nh d畉ng k畉t qu畉 xu畉t ra mn h狸nh
import java.text.DecimalFormat;
import java.util.*;
public class CircleStats {
public static void main (String[] args) {
int radius;
double area, circumference;
Scanner scan = new Scanner(System.in);
System.out.print ("Enter the circle's radius: ");
radius = scan.nextInt();
area = Math.PI * Math.pow(radius, 2);
circumference = 2 * Math.PI * radius;
// Round the output to three decimal places
DecimalFormat fmt = new DecimalFormat ("0.###");
System.out.println ("The circle's area: " + fmt.format(area));
System.out.println ("The circle's circumference: " + fmt.format(circumference));
}
}
Enter the circle's radius: 5
The circle's area: 78.54
The circle's circumference: 31.416
47
48
Ki畉n th畛c c畉n n畉m
1. Xu畉t (d湛ng c畛ng chu畛i print/println v c坦 畛nh d畉ng printf)
2. Nh畉p (c叩c lo畉i gi叩 tr畛) (際際滷 41, 42)
3. Ph但n bi畛t c叩c ki畛u d畛 li畛u (際際滷 11-14)
4. Khai b叩o bi畉n, h畉ng (際際滷 15-17), m畉ng (際際滷 27-29)
5. Ph但n bi畛t c叩ch d湛ng bi畉n ton c畛c, bi畉n c畛c b畛 (際際滷 18)
6. Ngo畉i l畛: b畉t ngo畉i l畛, n辿m ngo畉i l畛 (際際滷 30-40)
7. Vi畉t ph動董ng th畛c (際際滷 26)
49
Review questions
1. C畉n ci ch動董ng tr狸nh g狸 畛 vi畉t Java?
2. Hm b畉t 畉u ch畉y ch動董ng tr狸nh Java c坦 t棚n l g狸? C炭 ph叩p hm nh動 th畉
no?
3. C坦 bao nhi棚u ki畛u d畛 li畛u c董 s畛 trong Java? Bao nhi棚u ki畛u d畛 li畛u tham
chi畉u?
4. 4 ki畛u d畛 li畛u s畛 nguy棚n trong Java?
5. To叩n t畛 new d湛ng 畛 lm g狸?
6. Ki畛u chu畛i trong Java c坦 t棚n l g狸? Thu畛c v畛 ki畛u d畛 li畛u g狸?
7. K畛 t棚n m畛t s畛 ki畛u d畛 li畛u tham chi畉u.
8. Ngo畉i l畛 l g狸. Lm th畉 no 畛 x畛 l箪 ngo畉i l畛.
9. Lm sao b畉t 動畛c ngo畉i l畛, n辿m ngo畉i l畛. Khi no c畉n n辿m ngo畉i l畛.
50
Demo
1. Vi畉t ch動董ng tr狸nh in ra d嘆ng ch畛 HELLO
2. Vi畉t ch動董ng tr狸nh in k畉t qu畉 (2 s畛 l畉) c畛a ph辿p chia 2 s畛 nguy棚n (s畛
c坦 s畉n ho畉c y棚u c畉u nh畉p)
3. Vi畉t ch動董ng tr狸nh n畛i 2 chu畛i v in ra k畉t qu畉
4. Vi畉t hm t鱈nh t畛ng c叩c s畛 ch畉n trong kho畉ng t畛 1 畉n n v 畛ng d畛ng
5. Vi畉t hm t鱈nh t畛ng c叩c s畛 ch畉n trong m畉ng v 畛ng d畛ng
51
Excercises
Bi 1: Nh畉p vo 2 s畛 nguy棚n, in ra t畛ng, hi畛u, t鱈ch, th動董ng c畛a 2 s畛.
Ch炭 箪 ki畛m tra s畛 th畛 2 c坦 kh叩c kh担ng hay kh担ng, n畉u b畉ng 0 th狸 th担ng
b叩o Kh担ng th畛 chia cho 0!!.
Y棚u c畉u ki畛m tra d畛 li畛u nh畉p ph畉i l s畛.
Bi 2: Gi畉i ph動董ng tr狸nh b畉c nh畉t.
Bi 3: Vi畉t ch動董ng tr狸nh t鱈nh t畛ng c叩c s畛 nguy棚n t畛 t畛 1-> N. N 動畛c
nh畉p t畛 bn ph鱈m. Y棚u c畉u vi畉t ph動董ng th畛c ki畛m tra s畛 nguy棚n t畛,
ph動董ng th畛c t鱈nh t畛ng, ki畛m tra gi叩 tr畛 N.
Bi 4: Nh畉p vo m畛t chu畛i. 畉m s畛 k箪 t畛, s畛 t畛 trong chu畛i v畛a nh畉p. In
m畛i t畛 tr棚n m畛t d嘆ng. Y棚u c畉u vi畉t hm cho t畛ng thao t叩c.
Bi 5: C叩c bi t畉p Module 1
52

More Related Content

Similar to OOP_02_Java can ban.pdf (20)

Core java 3
Core java 3Core java 3
Core java 3
. .
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provipSV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
huykhadeo113
際際滷 lap trinh web sieu nang cao provip
際際滷 lap trinh web sieu nang cao provip際際滷 lap trinh web sieu nang cao provip
際際滷 lap trinh web sieu nang cao provip
huykhadeo113
179887496 sdc1-bai tap-javacanban-5
179887496 sdc1-bai tap-javacanban-5179887496 sdc1-bai tap-javacanban-5
179887496 sdc1-bai tap-javacanban-5
le dai
S董 l動畛c v畛 Java
S董 l動畛c v畛 JavaS董 l動畛c v畛 Java
S董 l動畛c v畛 Java
i畛p L棚
H畛c l畉p tr狸nh c董 b畉n - Gi畛i thi畛u c叩c thu畉t ng畛 trong l畉p tr狸nh
H畛c l畉p tr狸nh c董 b畉n - Gi畛i thi畛u c叩c thu畉t ng畛 trong l畉p tr狸nhH畛c l畉p tr狸nh c董 b畉n - Gi畛i thi畛u c叩c thu畉t ng畛 trong l畉p tr狸nh
H畛c l畉p tr狸nh c董 b畉n - Gi畛i thi畛u c叩c thu畉t ng畛 trong l畉p tr狸nh
CodeGym N畉ng
Oop unit 10 ngo畉i l畛
Oop unit 10 ngo畉i l畛Oop unit 10 ngo畉i l畛
Oop unit 10 ngo畉i l畛
Tr叩ng H Vi畉t
Bi gi畉ng------------------ CTDLGT 1.pdf
Bi gi畉ng------------------ CTDLGT 1.pdfBi gi畉ng------------------ CTDLGT 1.pdf
Bi gi畉ng------------------ CTDLGT 1.pdf
LNguynNamSn
OOP in Java - Ver1.1
OOP in Java -  Ver1.1OOP in Java -  Ver1.1
OOP in Java - Ver1.1
vdlinh08
Linq net
Linq net Linq net
Linq net
NguynSang29
7 stl c++
7   stl c++7   stl c++
7 stl c++
qnv95
LINQ
LINQLINQ
LINQ
Hiep Luong
Oop unit 02 java c董 b畉n
Oop unit 02 java c董 b畉nOop unit 02 java c董 b畉n
Oop unit 02 java c董 b畉n
Tr叩ng H Vi畉t
4-Object_Class_Constructor_Overload_VN.pdf
4-Object_Class_Constructor_Overload_VN.pdf4-Object_Class_Constructor_Overload_VN.pdf
4-Object_Class_Constructor_Overload_VN.pdf
phanthanhthuan039914
4-Object_Class_Constructor_Ovearload.pdf
4-Object_Class_Constructor_Ovearload.pdf4-Object_Class_Constructor_Ovearload.pdf
4-Object_Class_Constructor_Ovearload.pdf
phanthanhthuan039914
Bi 2: Bi畉n v to叩n t畛 - Gi叩o tr狸nh FPT
Bi 2: Bi畉n v to叩n t畛 - Gi叩o tr狸nh FPTBi 2: Bi畉n v to叩n t畛 - Gi叩o tr狸nh FPT
Bi 2: Bi畉n v to叩n t畛 - Gi叩o tr狸nh FPT
MasterCode.vn
DoThanhNghi2016_Python.pdf
DoThanhNghi2016_Python.pdfDoThanhNghi2016_Python.pdf
DoThanhNghi2016_Python.pdf
TamDo58
Bi gi畉ng ng担n ng畛 l畉p tr狸nh hi畛n nay: Ng担n ng畛 Python
Bi gi畉ng ng担n ng畛 l畉p tr狸nh hi畛n nay: Ng担n ng畛 PythonBi gi畉ng ng担n ng畛 l畉p tr狸nh hi畛n nay: Ng担n ng畛 Python
Bi gi畉ng ng担n ng畛 l畉p tr狸nh hi畛n nay: Ng担n ng畛 Python
dtmanh1
Core java 3
Core java 3Core java 3
Core java 3
. .
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provipSV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
SV_2023_KTLT_01a_Mang-2-chieu.pdf際際滷 lap trinh web sieu nang cao provip
huykhadeo113
際際滷 lap trinh web sieu nang cao provip
際際滷 lap trinh web sieu nang cao provip際際滷 lap trinh web sieu nang cao provip
際際滷 lap trinh web sieu nang cao provip
huykhadeo113
179887496 sdc1-bai tap-javacanban-5
179887496 sdc1-bai tap-javacanban-5179887496 sdc1-bai tap-javacanban-5
179887496 sdc1-bai tap-javacanban-5
le dai
S董 l動畛c v畛 Java
S董 l動畛c v畛 JavaS董 l動畛c v畛 Java
S董 l動畛c v畛 Java
i畛p L棚
H畛c l畉p tr狸nh c董 b畉n - Gi畛i thi畛u c叩c thu畉t ng畛 trong l畉p tr狸nh
H畛c l畉p tr狸nh c董 b畉n - Gi畛i thi畛u c叩c thu畉t ng畛 trong l畉p tr狸nhH畛c l畉p tr狸nh c董 b畉n - Gi畛i thi畛u c叩c thu畉t ng畛 trong l畉p tr狸nh
H畛c l畉p tr狸nh c董 b畉n - Gi畛i thi畛u c叩c thu畉t ng畛 trong l畉p tr狸nh
CodeGym N畉ng
Oop unit 10 ngo畉i l畛
Oop unit 10 ngo畉i l畛Oop unit 10 ngo畉i l畛
Oop unit 10 ngo畉i l畛
Tr叩ng H Vi畉t
Bi gi畉ng------------------ CTDLGT 1.pdf
Bi gi畉ng------------------ CTDLGT 1.pdfBi gi畉ng------------------ CTDLGT 1.pdf
Bi gi畉ng------------------ CTDLGT 1.pdf
LNguynNamSn
OOP in Java - Ver1.1
OOP in Java -  Ver1.1OOP in Java -  Ver1.1
OOP in Java - Ver1.1
vdlinh08
7 stl c++
7   stl c++7   stl c++
7 stl c++
qnv95
Oop unit 02 java c董 b畉n
Oop unit 02 java c董 b畉nOop unit 02 java c董 b畉n
Oop unit 02 java c董 b畉n
Tr叩ng H Vi畉t
4-Object_Class_Constructor_Overload_VN.pdf
4-Object_Class_Constructor_Overload_VN.pdf4-Object_Class_Constructor_Overload_VN.pdf
4-Object_Class_Constructor_Overload_VN.pdf
phanthanhthuan039914
4-Object_Class_Constructor_Ovearload.pdf
4-Object_Class_Constructor_Ovearload.pdf4-Object_Class_Constructor_Ovearload.pdf
4-Object_Class_Constructor_Ovearload.pdf
phanthanhthuan039914
Bi 2: Bi畉n v to叩n t畛 - Gi叩o tr狸nh FPT
Bi 2: Bi畉n v to叩n t畛 - Gi叩o tr狸nh FPTBi 2: Bi畉n v to叩n t畛 - Gi叩o tr狸nh FPT
Bi 2: Bi畉n v to叩n t畛 - Gi叩o tr狸nh FPT
MasterCode.vn
DoThanhNghi2016_Python.pdf
DoThanhNghi2016_Python.pdfDoThanhNghi2016_Python.pdf
DoThanhNghi2016_Python.pdf
TamDo58
Bi gi畉ng ng担n ng畛 l畉p tr狸nh hi畛n nay: Ng担n ng畛 Python
Bi gi畉ng ng担n ng畛 l畉p tr狸nh hi畛n nay: Ng担n ng畛 PythonBi gi畉ng ng担n ng畛 l畉p tr狸nh hi畛n nay: Ng担n ng畛 Python
Bi gi畉ng ng担n ng畛 l畉p tr狸nh hi畛n nay: Ng担n ng畛 Python
dtmanh1

OOP_02_Java can ban.pdf

  • 2. M畛c ti棚u Vi畉t 動畛c ch動董ng tr狸nh Java theo 炭ng c炭 ph叩p 2
  • 3. N畛i dung 2.1. Gi畛i thi畛u Java 2.2. C畉u tr炭c ch動董ng tr狸nh Java 2.3. T畛ng quan l畉p tr狸nh Java 2.3.1. Ki畛u d畛 li畛u 2.3.2. Bi畉n, h畉ng 2.3.3. To叩n t畛, bi畛u th畛c 2.3.4. C叩c c畉u tr炭c l畛nh (c畉u tr炭c i畛u khi畛n, l畉p) 2.3.5. Vi畉t ph動董ng th畛c trong Java 2.3.6. M畉ng 2.4. Ngo畉i l畛 (Exception) 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n 3
  • 4. 2.1. Gi畛i thi畛u Java Java l g狸? Java: l ng担n ng畛 l畉p tr狸nh h動畛ng 畛i t動畛ng do Sun MicroSystem 動a ra vo gi畛a th畉p ni棚n 90 Java: 畛c l畉p n畛n (ph畉n c畛ng v h畛 i畛u hnh) Java: v畛a bi棚n d畛ch (compiler) v畛a th担ng d畛ch (interpreter) 4
  • 5. 2.1. Gi畛i thi畛u Java Qu叩 tr狸nh d畛ch ch動董ng tr狸nh Java Write once, run anywhere 5
  • 6. 2.1. Gi畛i thi畛u Java C叩c phi棚n b畉n ch鱈nh c畛a Java Standard Edition (Java SE ho畉c J2SE) o C嘆n g畛i l ng担n ng畛 l畉p tr狸nh Java o D湛ng 畛 vi畉t: 畛ng d畛ng desktop, applets, java FX, 畛ng d畛ng web kh担ng c畉n Java EE Enterprise Edition (Java EE ho畉c J2EE) o L java ch畉y tr棚n c叩c 畛ng d畛ng server o D湛ng 畛 vi畉t: Servlet, JSP, JSF, Strut, EJB, Spring, Hibernate, o V鱈 d畛: Google home page, gmail, Google Maps, Google Docs Micro Edition (Java ME) o L java ch畉y tr棚n thi畉t b畛 di 畛ng v nh炭ng o 畛ng d畛ng vi畉t cho: TDT, PDA, TV set-top box, m叩y in 6
  • 7. 2.1. Gi畛i thi畛u Java JDK Java Development Kit JDK l m畛t m担i tr動畛ng ph叩t tri畛n d湛ng 畛 x但y d畛ng c叩c 畛ng d畛ng, applet v c叩c component b畉ng ng担n ng畛 l畉p tr狸nh Java. Bao g畛m: o Development tools (bin/) o Runtime environment (jre/) o Additional libraries (lib/) o Java DB (db/) o Source code (src.zip) o 7
  • 8. 2.2. C畉u tr炭c ch動董ng tr狸nh Java 8 /* * Created on Jul 14, 2005 * * First Java Program */ package com.jds.sample; import java.util.*; /** * @author JDS */ public class JavaMain { public static void main(String[] args) { // print a message System.out.println("Welcome to Java!"); } } class Extra { /* * class body */ } 1. Single Line Comment // insert comments here 2. Block Comment /* * insert comments here */ 3. Documentation Comment /** * insert documentation */ Comments
  • 9. 2.2. C畉u tr炭c ch動董ng tr狸nh Java 9 /* * Created on Jul 14, 2005 * * First Java Program */ package com.jds.sample; import java.util.*; /** * @author JDS */ public class JavaMain { public static void main(String[] args) { // print a message System.out.println("Welcome to Java!"); } } class Extra { /* * class body */ } 1. Package declaration Used to organize a collection of related classes. 2. Import statement Used to reference classes. 3. Class declaration A Java source file can have several classes but only one public class is allowed. A file can have more classes, but the file name must match with one in it Declaration order
  • 10. 2.3. T畛ng quan l畉p tr狸nh Java Ki畛u d畛 li畛u Bi畉n, h畉ng To叩n t畛, bi畛u th畛c C叩c c畉u tr炭c l畛nh (c畉u tr炭c i畛u khi畛n, l畉p) Vi畉t ph動董ng th畛c trong Java M畉ng 10
  • 11. 2.3. T畛ng quan l畉p tr狸nh Java Ki畛u d畛 li畛u C坦 hai ki畛u d畛 li畛u: o Ki畛u d畛 li畛u c董 s畛 (Primitive data type) o Ki畛u d畛 li畛u tham chi畉u - d畉n xu畉t (Reference data type) 11
  • 12. 2.3. T畛ng quan l畉p tr狸nh Java Ki畛u d畛 li畛u c董 s畛 C坦 8 ki畛u d畛 li畛u c董 s畛: Ki畛u c董 s畛 Ki畛u lu畉n l箪 boolean Ki畛u s畛 Ki畛u nguy棚n Ki畛u th畛c Ki畛u k箪 t畛 char byte short int long float double 12
  • 13. 2.3. T畛ng quan l畉p tr狸nh Java Ki畛u d畛 li畛u c董 s畛 B畉ng gi叩 tr畛 c畛a c叩c ki畛u c董 s畛 Type Bits Lowest Value Highest Value boolean (n/a) false true char 16 'u0000' [0] 'uffff' [216-1] byte 8 -128 [-27] +127 [27-1] short 16 -32,768 [-215] +32,767 [215-1] int 32 -2,147,483,648 [-231] +2,147,483,647 [231-1] long 64 -9,223,372,036,854,775,808 [- 263] +9,223,372,036,854,775,807 [263-1] float 32 賊1.40129846432481707e-45 賊3.40282346638528860e+38 double 64 賊4.94065645841246544e-324 賊1.79769313486231570e+308 13
  • 14. 2.3. T畛ng quan l畉p tr狸nh Java Ki畛u d畛 li畛u tham chi畉u Ki畛u d畛 li畛u tham chi畉u bi畛u di畛n cho 畛i t動畛ng (object) Java c坦 3 ki畛u d畛 li畛u tham chi畉u: o Class o Array o Interface 14
  • 15. 2.3. T畛ng quan l畉p tr狸nh Java Bi畉n, h畉ng: C叩ch 畉t t棚n B畉t 畉u b畉ng k箪 t畛, k箪 t畛 g畉ch d動畛i (_) hay k箪 t畛 $ Sau 坦 l c叩c k箪 t畛, k箪 s畛 hay _, $. Kh担ng d湛ng c叩c k箪 t畛 kh叩c nh動: kho畉ng tr畛ng, k箪 hi畛u ph辿p to叩n T棚n c坦 t鱈nh ch畉t ph但n bi畛t ch畛 th動畛ng ch畛 hoa (case-sensitive) 15
  • 16. 2.3. T畛ng quan l畉p tr狸nh Java Bi畉n: Khai b叩o Khai b叩o v s畛 d畛ng bi畉n o Bi畉n l m畛t gi叩 tr畛 c坦 th畛 thay 畛i khi ch動董ng tr狸nh th畛c thi o C叩ch khai b叩o: o V鱈 d畛: int total; int count, temp, result; Ki畛u d畛 li畛u T棚n bi畉n int sum = 0; int base = 32, max = 149; <ki畛u_d畛_li畛u> <t棚n_bi畉n>; <ki畛u_d畛_li畛u> <t棚n_bi畉n> = <gi叩_tr畛>; 16
  • 17. 2.3. T畛ng quan l畉p tr狸nh Java Bi畉n: Khai b叩o Khai b叩o bi畉n c坦 ki畛u d畛 li畛u c董 s畛: int age = 0; Khai b叩o bi畉n c坦 ki畛u d畛 li畛u tham chi畉u (khai b叩o object): String name = new String("Jason"); Date bday = new Date(2018, 12, 24); 17 t棚n bi畉n ki畛u c董 s畛 gi叩 tr畛 kh畛i t畉o t棚n 畛i t動畛ng ki畛u tham chi畉u gi叩 tr畛 kh畛i t畉o
  • 18. 2.3. T畛ng quan l畉p tr狸nh Java Bi畉n: c畛c b畛, ton c畛c Bi畉n c畛c b畛: ph畉i 動畛c kh畛i t畉o gi叩 tr畛 tr動畛c khi s畛 d畛ng Bi畉n ton c畛c: ph畉i 動畛c khai b叩o b棚n ngoi c叩c hm v b棚n trong class, v鱈 d畛: class Example { static int x = 10; public static void main(String args[ ]) { System.out.println(x); } } 18
  • 19. 2.3. T畛ng quan l畉p tr狸nh Java H畉ng Khai b叩o v s畛 d畛ng h畉ng (constant) o Gi叩 tr畛 h畉ng kh担ng 動畛c ph辿p thay 畛i trong ch動董ng tr狸nh, n坦 ch畛 動畛c g叩n gi叩 tr畛 m畛t l畉n 畛 d嘆ng khai b叩o h畉ng o Trong Java, d湛ng final 畛 khai b叩o h畉ng, v鱈 d畛: final int MIN_HEIGHT = 69; 19
  • 20. 2.3. T畛ng quan l畉p tr狸nh Java To叩n t畛, bi畛u th畛c C叩c to叩n t畛 s畛 h畛c: To叩n t畛 tng/gi畉m o To叩n t畛 tng (++) o To叩n t畛 gi畉m (--) o C但u l畛nh count++; t動董ng 動董ng v畛i count = count + 1; C畛ng Tr畛 Nh但n Chia Chia l畉y s畛 d動 + - * / % 20
  • 21. 2.3. T畛ng quan l畉p tr狸nh Java To叩n t畛, bi畛u th畛c To叩n t畛 so s叩nh (quan h畛) == b畉ng != kh担ng b畉ng < nh畛 h董n > l畛n h董n <= nh畛 h董n ho畉c b畉ng >= l畛n h董n ho畉c b畉ng To叩n t畛 lu畉n l箪 ! : not && : and || : or 21
  • 22. 2.3. T畛ng quan l畉p tr狸nh Java To叩n t畛, bi畛u th畛c To叩n t畛 i畛u ki畛n o C炭 ph叩p: i畛u_ki畛n ? bi畛u_th畛c1 : bi畛u_th畛c2 o N畉u i畛u_ki畛n l true, bi畛u_th畛c1 動畛c th畛c thi; N畉u l false, bi畛u_th畛c2 動畛c th畛c thi o V鱈 d畛: int x = 10, y = 20; int Z = (x<y) ? 30 : 40; C叩c bi畛u th畛c o M畛t bi畛u th畛c l m畛t s畛 k畉t h畛p gi畛a c叩c to叩n t畛 v c叩c to叩n h畉ng o N畉u trong bi畛u th畛c c坦 ch畛a s畛 th畛c th狸 k畉t qu畉 tr畉 v畛 s畛 th畛c o L動u 箪 畛 動u ti棚n c畛a c叩c to叩n t畛 trong 1 bi畛u th畛c 22
  • 23. 2.3. T畛ng quan l畉p tr狸nh Java C叩c c畉u tr炭c l畛nh tr棚n Java C畉u tr炭c i畛u khi畛n R畉 nh叩nh C畉u tr炭c if if (Condition) { Statements; } else { Statement; } C畉u tr炭c switch switch (Expression) { case Cons1: Statements; break; case Cons2: Statements; break; . . . default : Statements; } 23
  • 24. 2.3. T畛ng quan l畉p tr狸nh Java C叩c c畉u tr炭c l畛nh tr棚n Java C畉u tr炭c i畛u khi畛n R畉 nh叩nh o Th動畛ng m畛t l畛nh break 動畛c d湛ng 畛 cu畛i danh s叩ch l畛nh c畛a m畛i case. M畛t c畉u tr炭c switch c坦 th畛 c坦 m畛t case default o K畉t qu畉 c畛a Expression trong switch ph畉i l ki畛u s畛 nguy棚n (byte, short, int, long) ho畉c char. Kh担ng th畛 l boolean ho畉c s畛 th畛c (float, double) 24
  • 25. 2.3. T畛ng quan l畉p tr狸nh Java C叩c c畉u tr炭c l畛nh tr棚n Java C畉u tr炭c l畉p while condition) { Statements; } do { Statements; }while (condition); for (VarInit; Condition; Statements) { Statements1; } 25
  • 26. 2.3. T畛ng quan l畉p tr狸nh Java Vi畉t ph動董ng th畛c trong Java C炭 ph叩p: V鱈 d畛 1: Vi畉t ch動董ng tr狸nh t鱈nh t畛ng c叩c s畛 nguy棚n ch畉n t畛 1-> N. N 動畛c nh畉p t畛 bn ph鱈m. Y棚u c畉u vi畉t ph動董ng th畛c t鱈nh t畛ng. V鱈 d畛 2: Vi畉t ch動董ng tr狸nh t鱈nh t畛ng c叩c s畛 nguy棚n t畛 t畛 1-> N. N 動畛c nh畉p t畛 bn ph鱈m. Y棚u c畉u vi畉t ph動董ng th畛c ki畛m tra nguy棚n t畛, ph動董ng th畛c t鱈nh t畛ng. 26 ReturnType methodName (Type agr1, Type arg2,) { return (somethingOfReturnType); }
  • 27. 2.3. T畛ng quan l畉p tr狸nh Java M畉ng L m畛t c畉u tr炭c d畛 li畛u cho ph辿p l動u m畛t t畉p c叩c gi叩 tr畛 c湛ng ki畛u 27 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 scores The entire array has a single name Each value has an index
  • 28. 2.3. T畛ng quan l畉p tr狸nh Java Khai b叩o m畉ng C炭 ph叩p: V鱈 d畛 khai b叩o m畉ng: o int[] scores = new int[100];// m畉ng scores ch畛a 100 ph畉n t畛 s畛 nguy棚n o String[] names; names = new String[20]; // m畉ng names ch畛a 20 chu畛i V鱈 d畛 khai b叩o v g叩n gi叩 tr畛 c湛ng l炭c: o int[] smallPrimes = { 2, 3, 5, 7, 11, 13 }; o char[] letterGrades = {'A', 'B', 'C', 'D', F'}; 28 DataType[] arrayName = new DataType[size]; DataType[] arrayName; // only declares the variable arrayName = new DataType[size];
  • 29. 2.3. T畛ng quan l畉p tr狸nh Java S畛 d畛ng m畉ng Duy畛t m畉ng a: V鱈 d畛: o for (int i=0; i < scores.length; i++) System.out.println(scores[i]); o for (int score : scores) System.out.println(score); o for (String n : names) System.out.println(n); Bi t畉p: t畉o m畉ng s畛 nguy棚n c坦 10 ph畉n t畛, g叩n gi叩 tr畛 b畉t k畛 v xu畉t m畉ng. 29 for (int i=0; i < a.length; i++) // process a[i] for (Type x : a) // process x length field returns the number of elements
  • 30. 2.4. Ngo畉i l畛 Exception l m畛t s畛 ki畛n x畉y ra trong qu叩 tr狸nh th畛c thi ch動董ng tr狸nh, ph叩 v畛 lu畛ng b狸nh th動畛ng c畛a ch動董ng tr狸nh V鱈 d畛: 30
  • 31. 2.4. Ngo畉i l畛 Ph但n lo畉i Trong Java c坦 2 lo畉i exception l Checked Exception v Unchecked Exception o Checked Exception: l c叩c exception x畉y ra t畉i th畛i i畛m compile. Nh畛ng exception ny th動畛ng li棚n quan 畉n l畛i c炭 ph叩p (syntax) v b畉t bu畛c ch炭ng ta ph畉i "b畉t" (catch) n坦. o Unchecked Exception: l c叩c exception x畉y ra t畉i th畛i i畛m runtime. Nh畛ng exception ny th動畛ng li棚n quan 畉n l畛i logic v kh担ng b畉t bu畛c ch炭ng ta ph畉i "b畉t" (catch) n坦. 31
  • 32. 2.4. Ngo畉i l畛 X畛 l箪 ngo畉i l畛 trong Java Keyword o try catch o finally o throw o throws 32
  • 33. 2.4. Ngo畉i l畛 S畛 d畛ng try...catch Kh畛i try ... catch": Ph但n t叩ch o畉n ch動董ng tr狸nh th担ng th動畛ng v ph畉n x畛 l箪 ngo畉i l畛 o ExceptionType: l t棚n l畛p ngo畉i l畛 c畉n x畛 l箪, n畉u kh担ng c畉n x叩c 畛nh c畛 th畛 th狸 s畛 d畛ng class Exception 33 try { // o畉n l畛nh c坦 kh畉 nng x畉y ra ngo畉i l畛 } catch (ExceptionType e) { // x畛 l箪 ngo畉i l畛 (th担ng b叩o l畛i ho畉c n辿m ngo畉i l畛 ra b棚n ngoi) }
  • 34. 2.4. Ngo畉i l畛 S畛 d畛ng try...catch: V鱈 d畛 1 34 public class Zero { public static void main (String[] args) { try { int tu = 10; int mau = 0; System.out.println (tu/mau); System.out.println ("This text will not be printed."); } catch (ArithmeticException e) { System.out.println ("Kh担ng th畛 chia cho 0"); // tb l畛i } } }
  • 35. 2.4. Ngo畉i l畛 S畛 d畛ng try...catch: Nhi畛u kh畛i catch M畛t o畉n m達 c坦 th畛 x畉y ra nhi畛u ngo畉i l畛: S畛 d畛ng nhi畛u kh畛i catch ExceptionType1 ph畉i l l畛p con ho畉c ngang hng v畛i ExceptionType2 (trong c但y ph但n c畉p k畉 th畛a) 35 try { // o畉n m達 c坦 th畛 x畉y ra nhi畛u ngo畉i l畛 } catch (ExceptionType1 e1) { // X畛 l箪 ngo畉i l畛 1 } catch (ExceptionType2 e2) { // X畛 l箪 ngo畉i l畛 2 } ...
  • 36. 2.4. Ngo畉i l畛 S畛 d畛ng try...catch: V鱈 d畛 2 36 import java.util.Scanner; class Zero2 { public static void main (String args[]){ int no1, no2; try { Scanner sc = new Scanner (System.in); System.out.print("Input no1: "); no1 = sc.nextInt(); System.out.print("Input no2: "); no2 = sc.nextInt(); System.out.println("Division result is: " + no1/no2); } catch (ArithmeticException e) { System.out.println("Cannot divide by zero"); } catch (InputMismatchException e) { System.out.println("You must input an integer"); } } }
  • 37. 2.4. Ngo畉i l畛 Kh畛i finally 畉m b畉o th畛c hi畛n t畉t c畉 c叩c c担ng vi畛c c畉n thi畉t d湛 ngo畉i l畛 c坦 x畉y ra hay kh担ng o 坦ng file, 坦ng socket, connection o Gi畉i ph坦ng ti nguy棚n (n畉u c畉n)... 37 try { // o畉n m達 c坦 th畛 g但y ra ngo畉i l畛 } catch (ExceptionType e) { // X畛 l箪 ngo畉i l畛 } finally { /* Th畛c hi畛n t畉t c畉 c叩c c担ng vi畛c d湛 ngo畉i l畛 c坦 x畉y ra hay kh担ng */ }
  • 38. 2.4. Ngo畉i l畛 S畛 d畛ng throw S畛 d畛ng throw khi ng動畛i l畉p tr狸nh mu畛n t畛 ph叩t sinh ra ngo畉i l畛, vd: 38 public static void main (String[] args) { try { final int MIN = 25, MAX = 40; Scanner scan = new Scanner (System.in); System.out.print ("Enter an integer value between " + MIN + " and " + MAX + ", inclusive: "); int value = scan.nextInt(); if (value < MIN || value > MAX) throw new Exception(); // make an exception else System.out.println("Gia tri nhap hop le"); System.out.println ("End of main method."); // may never be reached } catch( Exception x ) { // catch the exception System.out.println("Number is not valid."); }
  • 39. 2.4. Ngo畉i l畛 S畛 d畛ng throws Trong tr動畛ng h畛p ph動董ng th畛c c坦 ph叩t sinh ra ngo畉i l畛, n畉u kh担ng mu畛n b畉t n坦 ngay (nh動 trong v鱈 d畛 tr動畛c) th狸 s畛 d畛ng throws 畛 b叩o cho ch動董ng tr狸nh bi畉t l ph動董ng th畛c ny s畉 n辿m ra ngo畉i l畛 39 method1() { try { invoke method2; } catch (Exception ex) { Process exception; } } method2() throws Exception { if (an error occurs) { throw new Exception(); } } catch exception throw exception declare exception
  • 40. 2.4. Ngo畉i l畛 S畛 d畛ng throws: V鱈 d畛 40 public class DelegateExceptionDemo { public static void main(String args[]){ try { int num = calculate(9,3); System.out.println(Lan 1: + num); num = calculate(9,0); System.out.println(Lan 2: + num); } catch(Exception e) { System.out.println(e.getMessage()); } } static int calculate(int tu, int mau) throws ArithmeticException { if (mau== 0) throw new ArithmeticException("Khong the chia cho 0!"); int num = tu/ mau; return num; } }
  • 41. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n L畛p Scanner N畉m trong g坦i java.util C担ng d畛ng: 畛c d畛 li畛u t畛 bn ph鱈m C叩ch d湛ng: o T畉o 畛i t動畛ng 畛 畛c t畛 bn ph鱈m: Scanner scan = new Scanner (System.in); o D湛ng c叩c ph動董ng th畛c c畛a Scanner 畛 nh畉p d畛 li畛u t畛 bn ph鱈m, nh動: string answer = scan.nextLine(); Nh畉p m畛t chu畛i k箪 t畛 int a = scan.nextInt(); Nh畉p m畛t s畛 nguy棚n double d = scan.nextDouble(); Nh畉p m畛t s畛 th畛c 41
  • 42. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n L畛p Scanner: v鱈 d畛 import java.util.*; public class Cong2so { public static void main (String[] args) { Scanner nhap = new Scanner(System.in); int x,y; System.out.print("Nhap so thu nhat: "); x = nhap.nextInt(); System.out.print("Nhap so thu hai: "); y = nhap.nextInt(); int tong = x+y; System.out.println ("The sum is: "+ tong); } } 際際滷 42
  • 43. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n L畛p Random L畛p Random n畉m trong g坦i java.util C担ng d畛ng: D湛ng 畛 ph叩t sinh s畛 ng畉u nhi棚n C叩ch d湛ng: o T畉o 畛i t動畛ng: Random rd = new Random(); o Ph叩t sinh s畛 ng畉u nhi棚n c坦 mi畛n gi叩 tr畛 thu畛c [0, n-1] int a = rd.nextInt(n); o Ph叩t sinh s畛 ng畉u nhi棚n c坦 mi畛n gi叩 tr畛 thu畛c [0.0, 1.0) float a = rd.nextFloat(); Vd/ Ph叩t sinh 3 s畛 a,b,c thu畛c [0,50], t狸m s畛 l畛n nh畉t. 43
  • 44. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n L畛p Math L畛p Math n畉m trong g坦i java.lang (g坦i m畉c 畛nh) Ch畛a c叩c ph動董ng th畛c c坦 ch畛c nng t鱈nh to叩n v畛 to叩n h畛c: o pow: L滴y th畛a o sqrt: Cn b畉c 2 o abs: Tr畛 tuy畛t 畛i o C叩c ph動董ng th畛c trong l畛p Math l nh畛ng ph動董ng th畛c t挑nh (static methods). V狸 v畉y, ch畛 c畉n g畛i tr畛c ti畉p ph動董ng th畛c th担ng qua t棚n l畛p m kh担ng c畉n t畉o 畛i t動畛ng o V鱈 d畛: double value = Math.sqrt(25.0); 44
  • 45. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n 畛nh d畉ng k畉t qu畉 xu畉t ra mn h狸nh L畛p NumberFormat cho ph辿p 畛nh d畉ng gi叩 tr畛 theo ki畛u ti畛n t畛 ho畉c ph畉n trm L畛p DecimalFormat cho ph辿p 畛nh d畉ng gi叩 tr畛 theo m畉u 畛nh d畉ng cho tr動畛c C畉 2 n畉m trong g坦i java.text 45
  • 46. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n 畛nh d畉ng k畉t qu畉 xu畉t ra mn h狸nh import java.text.NumberFormat; import java.util.*; public class Price { public static void main (String[] args) { final double TAX_RATE = 0.06; // 6% sales tax int quantity; double subtotal, tax, totalCost, unitPrice; Scanner scan = new Scanner(System.in); System.out.print ("So luong: "); quantity = scan.nextInt(); System.out.print ("Don gia: "); unitPrice = scan.nextDouble(); subtotal = quantity * unitPrice; tax = subtotal * TAX_RATE; totalCost = subtotal + tax; // Print output with appropriate formatting NumberFormat money = NumberFormat.getCurrencyInstance(); NumberFormat percent = NumberFormat.getPercentInstance(); System.out.println ("Thanh tien: " + money.format(subtotal)); System.out.println ("Thue: " + money.format(tax) + " at " + percent.format(TAX_RATE)); System.out.println ("Tong tien: " + money.format(totalCost)); } } So luong: 5 Don gia: 3.87 Thanh tien: $19.35 Thue: $1.16 at 6% Tong tien: $20.51 46
  • 47. 2.5. S畛 d畛ng m畛t s畛 l畛p c坦 s畉n 畛nh d畉ng k畉t qu畉 xu畉t ra mn h狸nh import java.text.DecimalFormat; import java.util.*; public class CircleStats { public static void main (String[] args) { int radius; double area, circumference; Scanner scan = new Scanner(System.in); System.out.print ("Enter the circle's radius: "); radius = scan.nextInt(); area = Math.PI * Math.pow(radius, 2); circumference = 2 * Math.PI * radius; // Round the output to three decimal places DecimalFormat fmt = new DecimalFormat ("0.###"); System.out.println ("The circle's area: " + fmt.format(area)); System.out.println ("The circle's circumference: " + fmt.format(circumference)); } } Enter the circle's radius: 5 The circle's area: 78.54 The circle's circumference: 31.416 47
  • 48. 48
  • 49. Ki畉n th畛c c畉n n畉m 1. Xu畉t (d湛ng c畛ng chu畛i print/println v c坦 畛nh d畉ng printf) 2. Nh畉p (c叩c lo畉i gi叩 tr畛) (際際滷 41, 42) 3. Ph但n bi畛t c叩c ki畛u d畛 li畛u (際際滷 11-14) 4. Khai b叩o bi畉n, h畉ng (際際滷 15-17), m畉ng (際際滷 27-29) 5. Ph但n bi畛t c叩ch d湛ng bi畉n ton c畛c, bi畉n c畛c b畛 (際際滷 18) 6. Ngo畉i l畛: b畉t ngo畉i l畛, n辿m ngo畉i l畛 (際際滷 30-40) 7. Vi畉t ph動董ng th畛c (際際滷 26) 49
  • 50. Review questions 1. C畉n ci ch動董ng tr狸nh g狸 畛 vi畉t Java? 2. Hm b畉t 畉u ch畉y ch動董ng tr狸nh Java c坦 t棚n l g狸? C炭 ph叩p hm nh動 th畉 no? 3. C坦 bao nhi棚u ki畛u d畛 li畛u c董 s畛 trong Java? Bao nhi棚u ki畛u d畛 li畛u tham chi畉u? 4. 4 ki畛u d畛 li畛u s畛 nguy棚n trong Java? 5. To叩n t畛 new d湛ng 畛 lm g狸? 6. Ki畛u chu畛i trong Java c坦 t棚n l g狸? Thu畛c v畛 ki畛u d畛 li畛u g狸? 7. K畛 t棚n m畛t s畛 ki畛u d畛 li畛u tham chi畉u. 8. Ngo畉i l畛 l g狸. Lm th畉 no 畛 x畛 l箪 ngo畉i l畛. 9. Lm sao b畉t 動畛c ngo畉i l畛, n辿m ngo畉i l畛. Khi no c畉n n辿m ngo畉i l畛. 50
  • 51. Demo 1. Vi畉t ch動董ng tr狸nh in ra d嘆ng ch畛 HELLO 2. Vi畉t ch動董ng tr狸nh in k畉t qu畉 (2 s畛 l畉) c畛a ph辿p chia 2 s畛 nguy棚n (s畛 c坦 s畉n ho畉c y棚u c畉u nh畉p) 3. Vi畉t ch動董ng tr狸nh n畛i 2 chu畛i v in ra k畉t qu畉 4. Vi畉t hm t鱈nh t畛ng c叩c s畛 ch畉n trong kho畉ng t畛 1 畉n n v 畛ng d畛ng 5. Vi畉t hm t鱈nh t畛ng c叩c s畛 ch畉n trong m畉ng v 畛ng d畛ng 51
  • 52. Excercises Bi 1: Nh畉p vo 2 s畛 nguy棚n, in ra t畛ng, hi畛u, t鱈ch, th動董ng c畛a 2 s畛. Ch炭 箪 ki畛m tra s畛 th畛 2 c坦 kh叩c kh担ng hay kh担ng, n畉u b畉ng 0 th狸 th担ng b叩o Kh担ng th畛 chia cho 0!!. Y棚u c畉u ki畛m tra d畛 li畛u nh畉p ph畉i l s畛. Bi 2: Gi畉i ph動董ng tr狸nh b畉c nh畉t. Bi 3: Vi畉t ch動董ng tr狸nh t鱈nh t畛ng c叩c s畛 nguy棚n t畛 t畛 1-> N. N 動畛c nh畉p t畛 bn ph鱈m. Y棚u c畉u vi畉t ph動董ng th畛c ki畛m tra s畛 nguy棚n t畛, ph動董ng th畛c t鱈nh t畛ng, ki畛m tra gi叩 tr畛 N. Bi 4: Nh畉p vo m畛t chu畛i. 畉m s畛 k箪 t畛, s畛 t畛 trong chu畛i v畛a nh畉p. In m畛i t畛 tr棚n m畛t d嘆ng. Y棚u c畉u vi畉t hm cho t畛ng thao t叩c. Bi 5: C叩c bi t畉p Module 1 52