├── Exercises1 ├── Link.txt ├── Java exercises part 1.pdf ├── Bai4 │ └── Bai4.java ├── Bai7 │ └── Bai7.java ├── Bai9 │ └── Bai9.java ├── Bai8 │ └── Bai8.java ├── Bai15 │ └── Bai15.java ├── Bai6 │ └── Bai6.java ├── Bai12 │ └── Bai12.java ├── Bai1 │ └── Bai1.java ├── Bai13 │ └── Bai13.java ├── Bai2 │ └── Bai2.java ├── Bai10 │ └── Bai10.java ├── Bai5 │ └── Bai5.java ├── Bai3 │ └── Bai3.java ├── Bai11 │ └── Bai11.java └── Bai14 │ └── Bai14.java ├── HD.jpg ├── Nội dung loạt bài.pdf ├── Exercises3 ├── BMApp_Full │ ├── BMApp.rar │ ├── BMApp_func_1_2.rar │ └── BMApp_func_3_4.rar ├── BMApp part2_3 │ └── BMApp.rar ├── Java exercise part 3.pdf └── Books Managerment App │ └── BooksManagement.rar ├── Final Testt └── Java final test.pdf ├── Connect MSSQL Server ├── Tutorial.bacpac └── TestConnectDB.java ├── Exercises2 ├── Java exercises part 2.pdf ├── Bai1 │ └── Bai1.java ├── Bai19 │ └── Bai19.java ├── Bai12 │ └── Bai12.java ├── Bai16 │ └── Bai16.java ├── Bai17 │ └── Bai17.java ├── Bai6 │ └── Bai6.java ├── Bai3 │ └── Bai3.java ├── Bai4 │ └── Bai4.java ├── Bai8 │ └── Bai8.java ├── Bai5 │ └── Bai5.java └── Bai9 │ └── Bai9.java ├── Date Time Exercises ├── JAVA DATE TIME EXERCISES.docx └── JAVA DATE TIME EXERCISES.pdf ├── exception ├── Father.java ├── Child.java ├── user_defined │ ├── TestException.java │ ├── InvalidAgeException.java │ └── AgeOfDriver.java ├── runtime_exception │ ├── InvalidAgeException.java │ ├── Test.java │ └── AgeOfDriver.java ├── ThrowVSThrows.DAT ├── ThrowVSThrows.java ├── ThrowVSThrows.txt ├── TryWithResoure.java ├── TryCatchFinally.java └── TryCatchFinally.txt ├── java_interface ├── Animal.java ├── Bird.java ├── Cat.java ├── Fish.java ├── JavaInterfaceExample.java └── Dog.java ├── comparator_interface ├── SortByStudentID.java ├── SortByMark.java ├── SortByName.java └── ComparatorExample.java ├── ObjectInOutputStream ├── V1 │ ├── ReadSingleObjectExamle.java │ ├── SingleObjectInputOutputStreamExample.java │ └── Student.java ├── V2 │ ├── ReadSingleObjectExamle.java │ ├── SingleObjectInputOutputStreamExample.java │ └── Student.java └── V3 │ ├── ReadSingleObjectExamle.java │ ├── Student.java │ └── SingleObjectInputOutputStreamExample.java ├── comparable_interface └── ComparableExample.java ├── other └── BaiTapVidu.java ├── connect_db ├── TestConnectDB.java └── example.sql ├── Student.java └── model └── Student.java /Exercises1/Link.txt: -------------------------------------------------------------------------------- 1 | https://github.com/thantrieu/JavaExercise.git -------------------------------------------------------------------------------- /HD.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/HD.jpg -------------------------------------------------------------------------------- /Nội dung loạt bài.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Nội dung loạt bài.pdf -------------------------------------------------------------------------------- /Exercises3/BMApp_Full/BMApp.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Exercises3/BMApp_Full/BMApp.rar -------------------------------------------------------------------------------- /Final Testt/Java final test.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Final Testt/Java final test.pdf -------------------------------------------------------------------------------- /Exercises3/BMApp part2_3/BMApp.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Exercises3/BMApp part2_3/BMApp.rar -------------------------------------------------------------------------------- /Exercises3/Java exercise part 3.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Exercises3/Java exercise part 3.pdf -------------------------------------------------------------------------------- /Connect MSSQL Server/Tutorial.bacpac: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Connect MSSQL Server/Tutorial.bacpac -------------------------------------------------------------------------------- /Exercises1/Java exercises part 1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Exercises1/Java exercises part 1.pdf -------------------------------------------------------------------------------- /Exercises2/Java exercises part 2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Exercises2/Java exercises part 2.pdf -------------------------------------------------------------------------------- /Exercises3/BMApp_Full/BMApp_func_1_2.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Exercises3/BMApp_Full/BMApp_func_1_2.rar -------------------------------------------------------------------------------- /Exercises3/BMApp_Full/BMApp_func_3_4.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Exercises3/BMApp_Full/BMApp_func_3_4.rar -------------------------------------------------------------------------------- /Date Time Exercises/JAVA DATE TIME EXERCISES.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Date Time Exercises/JAVA DATE TIME EXERCISES.docx -------------------------------------------------------------------------------- /Date Time Exercises/JAVA DATE TIME EXERCISES.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Date Time Exercises/JAVA DATE TIME EXERCISES.pdf -------------------------------------------------------------------------------- /Exercises3/Books Managerment App/BooksManagement.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/thantrieu/JavaExercise/HEAD/Exercises3/Books Managerment App/BooksManagement.rar -------------------------------------------------------------------------------- /exception/Father.java: -------------------------------------------------------------------------------- 1 | package exception; 2 | 3 | public class Father { 4 | public void showInfo(Object message) throws NullPointerException{ 5 | System.out.println(message); 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /java_interface/Animal.java: -------------------------------------------------------------------------------- 1 | package java_interface; 2 | 3 | import java.io.Serializable; 4 | import java.util.Collection; 5 | 6 | public interface Animal { 7 | void move(); 8 | void eat(); 9 | void sleep(); 10 | } 11 | -------------------------------------------------------------------------------- /java_interface/Bird.java: -------------------------------------------------------------------------------- 1 | package java_interface; 2 | 3 | public abstract class Bird implements Animal { 4 | 5 | @Override 6 | public void move() { 7 | System.out.println("Bird move by flying"); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /exception/Child.java: -------------------------------------------------------------------------------- 1 | package exception; 2 | 3 | public class Child extends Father { 4 | @Override 5 | public void showInfo(Object message) throws NullPointerException { 6 | super.showInfo(message); 7 | // do something 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /exception/user_defined/TestException.java: -------------------------------------------------------------------------------- 1 | package exception.user_defined; 2 | 3 | public class TestException { 4 | public static void main(String[] args) throws InvalidAgeException { 5 | AgeOfDriver ageOfDriver; 6 | ageOfDriver = new AgeOfDriver(12); 7 | System.out.println(ageOfDriver.getAge()); 8 | 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /comparator_interface/SortByStudentID.java: -------------------------------------------------------------------------------- 1 | package java_interface.comparator_interface; 2 | 3 | import model.Student; 4 | 5 | import java.util.Comparator; 6 | 7 | public class SortByStudentID implements Comparator { 8 | @Override 9 | public int compare(Student o1, Student o2) { 10 | return o1.getStudentID().compareTo(o2.getStudentID()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Exercises1/Bai4/Bai4.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | public class Bai4 { 4 | public static void main(String[] args) { 5 | for(int i = 1; i< 10; i++){ 6 | for(int j = 0; j<= 10; j++){ 7 | System.out.printf("%2d x %2d = %3d\n", i, j, (i*j)); 8 | } 9 | System.out.println("______________________________"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /exception/user_defined/InvalidAgeException.java: -------------------------------------------------------------------------------- 1 | package exception.user_defined; 2 | 3 | public class InvalidAgeException extends Exception { 4 | private String message; 5 | 6 | public InvalidAgeException(String message) { 7 | super(message); 8 | this.message = message; 9 | } 10 | 11 | @Override 12 | public String getMessage() { 13 | return message; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /exception/runtime_exception/InvalidAgeException.java: -------------------------------------------------------------------------------- 1 | package exception.runtime_exception; 2 | 3 | public class InvalidAgeException extends RuntimeException { 4 | private String message; 5 | 6 | public InvalidAgeException(String message) { 7 | super(message); 8 | this.message = message; 9 | } 10 | 11 | @Override 12 | public String getMessage() { 13 | return message; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /exception/runtime_exception/Test.java: -------------------------------------------------------------------------------- 1 | package exception.runtime_exception; 2 | 3 | public class Test { 4 | public static void main(String[] args) { 5 | try { 6 | AgeOfDriver ageOfDriver = new AgeOfDriver(20); 7 | System.out.println("age valid: " + ageOfDriver.getAge()); 8 | }catch (InvalidAgeException e){ 9 | System.out.println(e.getMessage()); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /java_interface/Cat.java: -------------------------------------------------------------------------------- 1 | package java_interface; 2 | 3 | public class Cat implements Animal { 4 | @Override 5 | public void move() { 6 | System.out.println("Run as fast as possible."); 7 | } 8 | 9 | @Override 10 | public void eat() { 11 | System.out.println("Cat like Mose"); 12 | } 13 | 14 | @Override 15 | public void sleep() { 16 | System.out.println("Sleep any where he like"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java_interface/Fish.java: -------------------------------------------------------------------------------- 1 | package java_interface; 2 | 3 | public class Fish implements Animal { 4 | @Override 5 | public void move() { 6 | System.out.println("Fish is swimming"); 7 | } 8 | 9 | @Override 10 | public void eat() { 11 | System.out.println("Fish like ...."); 12 | } 13 | 14 | @Override 15 | public void sleep() { 16 | System.out.println("Fish sleep when she is stoped"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /java_interface/JavaInterfaceExample.java: -------------------------------------------------------------------------------- 1 | package java_interface; 2 | 3 | // interface trong java 4 | //mục đích: đạt đc tính trừu tượng hoàn toàn 5 | 6 | public class JavaInterfaceExample { 7 | public static void main(String[] args) { 8 | Animal animal = new Fish(); 9 | var animal1 = new Cat(); 10 | var animal2 = new Dog(); 11 | 12 | animal1.sleep(); 13 | animal2.sleep(); 14 | animal.move(); 15 | 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Exercises1/Bai7/Bai7.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai7 { 6 | public static void main(String[] args) { 7 | int n; 8 | Scanner scanner = new Scanner(System.in); 9 | n = scanner.nextInt(); 10 | 11 | double sum = 0; 12 | for(int i = 1; i<= n; i++){ 13 | sum+= (double)1/i;// chu y ep kieu cho chinh xac 14 | } 15 | 16 | System.out.printf("RESULT= %8.3f",sum); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Exercises1/Bai9/Bai9.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai9 { 6 | public static void main(String[] args) { 7 | int n; 8 | Scanner scanner = new Scanner(System.in); 9 | n = scanner.nextInt(); 10 | int sum = 0; 11 | int x; 12 | while (n > 0) { 13 | x = n % 10; 14 | n /= 10; 15 | sum += x; 16 | } 17 | System.out.println("SUM = " + sum); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /comparator_interface/SortByMark.java: -------------------------------------------------------------------------------- 1 | package java_interface.comparator_interface; 2 | 3 | import model.Student; 4 | 5 | import java.util.Comparator; 6 | 7 | public class SortByMark implements Comparator { 8 | @Override 9 | public int compare(Student o1, Student o2) { 10 | float res = o1.getStudentAVGMark() - o2.getStudentAVGMark(); 11 | if(res > 0){ 12 | return -1; // do nothing 13 | }else if(res < 0){ 14 | return 1; // swap o1, o2 15 | } 16 | return 0; 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Exercises1/Bai8/Bai8.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai8 { 6 | public static void main(String[] args) { 7 | int n; 8 | Scanner scanner = new Scanner(System.in); 9 | n = scanner.nextInt(); 10 | 11 | int count = 0; 12 | for (int i = 1; i <= n; i++) {//xet uoc la so duong 13 | if (n % i == 0) { 14 | count++; 15 | System.out.print(i + " "); 16 | } 17 | } 18 | System.out.println("\nn co " + count + " uoc so"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /comparator_interface/SortByName.java: -------------------------------------------------------------------------------- 1 | package java_interface.comparator_interface; 2 | 3 | import model.Student; 4 | 5 | import java.util.Comparator; 6 | 7 | public class SortByName implements Comparator { 8 | @Override 9 | public int compare(Student o1, Student o2) { 10 | String name1 = o1.getStudentName().substring(o1.getStudentName().lastIndexOf(" ")); 11 | String name2 = o2.getStudentName().substring(o2.getStudentName().lastIndexOf(" ")); 12 | return name1.compareTo(name2); 13 | // if name1 greater than name2 then swap 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Exercises1/Bai15/Bai15.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai15 { 6 | public static void main(String[] args) { 7 | double c; 8 | Scanner scanner = new Scanner(System.in); 9 | c = scanner.nextDouble(); 10 | double pi = 0; 11 | long n = 0; 12 | double x = 1.0 / (n * 2 + 1); 13 | while (c <= x) { 14 | pi += Math.pow(-1, n)*x; 15 | n++; 16 | x = 1.0 / (2 * n + 1); 17 | } 18 | pi *= 4; 19 | System.out.println("PI = " + pi); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Exercises1/Bai6/Bai6.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.time.LocalDate; 4 | import java.util.Scanner; 5 | 6 | public class Bai6 { 7 | public static void main(String[] args) { 8 | int n, i; 9 | Scanner scanner = new Scanner(System.in); 10 | n = scanner.nextInt(); 11 | 12 | if(n % 2 ==0){// neu n chan 13 | i = 2; 14 | }else{ // n le 15 | i = 1; 16 | } 17 | long sum = 0; 18 | for(; i <= n; i+= 2){ 19 | sum+= i; 20 | } 21 | System.out.println("RESULT = " + sum); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /java_interface/Dog.java: -------------------------------------------------------------------------------- 1 | package java_interface; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Dog implements Animal, Serializable, Comparable { 6 | @Override 7 | public void move() { 8 | System.out.println("Dogs move by his hands"); 9 | } 10 | 11 | @Override 12 | public void eat() { 13 | System.out.println("Dogs eat rices"); 14 | } 15 | 16 | @Override 17 | public void sleep() { 18 | System.out.println("Dogs sleep in daylight"); 19 | } 20 | 21 | @Override 22 | public int compareTo(Dog o) { 23 | return 0; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /exception/runtime_exception/AgeOfDriver.java: -------------------------------------------------------------------------------- 1 | package exception.runtime_exception; 2 | 3 | public class AgeOfDriver { 4 | private int age; 5 | 6 | public AgeOfDriver(int age) throws InvalidAgeException{ 7 | setAge(age); 8 | } 9 | 10 | public AgeOfDriver() { 11 | } 12 | 13 | public int getAge() { 14 | return age; 15 | } 16 | 17 | public void setAge(int age) throws InvalidAgeException { 18 | if(age >= 16){ 19 | this.age = age; 20 | }else{ 21 | throw new InvalidAgeException("Age must greater than or equals 16"); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Exercises1/Bai12/Bai12.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai12 { 6 | public static void main(String[] args) { 7 | int n; 8 | Scanner scanner = new Scanner(System.in); 9 | n = scanner.nextInt(); 10 | long f0 = 0, f1 = 1, fn = 0; 11 | for (int i = 0; i <= n; i++) { 12 | if (i < 2) { 13 | System.out.println(i); 14 | } else { 15 | fn = f0 + f1; 16 | f0 = f1; 17 | f1 = fn; 18 | System.out.println(fn); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Exercises1/Bai1/Bai1.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai1 { 6 | public static void main(String[] args) { 7 | int m, n; 8 | Scanner scanner = new Scanner(System.in); 9 | m = scanner.nextInt();// chieu cao hinh chu nhat 10 | n = scanner.nextInt();// chieu rong hinh chu nhat 11 | 12 | for(int i = 0; i< m;i++){// tang hang 13 | for(int j = 0; j< n; j++){// tang theo cot 14 | System.out.print(" * "); 15 | //************* 16 | } 17 | System.out.println();// in xuong dong 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Exercises1/Bai13/Bai13.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | public class Bai13 { 4 | public static void main(String[] args) { 5 | int count = 0; 6 | for (int i = 100000; i < 999999; i++) { 7 | int tmp = 0; 8 | int n = i; 9 | while(n>0){ 10 | tmp = tmp*10 + n%10; 11 | n/= 10; 12 | } 13 | 14 | if(i == tmp){ 15 | System.out.printf("%10d", i); 16 | count++; 17 | if(count %10 == 0){ 18 | System.out.println(); 19 | } 20 | } 21 | 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Exercises1/Bai2/Bai2.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai2 { 6 | public static void main(String[] args) { 7 | int m, n; 8 | Scanner scanner = new Scanner(System.in); 9 | m = scanner.nextInt(); 10 | n = scanner.nextInt(); 11 | 12 | for (int i = 0; i < m; i++) { 13 | for (int j = 0; j < n; j++) { 14 | if (i == 0 || j == 0 || i == m - 1 || j == n - 1) { 15 | System.out.print(" * "); 16 | } else { 17 | System.out.print(" "); 18 | } 19 | } 20 | System.out.println(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Exercises1/Bai10/Bai10.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai10 { 6 | public static void main(String[] args) { 7 | int n = new Scanner(System.in).nextInt(); 8 | int i = 2; 9 | if (n == 1) { 10 | System.out.println(n + " = " + n); 11 | } else { 12 | System.out.print(n + " = "); 13 | while (n != 1) { 14 | if (n % i == 0) { 15 | System.out.print(i + " x "); 16 | n /= i; 17 | } else { 18 | ++i; 19 | } 20 | } 21 | System.out.print("\b\b\b"); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /exception/user_defined/AgeOfDriver.java: -------------------------------------------------------------------------------- 1 | package exception.user_defined; 2 | /* 3 | Ngoại lệ do người dùng tự định nghĩa 4 | Goal of this video: write checked exception 5 | */ 6 | public class AgeOfDriver { 7 | private int age; 8 | 9 | public AgeOfDriver(int age) throws InvalidAgeException { 10 | setAge(age); 11 | } 12 | 13 | public int getAge() { 14 | return age; 15 | } 16 | 17 | public void setAge(int age) throws InvalidAgeException { 18 | if(age < 18){ 19 | throw new InvalidAgeException("Age must be greater or equals 18"); 20 | }else{ 21 | this.age = age; 22 | } 23 | } 24 | 25 | public AgeOfDriver() { 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Exercises1/Bai5/Bai5.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Random; 4 | import java.util.Scanner; 5 | 6 | public class Bai5 { 7 | public static void main(String[] args) { 8 | int input; 9 | Scanner scanner = new Scanner(System.in); 10 | input = scanner.nextInt(); 11 | int count = 0; 12 | Random random = new Random(); 13 | while (true) { 14 | int randNumber = random.nextInt(100); // [0,99] 15 | count++; 16 | System.out.println("Random number: " + randNumber + ", input: " + input); 17 | if (randNumber == input) { 18 | System.out.println("Thuc hien " + count + " lan."); 19 | break; 20 | } 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Exercises1/Bai3/Bai3.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai3 { 6 | public static void main(String[] args) { 7 | int h; 8 | Scanner scanner = new Scanner(System.in); 9 | h = scanner.nextInt(); 10 | int x, output; 11 | 12 | for(int i = 1;i<= h; i++){ 13 | for(int j = 0; j<= 2*h; j++){ 14 | x = j-h; 15 | if(x < 0){ 16 | x *= -1; 17 | } 18 | output = i - x; 19 | 20 | if(output > 0){ 21 | System.out.printf("%3d", output); 22 | } else{ 23 | System.out.printf(" "); 24 | } 25 | } 26 | System.out.println(); 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Exercises1/Bai11/Bai11.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai11 { 6 | public static void main(String[] args) { 7 | int n; 8 | n = new Scanner(System.in).nextInt(); 9 | int count = 0; 10 | int x = 2; 11 | while(true){ 12 | if(isNgTo(x)){ 13 | System.out.print(x+" "); 14 | count++; 15 | } 16 | if(count == n){ 17 | break; 18 | } 19 | if(x == 2) 20 | x++; 21 | else{ 22 | x+= 2; 23 | } 24 | } 25 | } 26 | 27 | public static boolean isNgTo(int n) { 28 | for (int i = 2; i <= Math.sqrt(n); i++) { 29 | if (n % i == 0){ 30 | return false; 31 | } 32 | } 33 | return true; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /exception/ThrowVSThrows.DAT: -------------------------------------------------------------------------------- 1 | package exception; 2 | 3 | import java.io.FileNotFoundException; 4 | 5 | /* 6 | throw vs throws: 7 | - Phân biệt? 8 | - Cách sử dụng? 9 | - Khi nào sử dụng? 10 | */ 11 | public class ThrowVSThrows { 12 | public static double div(int a, int b) { 13 | if (b == 0) { 14 | throw new ArithmeticException("divide by zero"); 15 | } 16 | return 1.0 * a / b; 17 | } 18 | 19 | public static void countTime(int n) throws InterruptedException { 20 | for (int i = 0; i < n; i++) { 21 | Thread.sleep(1000); 22 | System.out.println(i); 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | try { 28 | var res = div(10, 0); 29 | } catch (ArithmeticException e){ 30 | System.out.println(e.getMessage()); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /exception/ThrowVSThrows.java: -------------------------------------------------------------------------------- 1 | package exception; 2 | 3 | import java.io.FileNotFoundException; 4 | 5 | /* 6 | throw vs throws: 7 | - Phân biệt? 8 | - Cách sử dụng? 9 | - Khi nào sử dụng? 10 | */ 11 | public class ThrowVSThrows { 12 | public static double div(int a, int b) { 13 | if (b == 0) { 14 | throw new ArithmeticException("divide by zero"); 15 | } 16 | return 1.0 * a / b; 17 | } 18 | 19 | public static void countTime(int n) throws InterruptedException { 20 | for (int i = 0; i < n; i++) { 21 | Thread.sleep(1000); 22 | System.out.println(i); 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | try { 28 | var res = div(10, 0); 29 | } catch (ArithmeticException e){ 30 | System.out.println(e.getMessage()); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /exception/ThrowVSThrows.txt: -------------------------------------------------------------------------------- 1 | package exception; 2 | 3 | import java.io.FileNotFoundException; 4 | 5 | /* 6 | throw vs throws: 7 | - Phân biệt? 8 | - Cách sử dụng? 9 | - Khi nào sử dụng? 10 | */ 11 | public class ThrowVSThrows { 12 | public static double div(int a, int b) { 13 | if (b == 0) { 14 | throw new ArithmeticException("divide by zero"); 15 | } 16 | return 1.0 * a / b; 17 | } 18 | 19 | public static void countTime(int n) throws InterruptedException { 20 | for (int i = 0; i < n; i++) { 21 | Thread.sleep(1000); 22 | System.out.println(i); 23 | } 24 | } 25 | 26 | public static void main(String[] args) { 27 | try { 28 | var res = div(10, 0); 29 | } catch (ArithmeticException e){ 30 | System.out.println(e.getMessage()); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Exercises2/Bai1/Bai1.java: -------------------------------------------------------------------------------- 1 | //package exercise2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai1 { 6 | public static void main(String[] args) { 7 | int[] arr; 8 | int n; 9 | Scanner scanner = new Scanner(System.in); 10 | n = scanner.nextInt(); 11 | arr = new int[n]; 12 | for(int i = 0; i< n; i++){ 13 | arr[i] = scanner.nextInt(); 14 | } 15 | // ktra doi xung 16 | boolean ok = true; 17 | int mid = n/2; 18 | int x; 19 | for(int i = 0; i<= mid; i++){ 20 | x = n-1-i; 21 | if(arr[i] != arr[x]){ 22 | ok = false; 23 | break; 24 | } 25 | } 26 | if(ok == true){ 27 | System.out.println("Mang doi xung."); 28 | } 29 | else{ 30 | System.out.println("Mang khong doi xung."); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Exercises1/Bai14/Bai14.java: -------------------------------------------------------------------------------- 1 | //package exercise1; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai14 { 6 | public static void main(String[] args) { 7 | int m, n; 8 | Scanner scanner = new Scanner(System.in); 9 | m = scanner.nextInt(); 10 | n = scanner.nextInt(); 11 | int count = 0; 12 | if ((m >= 0 && n >= 0) || n >= 0) { 13 | for (int i = m; i <= n; i++) { 14 | if(i>=0){ 15 | int res = (int)Math.sqrt(i); 16 | if(res*res == i){ 17 | count++; 18 | System.out.printf("%10d", i); 19 | if(count %10 == 0){ 20 | System.out.println(); 21 | } 22 | } 23 | } 24 | } 25 | } 26 | System.out.println("\nSo luong so chinh phuong thoa man: " + count); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Exercises2/Bai19/Bai19.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | import java.nio.file.Paths; 6 | import java.util.Arrays; 7 | import java.util.Scanner; 8 | 9 | public class Bai19 { 10 | public static void main(String[] args) throws IOException { 11 | Scanner inputFromFile = new Scanner(Paths.get("input.txt"), 12 | "UTF-8"); 13 | PrintWriter printWriter = new PrintWriter("output.txt", "UTF-8"); 14 | 15 | String ex; 16 | while (inputFromFile.hasNextLine()){ 17 | ex = inputFromFile.nextLine();// read all line 18 | String [] exs = ex.split(" "); 19 | Arrays.sort(exs); 20 | for (String s: exs) { 21 | printWriter.print(s+" "); 22 | } 23 | // printWriter.print("\b"); 24 | printWriter.println(); 25 | } 26 | // dong ket noi voi file 27 | inputFromFile.close(); 28 | printWriter.close(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /exception/TryWithResoure.java: -------------------------------------------------------------------------------- 1 | package exception; 2 | 3 | // try with resource 4 | 5 | import java.io.File; 6 | import java.io.FileNotFoundException; 7 | import java.io.PrintWriter; 8 | import java.io.UnsupportedEncodingException; 9 | import java.util.Scanner; 10 | 11 | public class TryWithResoure { 12 | public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException { 13 | try (Scanner readFile = new Scanner( 14 | new File( 15 | "C:\\Users\\ASUS\\IdeaProjects\\JavaTutorial2\\src\\exception\\ThrowVSThrows.java"), 16 | "UTF8"); 17 | PrintWriter printWriter = 18 | new PrintWriter( 19 | "C:\\Users\\ASUS\\IdeaProjects\\JavaTutorial2\\src\\exception\\ThrowVSThrows.txt", 20 | "UTF-8")) { 21 | while(readFile.hasNextLine()){ 22 | var input = readFile.nextLine(); 23 | printWriter.println(input); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Exercises2/Bai12/Bai12.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai12 { 6 | /* 7 | Các bước thực hiện: 8 | - Tạo mảng đối sánh tương ứng với hệ cơ số 9 | - Chuyển đổi cơ số-> tạo kết quả đảo 10 | - Đảo lại kết quả -> Done! 11 | */ 12 | public static final String BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVXYZW"; 13 | 14 | public static String result(int n, int b){ 15 | StringBuilder stringBuilder = new StringBuilder(); 16 | int x = 0; 17 | while(n > 0){ 18 | x = n%b; 19 | n/= b; 20 | stringBuilder.append(BASE.charAt(x)); 21 | } 22 | return stringBuilder.reverse().toString(); 23 | } 24 | 25 | public static void main(String[] args) { 26 | Scanner scanner = new Scanner(System.in); 27 | int n, b; 28 | System.out.println("Nhap gia tri, co so: "); 29 | n = scanner.nextInt(); 30 | b = scanner.nextInt(); 31 | 32 | System.out.println("KET QUA: " + result(n, b)); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /ObjectInOutputStream/V1/ReadSingleObjectExamle.java: -------------------------------------------------------------------------------- 1 | package object_input_output_stream; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.FileNotFoundException; 5 | import java.io.IOException; 6 | import java.io.ObjectInputStream; 7 | 8 | public class ReadSingleObjectExamle { 9 | public static void main(String[] args) { 10 | // doc file ObjectInputStream: 11 | Object obj = null; 12 | try { 13 | FileInputStream fis = new FileInputStream("STUDENT.DAT"); 14 | ObjectInputStream ois = new ObjectInputStream(fis); 15 | // doc doi tuong ra: 16 | obj = ois.readObject(); 17 | 18 | ois.close(); 19 | fis.close(); 20 | 21 | } catch (FileNotFoundException e) { 22 | e.printStackTrace(); 23 | } catch (IOException e) { 24 | e.printStackTrace(); 25 | } catch (ClassNotFoundException e) { 26 | e.printStackTrace(); 27 | } 28 | 29 | if(obj == null){ 30 | System.out.println("Nothing"); 31 | }else{ 32 | System.out.println(obj); 33 | } 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /exception/TryCatchFinally.java: -------------------------------------------------------------------------------- 1 | package exception; 2 | 3 | // try, catch, finally, throw, throws 4 | // Nội dung này sẽ tìm hiểu cách sử dụng của try-catch-finally 5 | /* 6 | Các trường hợp có thể xảy ra với try-catch or try-catch-finally: 7 | 1. Ngoại lệ xảy ra trong khối try và được xử lý trong khối catch 8 | 2. Ngoại lệ xảy ra trong khối try nhưng không được xử lý trong khối catch 9 | 3. Ngoại lệ không xảy ra trong khối try 10 | 11 | Các trường hợp có thể gặp với try-finally: 12 | 1. Ngoại lệ xảy ra trong khối try 13 | 2. Ngoại lệ không xảy ra trong khối try 14 | */ 15 | // catch (ArithmeticException e){ 16 | // System.out.println("Ngoại lệ: ArithmeticException. "); 17 | // System.out.println("Trong khối catch"); 18 | // } 19 | public class TryCatchFinally { 20 | public static void main(String[] args) { 21 | try { 22 | String str = "lalala"; 23 | System.out.println(str.length()); 24 | System.out.println("Trong khối try"); 25 | } finally { 26 | System.out.println("Trong khối finally"); 27 | } 28 | System.out.println("Ra ngoài khối catch"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /exception/TryCatchFinally.txt: -------------------------------------------------------------------------------- 1 | package exception; 2 | 3 | // try, catch, finally, throw, throws 4 | // Nội dung này sẽ tìm hiểu cách sử dụng của try-catch-finally 5 | /* 6 | Các trường hợp có thể xảy ra với try-catch or try-catch-finally: 7 | 1. Ngoại lệ xảy ra trong khối try và được xử lý trong khối catch 8 | 2. Ngoại lệ xảy ra trong khối try nhưng không được xử lý trong khối catch 9 | 3. Ngoại lệ không xảy ra trong khối try 10 | 11 | Các trường hợp có thể gặp với try-finally: 12 | 1. Ngoại lệ xảy ra trong khối try 13 | 2. Ngoại lệ không xảy ra trong khối try 14 | */ 15 | // catch (ArithmeticException e){ 16 | // System.out.println("Ngoại lệ: ArithmeticException. "); 17 | // System.out.println("Trong khối catch"); 18 | // } 19 | public class TryCatchFinally { 20 | public static void main(String[] args) { 21 | try { 22 | String str = "lalala"; 23 | System.out.println(str.length()); 24 | System.out.println("Trong khối try"); 25 | } finally { 26 | System.out.println("Trong khối finally"); 27 | } 28 | System.out.println("Ra ngoài khối catch"); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Exercises2/Bai16/Bai16.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai16 { 6 | public static void main(String[] args) { 7 | /* 8 | 1. Nhập dữ liệu vào 9 | 2. tách từ 10 | 3. tìm độ dài từng từ, nếu từ nào dài nhất thì gán: 11 | + lưu lại độ dài 12 | + lưu lại giá trị chỉ số của nó 13 | 4. Xuất ra kết quả. 14 | DONE! 15 | */ 16 | 17 | Scanner scanner = new Scanner(System.in); 18 | String input; 19 | //nhap du lieu vao 20 | input = scanner.nextLine();// doc ca dong 21 | // tach tu 22 | String[] inputs = input.split(" "); 23 | // tim do dai, dai max, index 24 | int index = 0; 25 | int lenMax = 0; // do dai max cua tu trong xau 26 | for (int i = 0; i < inputs.length; i++) { 27 | int len = inputs[i].length(); 28 | if (len > lenMax) { 29 | lenMax = len; 30 | index = i; 31 | } 32 | } 33 | //output 34 | System.out.println("Tu: " + inputs[index] + ", tu thu: " + (index + 1) 35 | + ", do dai: " + lenMax); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Exercises2/Bai17/Bai17.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import java.io.IOException; 4 | import java.io.PrintWriter; 5 | import java.nio.file.Paths; 6 | import java.util.Scanner; 7 | 8 | public class Bai17 { 9 | public static void main(String[] args) throws IOException { 10 | Scanner inputFromFile = new Scanner(Paths.get("input.txt"), 11 | "UTF-8"); 12 | PrintWriter printWriter = new PrintWriter("KETQUA.OUT", "UTF-8"); 13 | // xu ly file: 14 | while (inputFromFile.hasNextLine()) { 15 | String name = inputFromFile.nextLine(); 16 | name = name.trim(); 17 | String[] names = name.split(" "); 18 | StringBuilder builder = new StringBuilder(); 19 | builder.append(names[names.length - 1] + " "); 20 | builder.append(names[0] + " "); 21 | for (int i = 1; i < names.length - 1; i++) { 22 | if (names[i].length() != 0) { 23 | builder.append(names[i] + " "); 24 | } 25 | } 26 | 27 | name = builder.toString(); 28 | printWriter.println(name); 29 | } 30 | 31 | printWriter.close(); 32 | inputFromFile.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /ObjectInOutputStream/V2/ReadSingleObjectExamle.java: -------------------------------------------------------------------------------- 1 | //package object_input_output_stream; 2 | 3 | import model.Student; 4 | 5 | import java.io.FileInputStream; 6 | import java.io.FileNotFoundException; 7 | import java.io.IOException; 8 | import java.io.ObjectInputStream; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public class ReadSingleObjectExamle { 13 | public static void main(String[] args) { 14 | // doc file ObjectInputStream: 15 | // Object obj = null; 16 | try { 17 | FileInputStream fis = new FileInputStream("STUDENT.DAT"); 18 | ObjectInputStream ois = new ObjectInputStream(fis); 19 | // doc doi tuong ra: 20 | while(fis.available() > 0){ 21 | Student obj = (Student) ois.readObject(); 22 | System.out.println(obj); 23 | } 24 | 25 | ois.close(); 26 | fis.close(); 27 | 28 | } catch (FileNotFoundException e) { 29 | e.printStackTrace(); 30 | } catch (IOException e) { 31 | e.printStackTrace(); 32 | } catch (ClassNotFoundException e) { 33 | e.printStackTrace(); 34 | } 35 | 36 | // if(obj == null){ 37 | // System.out.println("Nothing"); 38 | // }else{ 39 | // System.out.println(obj); 40 | // } 41 | 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /comparable_interface/ComparableExample.java: -------------------------------------------------------------------------------- 1 | package java_interface.comparable_interface; 2 | 3 | /* 4 | Sử dụng Comparable interface 5 | */ 6 | 7 | import model.Student; 8 | 9 | import java.util.ArrayList; 10 | import java.util.Arrays; 11 | import java.util.Collections; 12 | 13 | public class ComparableExample { 14 | public static void main(String[] args) { 15 | ArrayList list = new ArrayList<>(); 16 | //String studentID, int studentYears, float studentAVGMark, 17 | // String studentName 18 | list.add(new Student("B19DCCN123", 4, 3.25f, 19 | "Tran Van Hung")); 20 | list.add(new Student("B19DCCN789", 4, 3.55f, 21 | "Nguyen Van Nam")); 22 | list.add(new Student("B19DCCN012", 4, 3.20f, 23 | "Le Van An")); 24 | list.add(new Student("B19DCCN339", 4, 3.19f, 25 | "Hoang Van Ngo")); 26 | list.add(new Student("B19DCCN412", 4, 3.89f, 27 | "Ngo Van Tung")); 28 | 29 | System.out.println("Truoc sap xep: "); 30 | for(var x: list){ 31 | System.out.println(x); 32 | } 33 | 34 | System.out.println("Sau khi sap xep: "); 35 | Collections.sort(list);// sap xep theo tieu chi duoc dua ra o pt 36 | for(var x: list){ 37 | System.out.println(x); 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /Exercises2/Bai6/Bai6.java: -------------------------------------------------------------------------------- 1 | //package exercise2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Bai6 { 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | int[] arr; 10 | int n, x; 11 | n = scanner.nextInt(); 12 | System.out.println("Nhap cac phan tu cua mang arr[] co "+ n + " phan tu: "); 13 | arr = nhap(n, scanner); 14 | System.out.println("Nhap x: "); 15 | x = scanner.nextInt(); 16 | 17 | int index = -1; 18 | for (int i = 0; i < n; i++) { 19 | if(arr[i] > x){ 20 | index = i; 21 | break; 22 | } 23 | } 24 | 25 | if(index == -1){ 26 | // chen vao cuoi 27 | arr[n] = x; 28 | }else{ 29 | //dich phai toan bo cac phan tu tu vi tri index 30 | for (int i = n; i > index ; i--) { 31 | arr[i] = arr[i-1]; 32 | } 33 | arr[index] = x; 34 | } 35 | 36 | System.out.println(Arrays.toString(arr)); 37 | } 38 | //[1 2 3 4 5 6 ] , x = 5 39 | 40 | public static int[] nhap(int n, Scanner scanner) { 41 | int[] x = new int[n + 1]; 42 | for (int i = 0; i < n; i++) { 43 | x[i] = scanner.nextInt(); 44 | } 45 | 46 | return x; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /other/BaiTapVidu.java: -------------------------------------------------------------------------------- 1 | package ngay_25_9; 2 | 3 | import java.text.ParseException; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | import java.util.Scanner; 7 | 8 | public class BaiTapVidu { 9 | /* 10 | Tính số ngày sai lệch giữa hai thời điểm cho trước 11 | Hướng dẫn: 12 | B1: Nhập vào hai giá trị string 13 | B2: Chuyển đổi các giá trị đầu vào từ String->Date qua lớp SimpleDateFormat 14 | B3: Chuyển hai thời điểm thành mili giây 15 | B4: Lấy trị tuyệt đối của hiệu hai mili giây này 16 | B5: Chia cho số mili giây trong một ngày => DONE! 17 | */ 18 | public static void main(String[] args) { 19 | Scanner scanner = new Scanner(System.in); 20 | String start = scanner.nextLine(); 21 | String end = scanner.nextLine(); 22 | 23 | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy"); 24 | 25 | try { 26 | 27 | Date startDate = simpleDateFormat.parse(start); 28 | Date endDate = simpleDateFormat.parse(end); 29 | 30 | long startValue = startDate.getTime(); 31 | long endValue = endDate.getTime(); 32 | long tmp = Math.abs(startValue- endValue); 33 | 34 | long result = tmp/(24*60*60*1000); 35 | 36 | System.out.println("So ngay chenh lech: " + result); 37 | 38 | } catch (ParseException e) { 39 | e.printStackTrace(); 40 | } 41 | 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /ObjectInOutputStream/V3/ReadSingleObjectExamle.java: -------------------------------------------------------------------------------- 1 | package object_input_output_stream; 2 | 3 | import model.Student; 4 | 5 | import java.io.FileInputStream; 6 | import java.io.FileNotFoundException; 7 | import java.io.IOException; 8 | import java.io.ObjectInputStream; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | public class ReadSingleObjectExamle { 13 | public static void main(String[] args) { 14 | // doc file ObjectInputStream: 15 | // Object obj = null; 16 | try { 17 | FileInputStream fis = new FileInputStream("STUDENT.DAT"); 18 | ObjectInputStream ois = new ObjectInputStream(fis); 19 | // doc doi tuong ra: 20 | // while(fis.available() > 0){ 21 | // Student obj = (Student) ois.readObject(); 22 | // System.out.println(obj); 23 | // } 24 | 25 | var resultList = (ArrayList)ois.readObject(); 26 | 27 | for (var obj : resultList) { 28 | System.out.println(obj); 29 | } 30 | 31 | ois.close(); 32 | fis.close(); 33 | 34 | } catch (FileNotFoundException e) { 35 | e.printStackTrace(); 36 | } catch (IOException e) { 37 | e.printStackTrace(); 38 | } catch (ClassNotFoundException e) { 39 | e.printStackTrace(); 40 | } 41 | 42 | // if(obj == null){ 43 | // System.out.println("Nothing"); 44 | // }else{ 45 | // System.out.println(obj); 46 | // } 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Exercises2/Bai3/Bai3.java: -------------------------------------------------------------------------------- 1 | //package exercise2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai3 { 6 | public static void main(String[] args) { 7 | int n, x, index = -1, min = Integer.MAX_VALUE; 8 | int[] arr; 9 | Scanner scanner = new Scanner(System.in); 10 | System.out.println("Nhap vao n, x: "); 11 | n = scanner.nextInt(); 12 | x = scanner.nextInt(); 13 | 14 | arr = new int[n]; 15 | // nhap cac phan tu cho mang 16 | for (int i = 0; i < n; i++) { 17 | arr[i] = scanner.nextInt(); 18 | } 19 | 20 | //kiem tra , tim kq 21 | for (int i = 0; i < n; i++) { 22 | int currentMin = getMin(arr[i], x); 23 | if (nguyenTo(arr[i]) && currentMin < min) { 24 | min = currentMin; 25 | index = i; 26 | } 27 | } 28 | 29 | if (index != -1) {// tim thay gia tri thoa man 30 | System.out.println("Chi so phan tu: " + index + ", Gia tri = " + arr[index]); 31 | } else { 32 | System.out.println("Khong ton tai gia tri thoa man!"); 33 | } 34 | } 35 | 36 | public static int getMin(int a, int x) { 37 | return Math.abs(a - x); 38 | } 39 | 40 | public static boolean nguyenTo(int n) { 41 | if (n < 2) { 42 | return false; 43 | } 44 | int a = (int) Math.sqrt(n); 45 | for (int i = 2; i <= a; i++) { 46 | if (n % i == 0) { 47 | return false; 48 | } 49 | } 50 | return true; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Exercises2/Bai4/Bai4.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | import java.util.Random; 3 | import java.util.Scanner; 4 | 5 | public class Bai4 { 6 | public static void main(String[] args) { 7 | int [] arr; 8 | int n; 9 | Scanner scanner = new Scanner(System.in); 10 | n = scanner.nextInt(); 11 | Random random = new Random(); 12 | arr = new int[n]; 13 | int count = 0;// dem 14 | while (count < n){// dem so cac so ngau nhien da dc sinh ra 15 | arr[count++] =random.nextInt(100);// gan so ngau nhien cho arr[count]; count++ 16 | } 17 | int [] brr = new int[n]; 18 | int bSize = 0; 19 | boolean isExist = false; 20 | for (int i = 0; i < n; i++) { 21 | isExist = false; 22 | for (int j = 0; j < bSize; j++) { 23 | if(arr[i] == brr[j]){ 24 | isExist = true; 25 | break; 26 | } 27 | } 28 | if (!isExist){ 29 | brr[bSize++] = arr[i]; 30 | } 31 | } 32 | 33 | System.out.println("Truoc khi sap xep: "); 34 | System.out.println(Arrays.toString(arr)); 35 | // thao tac 1 36 | Arrays.sort(arr); 37 | System.out.println("Sau khi sap xep: "); 38 | System.out.println(Arrays.toString(arr)); 39 | // thao tac 2 40 | brr = Arrays.copyOfRange(brr, 0, bSize); 41 | Arrays.sort(brr); // cat bo cac phan tu du thua 42 | System.out.println("Mang chi chua cac phan tu duy nhat: "); 43 | System.out.println(Arrays.toString(brr)); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /connect_db/TestConnectDB.java: -------------------------------------------------------------------------------- 1 | // package connect_db; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class TestConnectDB { 9 | public static void main(String[] args) { 10 | try { 11 | Class.forName("com.mysql.cj.jdbc.Driver"); 12 | String url = "jdbc:mysql://localhost:3306/example";// your db name 13 | String user="root"; // your db username 14 | String password=""; // your db password 15 | Connection conn = DriverManager.getConnection(url, user, password); 16 | if(conn != null){ 17 | System.out.println("Connect success!"); 18 | } 19 | 20 | var sql = "select * from user"; 21 | var statement = conn.prepareStatement(sql); 22 | var resultSet = statement.executeQuery(); 23 | showResutl(resultSet); 24 | } catch (ClassNotFoundException e) { 25 | e.printStackTrace(); 26 | } catch (SQLException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | 31 | private static void showResutl(ResultSet resultSet) throws SQLException { 32 | System.out.printf("%-10s%-20s%-20s%-30s\n", "ID", "Username", "Password", "Email"); 33 | while (resultSet.next()){ 34 | var id = resultSet.getInt("id"); 35 | var username = resultSet.getString("username"); 36 | var password = resultSet.getString("password"); 37 | var email = resultSet.getString("email"); 38 | 39 | System.out.printf("%-10d%-20s%-20s%-30s\n", id, username, password, email); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /ObjectInOutputStream/V2/SingleObjectInputOutputStreamExample.java: -------------------------------------------------------------------------------- 1 | //package object_input_output_stream; 2 | 3 | import model.Student; 4 | 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.ObjectOutputStream; 9 | import java.util.ArrayList; 10 | 11 | /* 12 | Đọc ghi một đối tượng đơn vào/từ file sử dụng ObjectInput/OutputStream 13 | */ 14 | public class SingleObjectInputOutputStreamExample { 15 | public static void main(String[] args) { 16 | 17 | Student student = new Student("B19DCCN123", 4, 18 | 3.21f, "Trieu Van Than"); 19 | Student student1 = new Student("B19DCCN155", 4, 20 | 3.29f, "Le Van Hung"); 21 | Student student2 = new Student("B19DCCN223", 4, 22 | 3.51f, "Le Van Luong"); 23 | Student student3 = new Student("B19DCCN523", 4, 24 | 3.31f, "Ngo Van Tai"); 25 | Student student4 = new Student("B19DCCN188", 4, 26 | 3.02f, "Truong Trong Nghia"); 27 | 28 | // ghi file: 29 | try { 30 | FileOutputStream fos = new FileOutputStream("STUDENT.DAT"); 31 | ObjectOutputStream oos = new ObjectOutputStream(fos); 32 | 33 | oos.writeObject(student); 34 | oos.writeObject(student1); 35 | oos.writeObject(student2); 36 | oos.writeObject(student3); 37 | oos.writeObject(student4); 38 | 39 | oos.close(); 40 | fos.close(); 41 | } catch (FileNotFoundException e) { 42 | e.printStackTrace(); 43 | } catch (IOException e) { 44 | e.printStackTrace(); 45 | } 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /ObjectInOutputStream/V1/SingleObjectInputOutputStreamExample.java: -------------------------------------------------------------------------------- 1 | package object_input_output_stream; 2 | 3 | import model.Student; 4 | 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.ObjectOutputStream; 9 | import java.util.ArrayList; 10 | 11 | /* 12 | Đọc ghi một đối tượng đơn vào/từ file sử dụng ObjectInput/OutputStream 13 | */ 14 | public class SingleObjectInputOutputStreamExample { 15 | public static void main(String[] args) { 16 | 17 | Student student = new Student("B19DCCN123", 4, 18 | 3.21f, "Trieu Van Than"); 19 | // Student student1 = new Student("B19DCCN155", 4, 20 | // 3.29f, "Le Van Hung"); 21 | // Student student2 = new Student("B19DCCN223", 4, 22 | // 3.51f, "Le Van Luong"); 23 | // Student student3 = new Student("B19DCCN523", 4, 24 | // 3.31f, "Ngo Van Tai"); 25 | // Student student4 = new Student("B19DCCN188", 4, 26 | // 3.02f, "Truong Trong Nghia"); 27 | 28 | // ghi file: 29 | try { 30 | FileOutputStream fos = new FileOutputStream("STUDENT.DAT"); 31 | ObjectOutputStream oos = new ObjectOutputStream(fos); 32 | 33 | oos.writeObject(student); 34 | // oos.writeObject(student1); 35 | // oos.writeObject(student2); 36 | // oos.writeObject(student3); 37 | // oos.writeObject(student4); 38 | 39 | oos.close(); 40 | fos.close(); 41 | } catch (FileNotFoundException e) { 42 | e.printStackTrace(); 43 | } catch (IOException e) { 44 | e.printStackTrace(); 45 | } 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Exercises2/Bai8/Bai8.java: -------------------------------------------------------------------------------- 1 | //package exercise2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Bai8 { 7 | public static void main(String[] args) { 8 | int m, n; 9 | int[][] arr, brr; 10 | Scanner scanner = new Scanner(System.in); 11 | System.out.println("Nhap m, n: "); 12 | m = scanner.nextInt(); 13 | n = scanner.nextInt(); 14 | System.out.println("Nhap ma tran a[][]: "); 15 | arr = nhap(m, n, scanner);// goi phuong thuc nhap 16 | brr = chuyenVi(arr); 17 | 18 | System.out.println("Ma tran goc: "); 19 | show(arr); 20 | 21 | System.out.println("Ma tran chuyen vi: "); 22 | show(brr); 23 | 24 | } 25 | 26 | private static int[][] chuyenVi(int[][] arr) { 27 | int m, n; 28 | m = arr.length; 29 | n = arr[0].length; 30 | 31 | int[][] brr = new int[n][m]; 32 | for (int i = 0; i < n; i++) { 33 | for (int j = 0; j < m; j++) { 34 | brr[i][j] = arr[j][i]; 35 | } 36 | } 37 | return brr; 38 | } 39 | 40 | private static int[][] nhap(int m, int n, Scanner scanner) { 41 | int[][] x = new int[m][n]; 42 | for (int i = 0; i < m; i++) { 43 | for (int j = 0; j < n; j++) { 44 | x[i][j] = scanner.nextInt(); 45 | } 46 | } 47 | return x; 48 | } 49 | 50 | private static void show(int[][] arr) { 51 | int m = arr.length; 52 | int n = arr[0].length; 53 | for (int i = 0; i < m; i++) { 54 | for (int j = 0; j < n; j++) { 55 | System.out.print(arr[i][j] + " "); 56 | } 57 | System.out.println(); 58 | } 59 | } 60 | 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Exercises2/Bai5/Bai5.java: -------------------------------------------------------------------------------- 1 | //package exercise2; 2 | 3 | import java.util.Arrays; 4 | import java.util.Scanner; 5 | 6 | public class Bai5 { 7 | public static Scanner scanner = new Scanner(System.in); 8 | 9 | public static void main(String[] args) { 10 | int[] a, b, c;// 3 mang dau vao 11 | int m, n, k, p;// m, n, k: so phan tu mang a, b, c tuong ung. p la vi tri chen 12 | Scanner scanner = new Scanner(System.in); 13 | System.out.println("Nhap m, n: "); 14 | m = scanner.nextInt(); 15 | n = scanner.nextInt(); 16 | k = m + n; 17 | 18 | // nhap du lieu 19 | System.out.println("Nhap gia tri mang a[] gom " + m + " phan tu"); 20 | a = nhap(m); 21 | System.out.println("Nhap gia tri b[] gom " + n + " phan tu"); 22 | b = nhap(n); 23 | c = new int[k]; 24 | System.out.println("Nhap gia tri p: "); 25 | 26 | do { 27 | p = scanner.nextInt(); 28 | } while (p >= m || p < 0); 29 | 30 | // copy du lieu tu hai mang vao mang dich 31 | for (int i = 0; i < p; i++) { 32 | c[i] = a[i]; 33 | } 34 | // copy ca mang b vao mang c 35 | for (int i = 0; i < n; i++) { 36 | c[i+p] = b[i]; 37 | } 38 | // copy phan con lai cua mang a vao mang c 39 | for (int i = p; i < m; i++) { 40 | c[i+n] = a[i]; 41 | } 42 | 43 | System.out.println("Mang a[]: " + Arrays.toString(a)); 44 | System.out.println("Mang b[]: " + Arrays.toString(b)); 45 | System.out.println("Mang c[]: " + Arrays.toString(c)); 46 | } 47 | 48 | public static int[] nhap(int n) { 49 | int[] x = new int[n]; 50 | for (int i = 0; i < n; i++) { 51 | x[i] = scanner.nextInt(); 52 | } 53 | 54 | return x; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /ObjectInOutputStream/V1/Student.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Student implements Serializable { 6 | private String studentID; 7 | private int studentYears; 8 | private float studentAVGMark; 9 | private String studentName; 10 | 11 | public Student(){ 12 | 13 | } 14 | 15 | public Student(String studentID){ 16 | 17 | } 18 | 19 | public Student(String studentID, int studentYears, float studentAVGMark, 20 | String studentName) { 21 | this.studentID = studentID; 22 | this.studentYears = studentYears; 23 | this.studentAVGMark = studentAVGMark; 24 | this.studentName = studentName; 25 | } 26 | 27 | public String getStudentID() { 28 | return studentID; 29 | } 30 | 31 | public void setStudentID(String studentID) { 32 | this.studentID = studentID; 33 | } 34 | 35 | public int getStudentYears() { 36 | return studentYears; 37 | } 38 | 39 | public void setStudentYears(int studentYears) { 40 | this.studentYears = studentYears; 41 | } 42 | 43 | public float getStudentAVGMark() { 44 | return studentAVGMark; 45 | } 46 | 47 | public void setStudentAVGMark(float studentAVGMark) { 48 | this.studentAVGMark = studentAVGMark; 49 | } 50 | 51 | public String getStudentName() { 52 | return studentName; 53 | } 54 | 55 | public void setStudentName(String studentName) { 56 | this.studentName = studentName; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "Student{" + 62 | "studentID='" + studentID + '\'' + 63 | ", studentYears=" + studentYears + 64 | ", studentAVGMark=" + studentAVGMark + 65 | ", studentName='" + studentName + '\'' + 66 | '}'; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /ObjectInOutputStream/V2/Student.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Student implements Serializable { 6 | private String studentID; 7 | private int studentYears; 8 | private float studentAVGMark; 9 | private String studentName; 10 | 11 | public Student(){ 12 | 13 | } 14 | 15 | public Student(String studentID){ 16 | 17 | } 18 | 19 | public Student(String studentID, int studentYears, float studentAVGMark, 20 | String studentName) { 21 | this.studentID = studentID; 22 | this.studentYears = studentYears; 23 | this.studentAVGMark = studentAVGMark; 24 | this.studentName = studentName; 25 | } 26 | 27 | public String getStudentID() { 28 | return studentID; 29 | } 30 | 31 | public void setStudentID(String studentID) { 32 | this.studentID = studentID; 33 | } 34 | 35 | public int getStudentYears() { 36 | return studentYears; 37 | } 38 | 39 | public void setStudentYears(int studentYears) { 40 | this.studentYears = studentYears; 41 | } 42 | 43 | public float getStudentAVGMark() { 44 | return studentAVGMark; 45 | } 46 | 47 | public void setStudentAVGMark(float studentAVGMark) { 48 | this.studentAVGMark = studentAVGMark; 49 | } 50 | 51 | public String getStudentName() { 52 | return studentName; 53 | } 54 | 55 | public void setStudentName(String studentName) { 56 | this.studentName = studentName; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "Student{" + 62 | "studentID='" + studentID + '\'' + 63 | ", studentYears=" + studentYears + 64 | ", studentAVGMark=" + studentAVGMark + 65 | ", studentName='" + studentName + '\'' + 66 | '}'; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /ObjectInOutputStream/V3/Student.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Student implements Serializable { 6 | private String studentID; 7 | private int studentYears; 8 | private float studentAVGMark; 9 | private String studentName; 10 | 11 | public Student(){ 12 | 13 | } 14 | 15 | public Student(String studentID){ 16 | 17 | } 18 | 19 | public Student(String studentID, int studentYears, float studentAVGMark, 20 | String studentName) { 21 | this.studentID = studentID; 22 | this.studentYears = studentYears; 23 | this.studentAVGMark = studentAVGMark; 24 | this.studentName = studentName; 25 | } 26 | 27 | public String getStudentID() { 28 | return studentID; 29 | } 30 | 31 | public void setStudentID(String studentID) { 32 | this.studentID = studentID; 33 | } 34 | 35 | public int getStudentYears() { 36 | return studentYears; 37 | } 38 | 39 | public void setStudentYears(int studentYears) { 40 | this.studentYears = studentYears; 41 | } 42 | 43 | public float getStudentAVGMark() { 44 | return studentAVGMark; 45 | } 46 | 47 | public void setStudentAVGMark(float studentAVGMark) { 48 | this.studentAVGMark = studentAVGMark; 49 | } 50 | 51 | public String getStudentName() { 52 | return studentName; 53 | } 54 | 55 | public void setStudentName(String studentName) { 56 | this.studentName = studentName; 57 | } 58 | 59 | @Override 60 | public String toString() { 61 | return "Student{" + 62 | "studentID='" + studentID + '\'' + 63 | ", studentYears=" + studentYears + 64 | ", studentAVGMark=" + studentAVGMark + 65 | ", studentName='" + studentName + '\'' + 66 | '}'; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /ObjectInOutputStream/V3/SingleObjectInputOutputStreamExample.java: -------------------------------------------------------------------------------- 1 | package object_input_output_stream; 2 | 3 | import model.Student; 4 | 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.ObjectOutputStream; 9 | import java.util.ArrayList; 10 | 11 | /* 12 | Đọc ghi một đối tượng đơn vào/từ file sử dụng ObjectInput/OutputStream 13 | */ 14 | public class SingleObjectInputOutputStreamExample { 15 | public static void main(String[] args) { 16 | 17 | Student student = new Student("B19DCCN123", 4, 18 | 3.21f, "Trieu Van Than"); 19 | Student student1 = new Student("B19DCCN155", 4, 20 | 3.29f, "Le Van Hung"); 21 | Student student2 = new Student("B19DCCN223", 4, 22 | 3.51f, "Le Van Luong"); 23 | Student student3 = new Student("B19DCCN523", 4, 24 | 3.31f, "Ngo Van Tai"); 25 | Student student4 = new Student("B19DCCN188", 4, 26 | 3.02f, "Truong Trong Nghia"); 27 | 28 | var list = new ArrayList(); 29 | list.add(student); 30 | list.add(student4); 31 | list.add(student2); 32 | list.add(student1); 33 | list.add(student3); 34 | // ghi file: 35 | try { 36 | FileOutputStream fos = new FileOutputStream("STUDENT.DAT"); 37 | ObjectOutputStream oos = new ObjectOutputStream(fos); 38 | 39 | oos.writeObject(list); 40 | 41 | // oos.writeObject(student); 42 | // oos.writeObject(student1); 43 | // oos.writeObject(student2); 44 | // oos.writeObject(student3); 45 | // oos.writeObject(student4); 46 | 47 | oos.close(); 48 | fos.close(); 49 | } catch (FileNotFoundException e) { 50 | e.printStackTrace(); 51 | } catch (IOException e) { 52 | e.printStackTrace(); 53 | } 54 | 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /connect_db/example.sql: -------------------------------------------------------------------------------- 1 | -- phpMyAdmin SQL Dump 2 | -- version 4.8.0.1 3 | -- https://www.phpmyadmin.net/ 4 | -- 5 | -- Máy chủ: 127.0.0.1 6 | -- Thời gian đã tạo: Th10 19, 2018 lúc 08:20 AM 7 | -- Phiên bản máy phục vụ: 10.1.32-MariaDB 8 | -- Phiên bản PHP: 7.2.5 9 | 10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 11 | SET AUTOCOMMIT = 0; 12 | START TRANSACTION; 13 | SET time_zone = "+00:00"; 14 | 15 | 16 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 17 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 18 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 19 | /*!40101 SET NAMES utf8mb4 */; 20 | 21 | -- 22 | -- Cơ sở dữ liệu: `example` 23 | -- 24 | 25 | -- -------------------------------------------------------- 26 | 27 | -- 28 | -- Cấu trúc bảng cho bảng `user` 29 | -- 30 | 31 | CREATE TABLE `user` ( 32 | `id` int(11) NOT NULL, 33 | `username` varchar(255) DEFAULT NULL, 34 | `password` varchar(25) DEFAULT NULL, 35 | `email` varchar(255) DEFAULT NULL 36 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 37 | 38 | -- 39 | -- Đang đổ dữ liệu cho bảng `user` 40 | -- 41 | 42 | INSERT INTO `user` (`id`, `username`, `password`, `email`) VALUES 43 | (1, 'thantrieu', '123', 'trieuthan92pro@xmail.com'), 44 | (2, 'my_love', '********', 'mylove@xmail.com'), 45 | (3, 'kitty', '********', 'kitty@xmail.com'), 46 | (4, 'puppy', '********', 'puppy@xmail.com'); 47 | 48 | -- 49 | -- Chỉ mục cho các bảng đã đổ 50 | -- 51 | 52 | -- 53 | -- Chỉ mục cho bảng `user` 54 | -- 55 | ALTER TABLE `user` 56 | ADD PRIMARY KEY (`id`); 57 | 58 | -- 59 | -- AUTO_INCREMENT cho các bảng đã đổ 60 | -- 61 | 62 | -- 63 | -- AUTO_INCREMENT cho bảng `user` 64 | -- 65 | ALTER TABLE `user` 66 | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5; 67 | COMMIT; 68 | 69 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 70 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 71 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 72 | -------------------------------------------------------------------------------- /comparator_interface/ComparatorExample.java: -------------------------------------------------------------------------------- 1 | package java_interface.comparator_interface; 2 | 3 | import model.Student; 4 | 5 | import java.util.ArrayList; 6 | import java.util.Collections; 7 | 8 | /* 9 | Sử dụng Comparator interface 10 | */ 11 | 12 | public class ComparatorExample { 13 | public static void main(String[] args) { 14 | ArrayList list = new ArrayList<>(); 15 | //String studentID, int studentYears, float studentAVGMark, 16 | // String studentName 17 | list.add(new Student("B19DCCN123", 4, 3.25f, 18 | "Tran Van Hung")); 19 | list.add(new Student("B19DCCN789", 4, 3.55f, 20 | "Nguyen Van Nam")); 21 | list.add(new Student("B19DCCN012", 4, 3.20f, 22 | "Le Van An")); 23 | list.add(new Student("B19DCCN339", 4, 3.19f, 24 | "Hoang Van Ngo")); 25 | list.add(new Student("B19DCCN412", 4, 3.89f, 26 | "Ngo Van Tung")); 27 | 28 | System.out.println("Truoc sap xep: "); 29 | for(var x: list){ 30 | System.out.println(x); 31 | } 32 | 33 | System.out.println(); 34 | System.out.println("Sau khi sap xep theo ten tang dan: "); 35 | Collections.sort(list, new SortByName());// sap xep theo tieu chi duoc dua ra o pt 36 | for(var x: list){ 37 | System.out.println(x); 38 | } 39 | 40 | System.out.println(); 41 | System.out.println("Sau khi sap xep theo diem giam dan: "); 42 | Collections.sort(list, new SortByMark());// sap xep theo tieu chi duoc dua ra o pt 43 | for(var x: list){ 44 | System.out.println(x); 45 | } 46 | 47 | System.out.println(); 48 | System.out.println("Sau khi sap xep theo ma sv tang dan: "); 49 | Collections.sort(list, new SortByStudentID());// sap xep theo tieu chi duoc dua ra o pt 50 | for(var x: list){ 51 | System.out.println(x); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Student.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | public class Student implements Comparable { 4 | private String studentID; 5 | private int studentYears; 6 | private float studentAVGMark; 7 | private String studentName; 8 | 9 | public Student(){ 10 | 11 | } 12 | 13 | public Student(String studentID){ 14 | 15 | } 16 | 17 | public Student(String studentID, int studentYears, float studentAVGMark, 18 | String studentName) { 19 | this.studentID = studentID; 20 | this.studentYears = studentYears; 21 | this.studentAVGMark = studentAVGMark; 22 | this.studentName = studentName; 23 | } 24 | 25 | public String getStudentID() { 26 | return studentID; 27 | } 28 | 29 | public void setStudentID(String studentID) { 30 | this.studentID = studentID; 31 | } 32 | 33 | public int getStudentYears() { 34 | return studentYears; 35 | } 36 | 37 | public void setStudentYears(int studentYears) { 38 | this.studentYears = studentYears; 39 | } 40 | 41 | public float getStudentAVGMark() { 42 | return studentAVGMark; 43 | } 44 | 45 | public void setStudentAVGMark(float studentAVGMark) { 46 | this.studentAVGMark = studentAVGMark; 47 | } 48 | 49 | public String getStudentName() { 50 | return studentName; 51 | } 52 | 53 | public void setStudentName(String studentName) { 54 | this.studentName = studentName; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "Student{" + 60 | "studentID='" + studentID + '\'' + 61 | ", studentYears=" + studentYears + 62 | ", studentAVGMark=" + studentAVGMark + 63 | ", studentName='" + studentName + '\'' + 64 | '}'; 65 | } 66 | 67 | @Override 68 | public int compareTo(Student o) { 69 | String name1 = studentName.substring(studentName.lastIndexOf(" ")); 70 | String name2 = o.studentName.substring(o.studentName.lastIndexOf(" ")); 71 | return name1.compareTo(name2); 72 | // if return value greater than 0 then swap 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /model/Student.java: -------------------------------------------------------------------------------- 1 | package model; 2 | 3 | public class Student implements Comparable { 4 | private String studentID; 5 | private int studentYears; 6 | private float studentAVGMark; 7 | private String studentName; 8 | 9 | public Student(){ 10 | 11 | } 12 | 13 | public Student(String studentID){ 14 | 15 | } 16 | 17 | public Student(String studentID, int studentYears, float studentAVGMark, 18 | String studentName) { 19 | this.studentID = studentID; 20 | this.studentYears = studentYears; 21 | this.studentAVGMark = studentAVGMark; 22 | this.studentName = studentName; 23 | } 24 | 25 | public String getStudentID() { 26 | return studentID; 27 | } 28 | 29 | public void setStudentID(String studentID) { 30 | this.studentID = studentID; 31 | } 32 | 33 | public int getStudentYears() { 34 | return studentYears; 35 | } 36 | 37 | public void setStudentYears(int studentYears) { 38 | this.studentYears = studentYears; 39 | } 40 | 41 | public float getStudentAVGMark() { 42 | return studentAVGMark; 43 | } 44 | 45 | public void setStudentAVGMark(float studentAVGMark) { 46 | this.studentAVGMark = studentAVGMark; 47 | } 48 | 49 | public String getStudentName() { 50 | return studentName; 51 | } 52 | 53 | public void setStudentName(String studentName) { 54 | this.studentName = studentName; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | return "Student{" + 60 | "studentID='" + studentID + '\'' + 61 | ", studentYears=" + studentYears + 62 | ", studentAVGMark=" + studentAVGMark + 63 | ", studentName='" + studentName + '\'' + 64 | '}'; 65 | } 66 | 67 | @Override 68 | public int compareTo(Student o) { 69 | String name1 = studentName.substring(studentName.lastIndexOf(" ")); 70 | String name2 = o.studentName.substring(o.studentName.lastIndexOf(" ")); 71 | return name1.compareTo(name2); 72 | // if return value greater than 0 then swap 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Connect MSSQL Server/TestConnectDB.java: -------------------------------------------------------------------------------- 1 | // package connect_db; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.ResultSet; 6 | import java.sql.SQLException; 7 | 8 | public class TestConnectDB { 9 | public static void main(String[] args) { 10 | try { 11 | Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 12 | String url = "jdbc:sqlserver://localhost:1433;databaseName=Tutorial";// your db name 13 | String user="sa"; // your db username 14 | String password="123"; // your db password 15 | // String connectionUrl = "jdbc:sqlserver://localhost:1433;" + 16 | // "databaseName=Tutorial;username=sa;password=123;"; 17 | Connection conn = DriverManager.getConnection(url, user, password); 18 | if(conn != null){ 19 | System.out.println("Connect success!"); 20 | } 21 | 22 | var sql = "select * from dbo.Student"; 23 | var statement = conn.prepareStatement(sql); 24 | var resultSet = statement.executeQuery(); 25 | showResutl(resultSet); 26 | } catch (ClassNotFoundException e) { 27 | e.printStackTrace(); 28 | } catch (SQLException e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | 33 | private static void showResutl(ResultSet resultSet) throws SQLException { 34 | System.out.printf("%-6s%-20s%-15s%-30s%-20s%-10s%-20s\n", "ID", 35 | "Full Name", "Address", "Email", 36 | "Date of birth", "Gender", "Phone"); 37 | while (resultSet.next()){ 38 | var id = resultSet.getInt("ID"); 39 | var fullName = resultSet.getString("FullName"); 40 | var address = resultSet.getString("Adress"); 41 | var email = resultSet.getString("Email"); 42 | var dob = resultSet.getDate("DOB"); 43 | var gender = resultSet.getString("Gender"); 44 | var phoneNumnber = resultSet.getString("Phone"); 45 | 46 | System.out.printf("%-6d%-20s%-15s%-30s%-20s%-10s%-20s\n", id, fullName, 47 | address, email, dob, gender, phoneNumnber); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Exercises2/Bai9/Bai9.java: -------------------------------------------------------------------------------- 1 | package exercise2; 2 | 3 | import java.util.Scanner; 4 | 5 | public class Bai9 { 6 | /* A[3][4] B[4][3] C[3][3] 7 | i..j_0_1_2_3___ _0_1_2__ 8 | 0.. 1 2 3 4 0.. 1 3 3 c[i][j] 9 | 1.. 2 3 5 0 1.. 2 8 5 10 | 2.. 7 5 2 3 2.. 6 4 1 11 | 3.. 2 1 1 12 | */ 13 | public static void main(String[] args) { 14 | Scanner scanner = new Scanner(System.in); 15 | int m, n, k; 16 | int[][] a, b, c; 17 | System.out.println("Nhap cap ma tran a: "); 18 | // nhap so tu nhien > 0 19 | m = scanner.nextInt(); 20 | n = scanner.nextInt(); 21 | System.out.println("Nhap so cot ma tran b: "); 22 | k = scanner.nextInt(); 23 | System.out.println("Nhap ma tran a: "); 24 | a = nhap(m, n, scanner); 25 | System.out.println("Nhap ma tran b: "); 26 | b = nhap(n, k, scanner); 27 | System.out.println("Ma tran a: "); 28 | xuat(a); 29 | 30 | System.out.println("Ma tran b: "); 31 | xuat(b); 32 | 33 | c = nhanMaTran(a, b); 34 | System.out.println("Ket qua: "); 35 | xuat(c); 36 | 37 | } 38 | 39 | 40 | 41 | public static int[][] nhap(int m, int n, Scanner scanner){ 42 | int[][] x = new int[m][n]; 43 | for (int i = 0; i < m; i++) { 44 | for (int j = 0; j < n; j++) { 45 | x[i][j] = scanner.nextInt(); 46 | } 47 | } 48 | return x; 49 | } 50 | 51 | public static void xuat(int[][] x){ 52 | for (int i = 0; i < x.length; i++) { 53 | for (int j = 0; j < x[0].length; j++) { 54 | System.out.printf("%5d", x[i][j]); 55 | } 56 | System.out.println(); 57 | } 58 | } 59 | 60 | public static int[][] nhanMaTran(int[][] a, int[][] b){ 61 | int[][] c = new int[a.length][b[0].length]; 62 | int m = a.length; 63 | int n = b[0].length; 64 | int k = a[0].length; 65 | for (int i = 0; i < m; i++) { 66 | for (int j = 0; j < n; j++) { 67 | for (int l = 0; l < k; l++) { 68 | c[i][j] += a[i][l] * b[l][j]; 69 | } 70 | } 71 | } 72 | return c; 73 | } 74 | } 75 | --------------------------------------------------------------------------------