By GokiSoft.com| 14:16 30/06/2023|
Java Basic

[Share Code] [Examination] Kiêm tra 60 phút - Khoá java căn bản - C2209I

[Examination] Kiêm tra 60 phút - Khoá java căn bản

#AgeException.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2209i.bt3006;

/**
 *
 * @author teacher
 */
public class AgeException extends Exception {
    
}

#EmailException.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2209i.bt3006;

/**
 *
 * @author teacher
 */
public class EmailException extends Exception {
}

#Main.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2209i.bt3006;

/**
 *
 * @author teacher
 */
public class Main {
    public static void main(String[] args) {
        Student std = new Student();
        
        //TH1: Email & Age
        try {
            std.setEmail("tranvandiep.itgmail.com");
            System.out.println("Email > Success");
        } catch (EmailException ex) {
            System.out.println("Email > error");
        }
        try {
            std.setAge(-10);
            System.out.println("Age > Success");
        } catch (AgeException ex) {
            System.out.println("Age > error");
        }
        
        //TH2: Email > Error, Age > Success
        try {
            std.setEmail("tranvandiep.itgmail.com");
            System.out.println("Email > Success");
        } catch (EmailException ex) {
            System.out.println("Email > error");
        }
        try {
            std.setAge(10);
            System.out.println("Age > Success");
        } catch (AgeException ex) {
            System.out.println("Age > error");
        }
        
        //TH2: Email > Success, Age > Success
        try {
            std.setEmail("tranvandiep.it@gmail.com");
            System.out.println("Email > Success");
        } catch (EmailException ex) {
            System.out.println("Email > error");
        }
        try {
            std.setAge(10);
            System.out.println("Age > Success");
        } catch (AgeException ex) {
            System.out.println("Age > error");
        }
    }
}

#Student.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2209i.bt3006;

/**
 *
 * @author teacher
 */
public class Student {
    String fullname;
    String email;
    int age;

    public Student() {
    }

    public String getFullname() {
        return fullname;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) throws EmailException {
        if(!email.contains("@")) {
            throw new EmailException();
        }
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) throws AgeException {
        if(age < 0) {
            throw new AgeException();
        }
        
        this.age = age;
    }
}
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.gokisoft.c2209i.bt3006;

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author teacher
 */
public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        //Nhap 1 String tu ban phim > s
        System.out.println("Nhap chuoi s: ");
        String s = scan.nextLine();
        
        //Tim kiem pattern: abac -> Độ dài từ 5-15 ký tự
        //https://topdev.vn/blog/regex-la-gi/
        //Trong đó
        //      a: a-d -> 0 -> 5 ký tự
        //      b: f-z -> 1 -> 8 ký tự
        //      c: b-g -> >0 ký tự
        //affgiabbgg
        String pattern = "[a-d]{0,5}[f-z]{1,8}[a-d]{0,5}[b-g]*";
        
        //https://www.tutorialspoint.com/data_structures_algorithms/linear_search_algorithm.htm
        //https://www.analyticsvidhya.com/blog/2021/09/searching-in-data-structure-different-search-methods-explained/
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(s);
        
        while(m.find()) {
            String content = m.group();
            System.out.println("Tim thay ... > " + content);
        }
    }
}

Tags:



Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó