By GokiSoft.com| 19:17 18/07/2022|
Java Basic

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

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


#AgeExeption.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt3006;

/**
 *
 * @author std
 */
public class AgeExeption extends Exception{
}


#EmailException.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt3006;

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


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt3006;

/**
 *
 * @author std
 */
public class Main {
    public static void main(String[] args) {
        Student std;
        
        System.out.println("Test case 1");
        //Test case 1 -> Email & Age error
        std = new Student("A", "agmail.com", -20);
        
        System.out.println("Test case 2");
        //Test case 2
        std = new Student("A", "agmail.com", 20);
        
        System.out.println("Test case 3");
        //Test case 3
        std = new Student("A", "a@gmail.com", 20);
    }
}


#Student.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bt3006;

import java.util.logging.Level;
import java.util.logging.Logger;

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

    public Student(String fullname, String email, int age) {
        setFullname(fullname);
        try {
            setEmail(email);
            System.out.println("Email success");
        } catch (EmailException ex) {
            System.out.println("Email error");
        }
        try {
            setAge(age);
            System.out.println("Age success");
        } catch (AgeExeption ex) {
            System.out.println("Age error");
        }
    }
    
    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 AgeExeption {
        if(age < 0) {
            throw new AgeExeption();
        }
        this.age = age;
    }
}


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 đó