You are on page 1of 25

NGÔN NGỮ LẬP TRÌNH JAVA

RẤT CẢM ƠN CÁC BẠN ĐÃ ĐỌC!


CHÚC CÁC BẠN HỌC TẬP THẬT TỐT!
NẾU THẤY HAY CÁC BẠN CÓ THỂ SHARE LINK VỚI BẠN BÈ ĐỂ CHÚNG
TA CÙNG NHAU HỌC TẬP NHA!

Java_8: Cách sử dụng Stack trong lập trình Java

package main;

import java.util.Iterator;
import java.util.Scanner;
import java.util.Stack;

public class Test {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

Stack<String> stackChuoi = new Stack<String>();

// stackChuoi.push("giatri") => đưa giá trị vào


stack
// stackChuoi.pop() => lấy giá trị ra, xóa khỏi
stack
// stackChuoi.peek() => lấy giá trị ra, nhưng không
xóa khỏi stack
// stackChuoi.clear(); => xóa tất cả phần tử trong
stack
// stackChuoi.contains("giatri")=> xác định giá trị
có tồn tại trong stack hay không
// stackChuoi.size() => độ lớn của stack

By: Vũ Minh Quân 1


NGÔN NGỮ LẬP TRÌNH JAVA
// Ví dụ đảo ngược chuỗi
System.out.println("Nhập vào chuỗi: ");
String s = sc.nextLine();
// TITV
for(int i=0; i<s.length(); i++) {
stackChuoi.push(s.charAt(i)+"");
}
System.out.println("Chuỗi đảo ngược: ");
for(int i=0; i<s.length(); i++) {
System.out.print(stackChuoi.pop());
}
System.out.println();
// Ví dụ chuyển từ hệ thập phân sang hệ nhị phân
Stack<String> stackSoDu = new Stack<String>();
System.out.println("Nhập 1 số nguyên dương từ bàn
phím");
int x = sc.nextInt();
while(x>0) {
int soDu = x%2;
System.out.println(soDu);
stackSoDu.push(soDu+"");
x = x/2;
}
System.out.println("Số nhị phân là:");
int n = stackSoDu.size();
for(int i=0; i<n; i++) {
System.out.print(stackSoDu.pop());
}

}
}

Java_9: Hàng đợi Queue và Deque trong lập


trình Java

By: Vũ Minh Quân 2


NGÔN NGỮ LẬP TRÌNH JAVA

Lí thuyết

By: Vũ Minh Quân 3


NGÔN NGỮ LẬP TRÌNH JAVA

By: Vũ Minh Quân 4


NGÔN NGỮ LẬP TRÌNH JAVA

Code mẫu
package main;

import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

public class ViduPriorityQueue {


public static void main(String[] args) {

By: Vũ Minh Quân 5


NGÔN NGỮ LẬP TRÌNH JAVA
Queue<String> danhSachSV = new
PriorityQueue<String>();

danhSachSV.offer("TITV 1");
danhSachSV.offer("Nguyen Van B");
danhSachSV.offer("Nguyen Van A");
danhSachSV.offer("TITV 2");

while(true) {
String ten = danhSachSV.poll(); // => lấy
ra và xóa
if(ten==null) {
break;
}
//peek => lấy ra nhưng không xóa.
System.out.println(ten);
}
}
}

package main;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

public class ViduDeque {


public static void main(String[] args) {
Deque<String> danhSachSV = new
ArrayDeque<String>();

danhSachSV.offer("TITV 1");
danhSachSV.offer("Nguyen Van A");
danhSachSV.offer("Nguyen Van B");
danhSachSV.offer("TITV 2");
danhSachSV.offerLast("TITV 4");
danhSachSV.offerFirst("TITV 0");

By: Vũ Minh Quân 6


NGÔN NGỮ LẬP TRÌNH JAVA

// TITV 4 > TITV 2 > Nguyen Van B > Nguyen Van A


> TITV 1 > TITV 0

while(true) {
String ten = danhSachSV.poll(); // => lấy
ra và xóa
if(ten==null) {
break;
}
//peek => lấy ra nhưng không xóa.
System.out.println(ten);
}

}
}

Java_10: Hiểu rõ cấu trúc SET trong lập trình


Java

By: Vũ Minh Quân 7


NGÔN NGỮ LẬP TRÌNH JAVA

Code mẫu: Rút thăm trúng thưởng


package main;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class RutThamTrungThuong_HashSet {


Set<String> thungPhieuDuThuong = new
HashSet<String>();

public RutThamTrungThuong_HashSet() {
}

public boolean themPhieu(String giaTri) {


return this.thungPhieuDuThuong.add(giaTri);
}

public boolean xoaPhieu(String giaTri) {


return this.thungPhieuDuThuong.remove(giaTri);
}

public boolean kiemTraPhieuTonTai(String giaTri) {


return this.thungPhieuDuThuong.contains(giaTri);
}

public void xoaTatCaPhieuDuThuong() {


this.thungPhieuDuThuong.clear();
}

public int laySoLuong() {


return this.thungPhieuDuThuong.size();
}

public String rutMotPhieu() {


String ketQua = "";

By: Vũ Minh Quân 8


NGÔN NGỮ LẬP TRÌNH JAVA
Random rd = new Random();
int viTri =
rd.nextInt(this.thungPhieuDuThuong.size());
ketQua = (String)
this.thungPhieuDuThuong.toArray()[viTri];
return ketQua;
}

public void inTatCa() {

System.out.println(Arrays.toString(this.thungPhieuDuThuong.t
oArray()));
}

public static void main(String[] args) {


int luaChon = 0;
Scanner sc = new Scanner(System.in);
RutThamTrungThuong_HashSet rt = new
RutThamTrungThuong_HashSet();
do {

System.out.println("--------------------------------------")
;
System.out.println("MENU: ");
System.out.println("1. Thêm mã số dự
thưởng.");
System.out.println("2. Xóa mã số dự
thưởng.");
System.out.println("3. Kiểm tra mã số dự
thưởng có tồn tại hay không?");
System.out.println("4. Xóa tất cả các
phiếu dự thưởng.");
System.out.println("5. Số lượng phiếu dự
thưởng.");
System.out.println("6. Rút thăm trúng
thưởng.");

By: Vũ Minh Quân 9


NGÔN NGỮ LẬP TRÌNH JAVA
System.out.println("7. In ra tất cả các
phiếu.");
System.out.println("0. Thoát khỏi chương
trình");
luaChon = sc.nextInt();
sc.nextLine();
if(luaChon==1 || luaChon==2||luaChon==3) {
System.out.println("Nhập vào mã
phiếu dự thưởng: ");
String giaTri = sc.nextLine();
if(luaChon==1) {
rt.themPhieu(giaTri);
}else if(luaChon==2) {
rt.xoaPhieu(giaTri);
}else {
System.out.println("Kết quả
kiểm tra: "+rt.kiemTraPhieuTonTai(giaTri));
}
}else if(luaChon==4) {
rt.xoaTatCaPhieuDuThuong();
}else if(luaChon==5) {
System.out.println("Số lượng phiếu
là: "+ rt.laySoLuong());
}else if(luaChon==6) {
System.out.println("Mã số trúng
thưởng là: "+ rt.rutMotPhieu());
}else if(luaChon==7) {
System.out.println("Các mã phiếu dự
thưởng là: ");
rt.inTatCa();
}
}while(luaChon!=0);
}
}

_________

By: Vũ Minh Quân 10


NGÔN NGỮ LẬP TRÌNH JAVA
package main;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class RutThamTrungThuong_TreeSet {


Set<String> thungPhieuDuThuong = new
TreeSet<String>();

public RutThamTrungThuong_TreeSet() {
}

public boolean themPhieu(String giaTri) {


return this.thungPhieuDuThuong.add(giaTri);
}

public boolean xoaPhieu(String giaTri) {


return this.thungPhieuDuThuong.remove(giaTri);
}

public boolean kiemTraPhieuTonTai(String giaTri) {


return this.thungPhieuDuThuong.contains(giaTri);
}

public void xoaTatCaPhieuDuThuong() {


this.thungPhieuDuThuong.clear();
}

public int laySoLuong() {


return this.thungPhieuDuThuong.size();
}

public String rutMotPhieu() {


String ketQua = "";
Random rd = new Random();

By: Vũ Minh Quân 11


NGÔN NGỮ LẬP TRÌNH JAVA
int viTri =
rd.nextInt(this.thungPhieuDuThuong.size());
ketQua = (String)
this.thungPhieuDuThuong.toArray()[viTri];
return ketQua;
}

public void inTatCa() {

System.out.println(Arrays.toString(this.thungPhieuDuThuong.t
oArray()));
}

public static void main(String[] args) {


int luaChon = 0;
Scanner sc = new Scanner(System.in);
RutThamTrungThuong_TreeSet rt = new
RutThamTrungThuong_TreeSet();
do {

System.out.println("--------------------------------------")
;
System.out.println("MENU: ");
System.out.println("1. Thêm mã số dự
thưởng.");
System.out.println("2. Xóa mã số dự
thưởng.");
System.out.println("3. Kiểm tra mã số dự
thưởng có tồn tại hay không?");
System.out.println("4. Xóa tất cả các
phiếu dự thưởng.");
System.out.println("5. Số lượng phiếu dự
thưởng.");
System.out.println("6. Rút thăm trúng
thưởng.");

By: Vũ Minh Quân 12


NGÔN NGỮ LẬP TRÌNH JAVA
System.out.println("7. In ra tất cả các
phiếu.");
System.out.println("0. Thoát khỏi chương
trình");
luaChon = sc.nextInt();
sc.nextLine();
if(luaChon==1 || luaChon==2||luaChon==3) {
System.out.println("Nhập vào mã
phiếu dự thưởng: ");
String giaTri = sc.nextLine();
if(luaChon==1) {
rt.themPhieu(giaTri);
}else if(luaChon==2) {
rt.xoaPhieu(giaTri);
}else {
System.out.println("Kết quả
kiểm tra: "+rt.kiemTraPhieuTonTai(giaTri));
}
}else if(luaChon==4) {
rt.xoaTatCaPhieuDuThuong();
}else if(luaChon==5) {
System.out.println("Số lượng phiếu
là: "+ rt.laySoLuong());
}else if(luaChon==6) {
System.out.println("Mã số trúng
thưởng là: "+ rt.rutMotPhieu());
}else if(luaChon==7) {
System.out.println("Các mã phiếu dự
thưởng là: ");
rt.inTatCa();
}
}while(luaChon!=0);
}
}

By: Vũ Minh Quân 13


NGÔN NGỮ LẬP TRÌNH JAVA

Java_11: Cấu trúc Map của Java

By: Vũ Minh Quân 14


NGÔN NGỮ LẬP TRÌNH JAVA

Code mẫu: Tra từ điển Anh – Việt


package main;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;

public class TuDien {


private Map<String, String> duLieu = new TreeMap<String,
String>();
// private Map<String, String> duLieu = new
HashMap<String, String>();

public String themTu(String tuKhoa, String yNghia) {


return this.duLieu.put(tuKhoa, yNghia);
}

public String xoaTu(String tuKhoa) {


return this.duLieu.remove(tuKhoa);
}

public String traTu(String tuKhoa) {


String yNghia = this.duLieu.get(tuKhoa);
return yNghia;
}

public void inTuKhoa() {


Set<String> tapHopTuKhoa = this.duLieu.keySet();

System.out.println(Arrays.toString(tapHopTuKhoa.toArray()));
}

public int laySoLuong() {


return this.duLieu.size();

By: Vũ Minh Quân 15


NGÔN NGỮ LẬP TRÌNH JAVA
}

public void xoaTatCa() {


this.duLieu.clear();
}

public static void main(String[] args) {


TuDien tuDien = new TuDien();
int luaChon =0;
Scanner sc = new Scanner(System.in);
do {
System.out.println("---------------");
System.out.println("MENU ");
System.out.println("Tra từ điển Anh - Việt:\n"
+ "1. Thêm từ (Từ khóa, Ý nghĩa)\n"
+ "2. Xóa từ\n"
+ "3. Tìm ý nghĩa của từ khóa ⇒ Tra
từ\n"
+ "4. In ra danh sách từ khóa\n"
+ "5. Lấy số lượng từ\n"
+ "6. Xóa tất cả các từ khóa\n"
+ "0. Thoát khỏi chương trình\n"
+ "");
luaChon = sc.nextInt(); sc.nextLine();
if(luaChon==1) {
System.out.println("Nhập vào từ khóa: ");
String tuKhoa = sc.nextLine();
System.out.println("Nhập vào ý nghĩa: ");
String yNghia = sc.nextLine();
tuDien.themTu(tuKhoa, yNghia);
}else if(luaChon==2 || luaChon==3) {
System.out.println("Nhập vào từ khóa: ");
String tuKhoa = sc.nextLine();
if(luaChon==2) {
tuDien.xoaTu(tuKhoa);
}else {
System.out.println("Ý nghĩa là: "+
tuDien.traTu(tuKhoa));

By: Vũ Minh Quân 16


NGÔN NGỮ LẬP TRÌNH JAVA
}
}else if(luaChon==4) {
tuDien.inTuKhoa();
}else if(luaChon==5) {
System.out.println("Số lượng từ khóa là:
"+tuDien.laySoLuong());
}else if(luaChon==6) {
tuDien.xoaTatCa();
}
}while(luaChon!=0);
}
}

Java_12: Generic trong lập trình Java

Code mẫu
package main;

public class Box {


private int value;

public Box(int value) {


this.value = value;
}

/**
* @return the value
*/
public int getValue() {
return value;
}

By: Vũ Minh Quân 17


NGÔN NGỮ LẬP TRÌNH JAVA
/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}

package main;

public class TestBox {


public static void main(String[] args) {
Box box = new Box(15);
System.out.println("Giá trị: "+ box.getValue());

// Box box = new Box(15.5); => Báo lỗi


// Box box = new Box("Mười lăm")=> Báo lỗi

}
}

package main;

public class Box2<T> {


private T value;

public Box2(T value) {


this.value = value;
}

/**
* @return the value
*/
public T getValue() {
return value;

By: Vũ Minh Quân 18


NGÔN NGỮ LẬP TRÌNH JAVA
}

/**
* @param value the value to set
*/
public void setValue(T value) {
this.value = value;
}


package main;

public class TestBox2 {


public static void main(String[] args) {
Box2 b1 = new Box2<Integer>(15);
System.out.println("Giá trị: "+ b1.getValue());

Box2 b2 = new Box2<String>("Hello TITV!");


System.out.println("Giá trị: "+ b2.getValue());

Box2 b3 = new Box2<Double>(15.5);


System.out.println("Giá trị: "+ b3.getValue());

}
}
package main;

import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet;

public class TestJava {


public static void main(String[] args) {
ArrayList<String> list = new
ArrayList<String>();
ArrayList<Double> list2 = new
ArrayList<Double>();

By: Vũ Minh Quân 19


NGÔN NGỮ LẬP TRÌNH JAVA
Set<Float> set = new TreeSet<Float>();
}
}

Java_13: Cách lập trình tạo tập tin và thư mục
trong Java

Code mẫu
package main;

import java.io.File;
import java.io.IOException;

public class ViDuTaoTapTinVaThuMuc {


public static void main(String[] args) {
// Lưu ý:
// MS Windows: \ => \\ | Ví dụ: "C:\\Thu muc 1\\
Thuc muc\\Ten tap tin.xxx";
// Linux, MacOS: / | Ví dụ: /Thu muc 1/Thuc muc
2 /Ten tap tin.xxx"

// Kiểm tra thư mục hoặc tập tin có tồn tại hay
không?
File folder1 = new File("/Users/mac/eclipse-
workspace/Java_67");
File folder2 = new File("/Users/mac/eclipse-
workspace/Java_68");
System.out.println("Kiểm tra folder1 có tồn tại hay
không: "+ folder1.exists());
System.out.println("Kiểm tra folder2 có tồn tại hay
không: "+ folder2.exists());

// Tạo thư mục

By: Vũ Minh Quân 20


NGÔN NGỮ LẬP TRÌNH JAVA
// Phương thức mkdir() => Tạo 1 thư mục
File d1 = new
File("/Users/mac/eclipse-workspace/Java_67/Directory_1");
d1.mkdir();

// Phương thức mkdirs() => Tạo nhiều thư mục cùng


lúc
File d2 = new
File("/Users/mac/eclipse-workspace/Java_67/Directory_1/Direc
tory_2/Directory_3/Directory_4");
//d2.mkdir();
d2.mkdirs();

// Tạo tập tin (có phần mở


rộng: .exe, .txt, .doc, .xls .....)
File file1 = new File("/Users/mac/eclipse-
workspace/Java_67/Directory_1/Vidu1.txt");
File file2 = new File("/Users\\mac\\eclipse-
workspace\\Java_67\\Directory_1\\Vidu2.txt");
try {
file1.createNewFile();
file2.createNewFile();
} catch (IOException e) {
// Không có quyền tạo tập tin
// Ổ cứng bị đầy
// Đường dẫn sai
e.printStackTrace();
}
}
}

Java_14: Cách lấy thông tin cơ bản của tập


tin và duyệt thư mục trong Java

Code mẫu

By: Vũ Minh Quân 21


NGÔN NGỮ LẬP TRÌNH JAVA
package main;

import java.io.File;
import java.util.Iterator;
import java.util.Scanner;

public class ViduFile {


File file;

public ViduFile(String tenFile) {


this.file = new File(tenFile);
}

public boolean kiemTraThucThi() {


// Kiểm tra file có thể thực thi
return this.file.canExecute();
}

public boolean kiemTraDoc() {


// Kiểm tra file có thể đọc
return this.file.canRead();
}

public boolean kiemTraGhi() {


// Kiểm tra file có thể ghi
return this.file.canWrite();
}

public void inDuongDan() {


System.out.println(this.file.getAbsolutePath());
}

public void inTen() {


System.out.println(this.file.getName());
}

public void kiemTraLaThuMucHoacTapTin() {

By: Vũ Minh Quân 22


NGÔN NGỮ LẬP TRÌNH JAVA
if (this.file.isDirectory()) {
System.out.println("Đây là thư mục");
} else if (this.file.isFile()) {
System.out.println("Đây là tập tin");
}
}

public void inDanhSachCacFileCon() {


if (this.file.isDirectory()) {
System.out.println("Các tập tin con/ thư mục
con là:");
File[] mangCon = this.file.listFiles();
for (File file : mangCon) {

System.out.println(file.getAbsolutePath());
}
} else if (this.file.isFile()) {
System.out.println("Đây là tập tin, không có
dữ liệu con bên trong.");
}
}

public void inCayThuMuc() {


this.inChiTietCayThucMuc(this.file, 1);
}

public void inChiTietCayThucMuc(File f, int bac) {


// System.out.print("|");
for (int i = 0; i < bac; i++) {
System.out.print("\t");
}
System.out.print("|__");
System.out.println(f.getName());
if (f.canRead()&&f.isDirectory()) {
File[] mangCon = f.listFiles();
for (File fx : mangCon) {
inChiTietCayThucMuc(fx, bac + 1);
}

By: Vũ Minh Quân 23


NGÔN NGỮ LẬP TRÌNH JAVA
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
int luaChon = 0;
System.out.println("Nhập tên file: ");
String tenFile = sc.nextLine();
ViduFile vdf = new ViduFile(tenFile);

do {
System.out.println("MENU ---------- ");
System.out.println("1. Kiểm tra file có thể
thực thi: ");
System.out.println("2. Kiểm tra file có thể
đọc: ");
System.out.println("3. Kiểm tra file có thể
ghi: ");
System.out.println("4. In đường dẫn: ");
System.out.println("5. In tên file: ");
System.out.println("6. Kiểm tra file là thư
mục hoặc tập tin: ");
System.out.println("7. In ra danh sách các
file con: ");
System.out.println("8. In ra cây thư mục: ");
System.out.println("0. Thoát chương trình.");
luaChon = sc.nextInt();
switch (luaChon) {
case 1:
System.out.println(vdf.kiemTraThucThi());
break;
case 2:
System.out.println(vdf.kiemTraDoc());
break;
case 3:
System.out.println(vdf.kiemTraGhi());
break;

By: Vũ Minh Quân 24


NGÔN NGỮ LẬP TRÌNH JAVA
case 4:
vdf.inDuongDan();
break;
case 5:
vdf.inTen();
break;
case 6:
vdf.kiemTraLaThuMucHoacTapTin();
break;
case 7:
vdf.inDanhSachCacFileCon();
break;
case 8:
vdf.inCayThuMuc();
break;
default:
break;
}

sc.nextLine();
} while (luaChon != 0);
}
}

By: Vũ Minh Quân 25

You might also like