//cau1: import java.util.Scanner; // Interface IMathOperation interface IMathOperation { float PI = 3.1416f; float caculate(); void showInfo(); } // Addition class class Addition implements IMathOperation { private float operand1, operand2; @Override public float caculate(){ return(float) (operand1+operand2); } @Override public void showInfo(){ System.out.printf("Lớp: Addition\n"); System.out.printf("%.2f + %.2f = %.2f\n",operand1,operand2,caculate()); } public Addition(float operand1, float operand2){ this.operand1= operand1; this.operand2 = operand2; } } // Subtraction class class Subtraction implements IMathOperation { private float operand1, operand2; @Override public float caculate(){ return(float) (operand1-operand2); } @Override public void showInfo(){ System.out.printf("Lớp: Subtraction\n"); System.out.printf("%.2f - %.2f = %.2f\n",operand1,operand2,caculate()); } public Subtraction(float operand1, float operand2){ this.operand1 = operand1; this.operand2 = operand2; } } // Multiplication class class Multiplication implements IMathOperation { private float operand1, operand2; @Override public float caculate(){ return(float) (operand1*operand2); } @Override public void showInfo(){ System.out.printf("Lớp: Multiplication\n"); System.out.printf("%.2f * %.2f = %.2f\n",operand1,operand2,caculate()); } public Multiplication(float operand1, float operand2){ this.operand1=operand1; this.operand2=operand2; } } // Main class public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // Addition input float a1 = sc.nextFloat(); float a2 = sc.nextFloat(); // Subtraction input float s1 = sc.nextFloat(); float s2 = sc.nextFloat(); // Multiplication input float m1 = sc.nextFloat(); float m2 = sc.nextFloat(); IMathOperation A = new Addition(a1,a2); A.showInfo(); IMathOperation B = new Subtraction(s1,s2); B.showInfo(); IMathOperation C = new Multiplication(m1,m2); C.showInfo(); } } // cau 2:Ghi và đọc đối tượng bằng ObjectOutputStream và ObjectInputStream import java.io.*; import java.util.*; class MyObject implements Serializable { private String message; private int value; public MyObject(String message, int value) { this.message = message; this.value = value; } @Override public String toString() { return "MyObject{message='" + message + "', value=" + value + "}"; } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String message = sc.nextLine(); int value = sc.nextInt(); MyObject obj = new MyObject(message, value); String filePath = "object.txt"; // Ghi đối tượng vào file try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) { oos.writeObject(obj); } catch (IOException e) { e.printStackTrace(); } // Đọc đối tượng từ file try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) { MyObject readObj = (MyObject) ois.readObject(); System.out.println(readObj); // tự động gọi toString() } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } sc.close(); } } //cau 3:Đếm số lần xuất hiện của X trong mảng lớn bằng Thread //Cho mảng N số nguyên và một giá trị X. Hãy dùng đa luồng(k là số lượng luồng) để đếm xem X xuất hiện bao nhiêu lần trong mảng. import java.util.*; class CountThread extends Thread { private int[] arr; private int start; private int end; private int X; private int count = 0; public CountThread(int[] arr, int start, int end, int X) { this.arr = arr; this.start = start; this.end = end; this.X = X; } @Override public void run() { for (int i = start; i < end; i++) { if (arr[i] == X) count++; } } public int getCount() { return count; } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int[] arr = new int[N]; for (int i = 0; i < N; i++) arr[i] = sc.nextInt(); int X = sc.nextInt(); int K = sc.nextInt(); // Tạo mảng luồng CountThread[] threads = new CountThread[K]; int chunk = N / K; int extra = N % K; int start = 0; // Chia công việc cho K luồng for (int i = 0; i < K; i++) { int end = start + chunk; if (i < extra) end++; // phân bổ thêm phần dư threads[i] = new CountThread(arr, start, end, X); threads[i].start(); start = end; } // Chờ tất cả luồng xong int totalCount = 0; for (int i = 0; i < K; i++) { try { threads[i].join(); totalCount += threads[i].getCount(); } catch (InterruptedException e) { e.printStackTrace(); } } // In kết quả System.out.println(totalCount); } } //cau4 :Trích lọc phần tử duy nhất và sắp xếp tăng dần //Cho danh sách N số nguyên lưu trong LinkedList. Hãy dùng TreeSet để trích lọc các phần tử không trùng lặp, được sắp xếp tăng dần và in ra. import java.util.*; public class Main { public static void main(String[] args) { // Viết code Java của bạn ở đây Scanner sc = new Scanner(System.in); int N = sc.nextInt(); LinkedList list = new LinkedList<>(); for (int i = 0; i < N; i++) { list.add(sc.nextInt()); } // TreeSet tự động loại bỏ trùng lặp và sắp xếp tăng dần TreeSet set = new TreeSet<>(list); // In ra danh sách tăng dần không trùng lặp for (Integer x : set) { System.out.print(x + " "); } // //Trường hợp: TreeSet giảm dần (loại trùng + sắp xếp giảm dần) // TreeSet set = new TreeSet<>(Collections.reverseOrder()); // set.addAll(list); // // In ra danh sách giảm dần không trùng lặp // for (Integer x : set) { // System.out.print(x + " "); // } } } //cau 5:Phân loại nhân viên theo nhóm tuổi // Cho danh sách nhân viên gồm tên và tuổi. Hãy thực hiện các thao tác sau bằng Java Streams, trong đó sử dụng Function, Predicate, Operator, Comparator: // Dùng Predicate để lọc những nhân viên có tuổi ≥ 25. // Dùng Function để ánh xạ mỗi nhân viên sang chuỗi dạng "Tên - Tuổi". // Dùng BinaryOperator để chọn chuỗi có độ dài lớn hơn khi xảy ra xung đột khi gom nhóm. // Sắp xếp danh sách kết quả theo Comparator giảm dần theo tuổi. import java.util.*; import java.util.function.*; import java.util.stream.*; class Employee { String name; int age; Employee(String name, int age) { this.name = name; this.age = age; } } public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = Integer.parseInt(sc.nextLine()); List list = new ArrayList<>(); for (int i = 0; i < N; i++) { String[] parts = sc.nextLine().split(" "); list.add(new Employee(parts[0], Integer.parseInt(parts[1]))); } // 1. Predicate lọc nhân viên có tuổi >= 25 Predicate ageFilter = e -> e.age >= 25; // 2. Function chuyển Employee -> "Tên - Tuổi" Function toStringFunc = e -> e.name + " - " + e.age; // 3. BinaryOperator để chọn chuỗi dài hơn khi merge BinaryOperator chooseLonger = (a, b) -> { if (a.length() >= b.length()) return a; return b; }; // 4. Comparator giảm dần theo tuổi Comparator sortDescByAge = (a, b) -> b.age - a.age; // Áp dụng các bước List result = list.stream() .filter(ageFilter) // lọc tuổi >= 25 .sorted(sortDescByAge) // sắp xếp giảm dần tuổi .map(toStringFunc) // chuyển sang chuỗi .collect(Collectors.toList()); // thu kết quả // In ra kết quả result.forEach(System.out::println); } } //----------------------------K64-------------------- public abstract class Shape { private String color; public Shape(String color) { this.color = color; } public String getColor() { return color; } public abstract double getArea(); public abstract double getPerimeter(); } public class Circle extends Shape { private double radius; public Circle(String color, double radius) { super(color); this.radius = radius; } @Override public double getArea() { return Math.PI * radius * radius; } @Override public double getPerimeter() { return 2 * Math.PI * radius; } public void showInfo() { System.out.println("Circle Color: " + getColor()); System.out.println("Radius: " + radius); System.out.println("Area: " + getArea()); System.out.println("Perimeter: " + getPerimeter()); } } public class Rectangle extends Shape { private double width; private double length; public Rectangle(String color, double width, double length) { super(color); this.width = width; this.length = length; } @Override public double getArea() { return width * length; } @Override public double getPerimeter() { return 2 * (width + length); } public void showInfo() { System.out.println("Rectangle Color: " + getColor()); System.out.println("Width: " + width + ", Length: " + length); System.out.println("Area: " + getArea()); System.out.println("Perimeter: " + getPerimeter()); } } public static void main(String[] args) { Rectangle rectangle = new Rectangle("Red", 4.5, 3.5); Circle circle = new Circle("Blue", 5); rectangle.showInfo(); circle.showInfo(); } //cau 2 Tạo hàm writeToFile() để tạo và ghi 2 số nguyên vào file integers.txt public static String FILE_PATH = "integers.txt"; public static void writeToFile() { try { DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(FILE_PATH)); dataOutputStream.writeInt(3); dataOutputStream.writeInt(5); dataOutputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } } //Tạo hàm readFromFile() để đọc 2 số nguyên từ file intergers.txt và in ra kết quả đề bài yêu cầu public static void readFromFile() { try { DataInputStream dataInputStream = new DataInputStream(new FileInputStream(FILE_PATH)); int firstNumber = dataInputStream.readInt(); int secondNumber = dataInputStream.readInt(); System.out.println("Tổng của hai số nguyên là: " + (firstNumber + secondNumber)); System.out.println("Tích của hai số nguyên là: " + (firstNumber * secondNumber)); dataInputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } } public static void main(String[] args ){ writeToFile(); readFromFile(); } //cau 3:Khởi tạo 2 threads. Thread 1 in ra các số chia hết cho 2 trong đoạn từ 1 đến 50; // Thread 2 in ra các số nguyên tố trong khoảng 50 đến 100 public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread(() -> { for (int i = 1; i <= 50; i++) { if (i % 2 == 0) System.out.println("Thread 1 - Số chia hết cho 2: " + i); } }); Thread thread2 = new Thread(() -> { for (int i = 50; i <= 100; i++) { if (isPrimeNumber(i)) System.out.println("Thread 2 - Số nguyên tố: " + i); } }); thread1.start(); thread2.start(); thread1.join(); thread2.join(); } public static boolean isPrimeNumber(int i) { if (i == 2) return true; if (i == 1 || i % 2 == 0) return false; for (int j = 2; j <= Math.sqrt(i); j++) { if (i % j == 0) return false; } return true; } //4.Khởi tạo đối tượng Scanner.Thực hiện một vòng lặp while:Đọc tên sinh viên sử dụng scanner.nextLine() //+ Kiểm tra nếu tên vừa nhập vào là ‘kết thúc’ thì kết thúc vòng lặp để in danh sách sinh viên kèm điểm số //+ Nếu không phải là ‘kết thúc’ thì đọc điểm của sinh viên và cập nhật vào bảng public static void main(String[] args) { HashMap studentScores = new HashMap<>(); Scanner scanner = new Scanner(System.in); do { System.out.print("Nhập tên sinh viên (nhập 'kết thúc' để kết thúc): "); String studentName = scanner.nextLine(); if (studentName.equalsIgnoreCase("kết thúc")) break; System.out.print("Nhập điểm số của sinh viên: "); int score = scanner.nextInt(); studentScores.put(studentName, score); scanner.nextLine(); } while (true); System.out.println("\nDanh sách sinh viên và điểm số:"); studentScores.forEach((name, score) -> { System.out.println(name + ": " + score); }); } //5.Dùng stream() của danh sách people. Thực hiện lọc ra những người có tuổi trên 18 bằng hàm filter, // điều kiện là person.getAge() > 18. Sau đó gọi hàm forEach để in ra tên của những người vừa lọc được: public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } public static void main(String[] args) { List people = Arrays.asList( new Person("Alice", 20), new Person("Bob", 25), new Person("Charlie", 18), new Person("David", 22) ); System.out.print("Những người lớn hơn 18 tuổi: "); people.stream() .filter(person -> person.getAge() > 18) .forEach(person -> System.out.print(person.getName() + ", ")); } //—-------------------K60 —---------------------------- import java.io.*; import java.util.*; import static java.lang.Thread.sleep; public class STT6_HoangLong_1 { public static void main(String[] args) throws IOException { System.out.println("==========Câu 1==========================================="); // cau1(); System.out.println("==========Câu 2==========================================="); //cau2(); System.out.println("==========Câu 3==========================================="); //cau3(); System.out.println("==========Câu 4==========================================="); cau4(); System.out.println("==========Câu 5==========================================="); // cau5(); } //---------------------------cau 1----------------------------- interface IShape{ public void showInfo();//Khai báo hàm showInfo để hiển thị thông tin ra màn hình float getArea();//Khai báo hàm tính diện tích float getPerimeter();//khai báo hàm tính chu vi final double PI = 3.1416;//khai báo hằng số Pi } public static class Circle implements IShape{ private float radius;//Khai báo biến radius để nhận giá trị từ bàn phím public float getRadius() {//hàm trả về giá trị tính bán kính của hình tròn return radius;//giá trị trả về là thuộc tính radius } public void setRadius(float radius) {//hàm dán giá trị cho bán kính this.radius = radius;//dán giá trị } @Override public void showInfo() { System.out.println("Hinh tron co dien tich: "+ getArea());//hiển thị giá trị diện tích ra màn hình System.out.println("Hinh tron co chu vi: "+ getPerimeter());//hiển thị giá trị chu vi ra màn hình } @Override public float getArea() { return (float) (radius*radius*PI);//trả về giá trị của diện tích hình tròn } @Override public float getPerimeter() { return (float) (2*radius*PI);//trả về giá trị của chu vi hình tròn } public Circle(float radius) {//hàm tạo this.radius = radius;//dán giá trị vào biến radius } } public static class Rectangle implements IShape{ public double witdh;//khai báo thuộc tính chiều rộng public double length;//khai báo thuocj tính chiều dài @Override public void showInfo() { System.out.println("Hinh chu nhat co dien tich: "+ getArea());//hiển thị giá trị diện tích ra màn hình System.out.println("Hinh chu nhat co chu vi: "+ getPerimeter());//hiển thị giá trị chu vi ra màn hình } @Override public float getArea() { return (float) (witdh*length);//hàm trả về giá trị tính diện tích } @Override public float getPerimeter() { return (float) (witdh+witdh+length+length);//hàm trả về giá trị tính chu vi } public Rectangle(double witdh, double length) {//hàm tạo this.witdh = witdh;//dán giá trị vào biến chiều dài this.length = length;//dán giá trị vào biến chiều rộng } } public static void cau1(){ IShape C= new Circle(25.5F);//khai báo đối tượng C và khỏi tạo giá trị dùng hàm tạo IShape R= new Rectangle(5,12);//khai báo đối tượng R và khỏi tạo giá trị dùng hàm tạo C.showInfo();//gọi hàm showInfo để hiển thị thông tin ra màn hình R.showInfo();//gọi hàm showInfo để hiển thị thông tin ra màn hình } //----------------------------------cau2------------------------------- //Câu 2 (2 điểm): Viết hàm cau2() thực hiện:a. Đọc vào số phần tử của mảng từ bàn phím, khởi tạo một mảng các số thực (double), nhập các phần tử cho mảng, chạy và tính ra tổng các phần tử của mảng, ném ra (throw) một đối tượng Exception nếu tổng này > 20 //b. Lập trình đảm bảo bắt toàn bộ các ngoại lệ (exception) có thể xảy ra. public static void cau2() throws IOException { try{ Scanner s= new Scanner(System.in);//Sử dụng đối tuong scanner để nhập dữliệu từ bàn phím int n;//khai báo biến n là số lượng của phần tử trong mảng System.out.println("Nhap số phàn tử trong mang: ");//hiển thị thông báo nhập só lượng phần tử n = s.nextInt();//Nhập só nguyên vào cho biến n bằng hàm nextInt() double A[] = new double[n];//Khai báo mảng n só nguyên for (int i=0; i 20) throw new Exception("Tong lon hon 20");//tạo lỗi tong lớn hơn 20 System.out.println("tong: "+ tong);//hiển thị ra màn hình gia trị của tổng }catch (NegativeArraySizeException EX1){//bắt lỗi số âm System.out.println("Số lượng phần tử không duoc là so âm");//hiển thị thông báo lôi số âm } catch (InputMismatchException e){//bắt lôi sai kiểu dữ liệu System.out.println("Loi ki tu vua nhap khong thuoc kieu int");//hiển thị thông báo sai kiểu dữ liệu }catch (Exception e){//bắt các lỗi khác System.out.println("Loi: "+ e.getMessage());//hiển thị thông báo các lỗi khác } } //----------------------------------------cau3----------------------------------------- public static void cau3() throws IOException { FileOutputStream o= new FileOutputStream("./int.txt");//khai bao biến thuộc FileOutputStream FileInputStream i= new FileInputStream("./int.txt");// khai báo biến thuộc FileInputStream DataOutputStream o1= new DataOutputStream(o);//khai báo biến thuộc DataOutputStream DataInputStream i1= new DataInputStream(i);//khai báo biến thuộc DataInputStream int a= 2;// khai báo biến a và dán giá trị bằng 2 int b= 5;// khai báo biến b và dán giá trị bằng 5 o1.write(a);//ghi biến a vào file int.txt o1.write(b);//ghi biến b vào file int.txt o.close();// đóng file int int c,d; //khai báo 2 biến c, d c= i1.read();//dán giá trị cho biến c lấy từ file int d= i1.read();//dán giá trị cho biến d lấy từ file int System.out.println("Tong 2 so:" + (c+d));// hiển thị tổng 2 biến c, d ra màn hình i.close();// đóng file int } //------------------------------------------cau4------------------------------ public static void cau4(){ Thread t1 = new Thread(new Runnable() {//khai báo t1 là thread thứ nhất @Override public void run() { while (true){//vòng lặp vô hạn Random r = new Random(10);//khai báo biến r là biến ngau nhiên System.out.println(r.nextInt());//hiển thị ra màn hình số nguyên ngẫu nhiên try { sleep(15000);//thời gian chờ là 15giay } catch (InterruptedException e) {//xử lí ngoại lệ của hàm sleep() e.printStackTrace(); } } } }); Thread t2 = new Thread(new Runnable() {//khai báo t2 là thread thứ hai chạy song song với thread thứ nhất @Override public void run() { while (true){// vofg lặp vo hạn long tong = 0;//khai báo biến tính tổng for (int i= 1; i<=1000000000; i++){//vòng lặp số nguyên từ 1 đến 1 tỉ tong = (long) (tong+ i);//tính tổng, cổng lần lượt các giá trị } System.out.println("Tong: "+ tong);//hiển thị ra màn hình giá trị của biến tổng } } }); t1.start();//gọi hàm start để chạy t1 t2.start();//gọi hàm start để chạy t1 } //---------------------------------------------cau 5------------------------------- public static void cau5(){ List L= new LinkedList<>();//khai báo đối tượng L thuộc LinkedList IShape A = new Circle((float) 2.4);//khai báo lần lượt đối tượng thuộc lớp IShape và khỏi tạo giá trị dùng hàm tạo IShape B = new Circle(3); IShape C = new Rectangle(3,2); IShape D = new Rectangle(4,1); IShape E = new Rectangle(1,7); L.add(A);//thêm làn lượt các giá trị vào L L.add(B); L.add(C); L.add(D); L.add(E); Map M = new HashMap<>();//khai báo đối tượng M thuộc Map int i=1;//khai báo biến i để dùng làm key for(IShape r: L ) {// vòng lặp lần lượt truy cập vào các phần tử trong L M.put(i,r);//thêm lần lượt các đối tượng của L vào M i++;//tăng dàn key } System.out.println("So phan tu trong map: " + M.size());//thông báo số lượng phân tử trong M for(Map.Entry e : M.entrySet()) { //duyệt các phần tử trong M System.out.println("-------------------------------------"); e.getValue().showInfo();//dùng hàm showInfo để hiển thị thông tin từng phân tử trong M } } } //—-----------------------------------------k60 de 2—---------------------- import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.*; import static java.lang.Thread.sleep; import static javax.print.attribute.standard.MediaSizeName.*; public class STT6_HoangLong_2 { public static void main(String[] args) throws IOException { //cau1(); //cau2(); //cau3(); //cau4(); cau5(); } //--------------------------------------cau 1----------------------------- public static abstract class Animal{ public int age;//Khai báo biến age để nhận giá trị từ bàn phím public String gender;//Khai báo biến gender để nhận giá trị từ bàn phím public boolean isMammal() {//Hàm isMammal trả về giá trị false return false; } public abstract void showInfo(); //hàm trừu trựong showInfo public Animal(int age, String gender) {//Hàm tạo this.age = age;//dán giá trị vào thuộc tính age this.gender = gender;// dán giá trị vào thuộc tính gender } } public static class Duck extends Animal{ public String color;//Khai báo biến color nhập từ bàn phím void swim(){ //hàm swim } void quack(){ } public Duck(String color, int age, String gender) {//hàm tạo super(age, gender);//dán các biến vào các thuộc tính ở lớp cha this.color= color;// dán giá trị vào thuộc tính color } @Override public void showInfo() { System.out.println("Duck{" + //hiển thị các giá trị của thuộc tính ra màn hình "age=" + age + ", gender='" + gender + '\'' + ", color='" + color + '\'' + '}'); } } public static class Fish extends Animal{ int size;//Khai báo biến size nhập từ bàn phím boolean canEat;//Khai báo biến canEat nhập từ bàn phím public Fish(int size, int age, String gender) { //hàm tạo super(age, gender);//dán các biến vào các thuộc tính ở lớp cha this.size= size;// dán giá trị vào thuộc tính size } void swim(){ } @Override public void showInfo() { System.out.println("Fish{" + //hiển thị các giá trị của thuộc tính ra màn hình "age=" + age + ", gender='" + gender + '\'' + ", size=" + size + ", canEat=" + canEat + '}'); } } public static class Horse extends Animal{ boolean isWild;//Khai báo biến isWild nhập từ bàn phím public Horse(int age, String gender) { super(age, gender);//dán các biến vào các thuộc tính ở lớp cha } void run(){ //hàm run của lớp Horse } @Override public boolean isMammal() { // hàm isMammal trả về giá trị true return true; } @Override public void showInfo() { //Hàm hiển thị các giá trị của thuộc tính ra màn hình System.out.println("Horse{" + //hiển thị lần lượt các giá trị "age=" + age + ", gender='" + gender + '\'' + ", isWild=" + isWild + '}'); } } public static void cau1(){ Animal A = new Duck("Vang", 2, "male");//khai báo và dùng hàm tạo Animal B = new Fish(5, 3, "female");//khai báo và dùng hàm tạo Animal C = new Horse(5, "male");//khai báo và dùng hàm tạo A.showInfo();//hiển thị đối tượng ra màn hình B.showInfo();//hiển thị đối tượng ra màn hình C.showInfo();//hiển thị đối tượng ra màn hình } //-----------------------------------cau2--------------------------------- public static void cau2(){ try { System.out.println("Nhap vao hai so nguyen, mot so am, mot so duong ");//hiển thị thông báo nhập 2 số Scanner s = new Scanner(System.in); //Sử dụng đối tượng scanner để nhập dữ liệu từ bàn phím int so1, so2; //khai báo 2 biến số so1 = s.nextInt(); //Nhập số nguyên từ vào cho biến so1 bằng hàm nextInt() so2 = s.nextInt(); //Nhập số nguyên từ vào cho biến so2 bằng hàm nextInt() if (so1*so2>0) System.err.println("Phai nhap mot so am, mot so duong");//kiểm tra 2 số có thỏa mãn điều kiện âm, dương không else System.out.println("Tong: "+ (so1+so2)); //Nếu thỏa mãn thì tính tổng } catch (InputMismatchException e){ System.out.println("Loi ki tu vua nhap khong thuoc kieu int");//thông báo lỗi khi không thuộc kiểu int } catch (Exception e){ System.out.println("Loi: "+ e.getMessage());// thông báo các lỗi khác } } //-----------------------------------cau3------------------------------ public static void cau3() throws IOException { FileOutputStream o= new FileOutputStream("./raw.txt");//khai bao biến thuộc FileOutputStream FileInputStream i= new FileInputStream("./raw.txt");// khai báo biến thuộc FileInputStream int a= 2;// khai báo biến a và dán giá trị bằng 2 int b= 5;// khai báo biến b và dán giá trị bằng 5 o.write(a); //ghi a vào file raw.txt o.write(b);// ghi b vào file raw.txt o.close();// đóng file raw int c,d; //khai báo 2 biến c, d c= i.read();//dán giá trị cho c từ file raw d= i.read();//dán giá trị cho c từ file raw System.out.println(c+","+ d);// hiển thị lần lượt c, d ra màn hình i.close();// đóng file raw } //-------------------------------cau4------------------------ public static void cau4(){ Thread t1 = new Thread(new Runnable() { //khai báo luồng thứ nhất @Override public void run() { while (true){ System.out.println("Deo khau trang");// hiển thị thông báo đeo khẩu trang try { sleep(10000); //câu lệnh chờ 10 giây } catch (InterruptedException e) { //xử lí ngoại lệ e.printStackTrace(); } } } }); Thread t2 = new Thread(new Runnable() {//khai báo luồng thứ 2 @Override public void run() { Scanner s= new Scanner(System.in); while (true){ System.out.println("Nhap vao so nguyen: ");//hiển thị thông báo nhập một số nguyên để kiểm tra chẵn lẻ int x = s.nextInt();//Nhập số nguyên từ vào cho biến x bằng hàm nextInt() if (x%2==0) System.out.println("So vua nhap la so chan");//kiểm tra chẵn lẻ else System.out.println("So vua nhap la so le");// hiển thị nếu là số lẻ } } }); t1.start();//chạy luồng thứ nhất t2.start();// chạy luồng thứ 2 } //-------------------------cau5---------------------------- public static void cau5(){ Map M= new LinkedHashMap<>();//khai báo đối tượng M dùng lớp LinkedHashMap Animal A = new Duck("Vang", 2, "male");//khai báo lần lượt 3 đối tượng thuộc lớp Animal Animal B = new Fish(5, 3, "female"); Animal C = new Horse(5, "male"); M.put(1, A); //thêm lần lượt 3 đối tượng Animal vào M M.put(3, B); M.put(4, C); Set S = new HashSet();//khai báo đối tượng S dùng lớp HashSet for(Map.Entry entry : M.entrySet()) { //duyệt các phần tử trong M S.add(entry.getValue());//thêm lần lượt các giá trị từ M vào S } System.out.println("So phan tu trong HashSet = " + S.size());//Hiển thị ra màn hình số phàn tử } }