By GokiSoft.com|
15:14 26/10/2022|
Java Advanced
[Source Code] Tìm hiểu Exception & Collection & Package trong Java - C2109I
https://meet.google.com/xji-euky-fid
Nội dung kiến thức:
- Exception
- Logic: chia 0 (5/0 -> Error -> Exception), int arr[] = new int[3] -> arr[4], arr[5] -> error -> Exception =)), NullPointerException
Student std = null;
- Runtime
- ...
- Package
- Collections
- List (ArrayList, Vector)
- Map (HashMap)
- Set
====================================================
Ví dụ:
- Class Animal: name, age, gender (Male/Female)
-> age < 0 => AgeException (msg: Age >= 0)
-> gender != Male/Female -> GenderException (msg: Gender is not correct)
-> OK -> Init object success
Bai toan:
Animal
gender: Male & Female
Light:
status: 1 -> SANG, 0 -> TAT => int
#AgeException.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.lesson01;
/**
*
* @author diepvan
*/
public class AgeException extends Exception{
public AgeException(String message) {
super(message);
}
}
#Animal.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.lesson01;
/**
*
* @author diepvan
*/
public class Animal {
public static final String GENDER_MALE = "Male";
public static final String GENDER_FEMALE = "Female";
enum GENDER {MALE, FEMALE};
String name;
int age;
String gender; //Male & Female
GENDER mGender;
public Animal(String name, int age, String gender) throws AgeException, GenderException {
this.name = name;
setAge(age);
setGender(gender);
mGender = GENDER.MALE;
System.out.println("Init object success");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) throws AgeException {
if(age < 0) {
throw new AgeException("Required: Age >= 0");
}
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) throws GenderException {
if(!gender.equals("Male") && !gender.equals("Female")) {
throw new GenderException("Gender is not correct");
}
this.gender = gender;
}
}
#GenderException.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.lesson01;
/**
*
* @author diepvan
*/
public class GenderException extends Exception{
public GenderException(String message) {
super(message);
}
}
#Light.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.lesson01;
/**
*
* @author diepvan
*/
public class Light {
enum STATUS {ON, OFF};
//status: 1 -> ON, 0 -> OFF
STATUS status;
}
#Main.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.lesson01;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author diepvan
*/
public class Main {
public static void main(String[] args) {
// test01();
test02();
}
static void test02() {
Animal animal;
try {
animal = new Animal("ABC", -10, "Male");
} catch (AgeException ex) {
System.out.println(ex.getMessage());
} catch (GenderException ex) {
System.out.println(ex.getMessage());
}
try {
animal = new Animal("ABC", 2, "AAA");
} catch (AgeException ex) {
System.out.println(ex.getMessage());
} catch (GenderException ex) {
System.out.println(ex.getMessage());
}
try {
animal = new Animal("ABC", 5, "Male");
} catch (AgeException ex) {
System.out.println(ex.getMessage());
} catch (GenderException ex) {
System.out.println(ex.getMessage());
}
}
static void test01() {
Scanner scan = new Scanner(System.in);
int x, y;
System.out.println("Nhap x = ");
x = scan.nextInt();
System.out.println("Nhap y = ");
y = scan.nextInt();
//C1: Kiem tra dieu kien cua y -> thuc hien phep chia (if/else) -> Su dung cach nay
try {
int s = x / y;
System.out.println("s = " + s);
} catch (ArithmeticException e) {
System.out.println("Xu ly exception ...");
} finally {
System.out.println("Finally ...");
}
System.out.println("Finish ...");
}
}
#Test.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.lesson01;
/**
*
* @author diepvan
*/
public class Test {
public static void main(String[] args) {
Light light = new Light();
light.status = Light.STATUS.ON;
}
}
#Test02.java
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package java2.lesson01;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.Vector;
/**
*
* @author diepvan
*/
public class Test02 {
public static void main(String[] args) {
//B1. Khai bao
ArrayList<Integer> list = new ArrayList<>();
Vector<Integer> list2 = new Vector<>();
list2.add(6);
//B2. Them phan tu vao trong mang
list.add(12);
list.add(2);
list.add(22);
list.add(62);
list.add(99); //Mang nay co bao nhieu phan tu??? -> 5 phan tu
//B3. Lay phan tu trog mang
System.out.println("In phan tu 1 > " + list.get(1));
for (int i = 0; i < list.size(); i++) {
System.out.println("In phan tu > " + list.get(i));
}
//B4. Xoa phan tu trong mang
//index = 1
list.remove(1);
//B5. Chen 1 phan tu vao trong mang -> index = 1: 100
list.add(1, 100);
//B6. Xoa ALL
list.clear();
Map<String, String> map = new HashMap<>();
//key: int, String
//value: all data type
map.put("fullname", "Tran Van A");
map.put("fullname", "Tran Van B");
System.out.println(map.get("fullname"));
map.remove("fullname");
for (Map.Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
String val = entry.getValue();
}
//Queue
Deque<String> stack = new ArrayDeque<>();
stack.push("1");
stack.push("2");
stack.push("3");
stack.push("4");
stack.push("5");
System.out.println(stack.pop());
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)