By GokiSoft.com|
20:08 13/03/2023|
Java Advanced
[Source Code] Tìm hiểu về Exception & String & Collections - C2006L
#ExceptionTest.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.gokisoft.lesson01;
import java.util.Scanner;
/**
*
* @author teacher
*/
public class ExceptionTest {
static enum STATUS {ON, OFF}; //using
static int status = 0;//status = 0 -> OFF, 1: ON -> kho maintain
/**
* @param args the command line arguments
* Unit Test:
* Nghi ra tat ca cac TH (cases)
* Input & Output
* Input: (x=8, y=2) -> s= 4 -> testcase01
* Input: (x=8, y=0) -> Exception -> testcase02
* Converage: 85-90%
*/
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Nhap x = ");
int x = Integer.parseInt(scan.nextLine());
System.out.println("Nhap y = ");
int y = Integer.parseInt(scan.nextLine());
try {
int s = x/y; //Trong TH: y = 0 -> crash (Exception)
System.out.println("s = " + s);
} catch(ArithmeticException e) {
System.out.println("Error -> devide / 0");
}
int[] t = {1, 5, 8}; //length = 3; index = 0 -> 2
System.out.println("t[1] = " + t[1]);
try {
System.out.println("t[5] = " + t[5]); //TH -> crash (Exception)
//Error -> logic -> Fix
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Error > index");
}
}
}
#Main.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.gokisoft.lesson01;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Vector;
/**
*
* @author teacher
*/
public class Main {
public static void main(String[] args) {
// Nội dung kiến thức:
// - Exception
// - Collections
// - List
// - ArrayList
// - Vector
// - Set
// - Map
// - HashMap
// - Stack & Queue
// - File
// - Thread
// - CSDL
// - MySQL
// - Java Swing / Java FX
// - Mục Khác
//===========================================================
//SET -> Collections
//ArrayList
ArrayList<Integer> list = new ArrayList<>();
Vector<Integer> list2 = new Vector<>(); //Su dung list & list2 nhu nhau
list2.add(2323);
//B1. Them phan tu vao trong mang
list.add(12);
list.add(22);
list.add(220);
//B2. Lay du lieu ra
System.out.println("0 -> " + list.get(0));
System.out.println("1 -> " + list.get(1));
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " -> " + list.get(i));
}
for (Integer v : list) {
System.out.println("v -> " + v);
}
//B3. Xoa phan tu trong mang
//12, 22, 220 -> remove (index = 1) -> del 22 -> don mang -> 12, 220
list.remove(1);
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " -> " + list.get(i));
}
//B4. Chen 1 phan vao trong mang
//12, 220 -> insert 900 (index = 1) -> 12, 900, 220
list.add(1, 900);
list.add(1, 300); //12, 300, 900, 220
for (Integer v : list) {
System.out.println("v -> " + v);
}
//SET
HashSet<String> setList = new HashSet<>();
setList.add("Xin chao");
setList.add("Xin 234");
setList.add("Xin 423423");
for (String v : setList) {
System.out.println("v -> " + v);
}
//Map
HashMap<String, String> maps = new HashMap<>();
maps.put("fullname", "TRAN VAN A");
maps.put("fullname", "TRAN VAN B");
maps.put("age", "30");
String fname = maps.get("fullname");
System.out.println(fname);
System.out.println(maps.get("ABC"));
for (Map.Entry<String, String> entry : maps.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
System.out.println("key = " + key + ", value = " + value);
}
}
}
#StringTest.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.gokisoft.lesson01;
/**
*
* @author teacher
*/
public class StringTest {
public static void main(String[] args) {
//Cach 1
String s = "Sinh vien Aptech 54 Le Thanh Nghi";
System.out.println(s);
s += " OKOK";
System.out.println(s);
//Cach 2
StringBuilder builder = new StringBuilder();//Construct -> Giong Nhau
builder.append("Sinh vien Aptech");//Xu ly bat dong bo
builder.append(" 54 Le Thanh Nghi");
System.out.println(builder.toString());
//Cach 3StringBuffer
StringBuffer buffer = new StringBuffer();
buffer.append("Sinh vien Aptech");//Xu ly dong bo
buffer.append(" 54 Le Thanh Nghi");
System.out.println(buffer.toString());
}
}
#Test.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.gokisoft.lesson01;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author teacher
*/
public class Test {
public static void main(String[] args) {
String s = "Tran Van Diep";
//Kiem tra s -> tuan thu cu phap: [a-zA-Z (space)] -> 6 -> 30
String check = "^[a-zA-Z ]{6,30}$";
Pattern pattern = Pattern.compile(check);
Matcher matcher = pattern.matcher(s);
if(matcher.find()) {
System.out.println("Du lieu chinh xac");
} else {
System.out.println("Du lieu sai");
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)