Practice (Chapter 7) - Thực hành về lớp

Bài 1: Tạo một lớp Employee gồm có các trường: name, age, designation và salary. Lớp Employee có hàm khởi tạo với tham số đầu vào là name, có hàm in tất cả các thông tin ra màn hình. Trong chương trình chính lần lượt tạo hai đối tượng có các thông số khác nhau.
Diagram:

Bài giải:
public class main {
       public static void main(String[] args) {
              Employee employeeOne = new Employee("Nguyen A");
              Employee employeeTwo = new Employee("Nguyen B");
              employeeOne.setAge(26);
              employeeOne.setDesignation("Senior Software Engineer");
              employeeOne.setSalary(1000);
              employeeOne.printEmployee();

              employeeTwo.setAge(21);
              employeeTwo.setDesignation("Software Engineer");
              employeeTwo.setSalary(500);
              employeeTwo.printEmployee();
       }
}

class Employee {
       private String name;
       private int age;
       private String designation;
       private double salary;

       public Employee(String _name) {
              this.name = _name;
       }
      
       public void setAge(int _age) {
              this.age = _age;
       }
      
       public void setDesignation(String _designation) {
              this.designation = _designation;
       }
      
       public void setSalary(double _salary) {
              this.salary = _salary;
       }
      
       public void printEmployee() {
             System.out.println("Name:"+ name );
             System.out.println("Age:" + age );
             System.out.println("Designation:" + designation );
             System.out.println("Salary:" + salary);
       }
}

Bài 2: Xây dựng lớp mô tả các điểm trên màn hình đồ họa. Lớp được đặt tên là DIEM:
+ Các thuộc tính gồm:
    . int x;// hoành độ
    . int y;// tung độ
    . String m;// màu sắc
+ Các phương thức gồm:
    . xây dựng hàm khởi tạo các điểm có tọa độ mặc định x=0, y=0, và màu Trắng.
    . xây dựng hàm khởi tạo có 3 tham số tọa độ x, y và màu sắc //nạp chồng hàm khởi tạo
    . xuất giá trị một điểm ra màn hình console (tọa độ x, y, màu sắc)
Bài giải:
class DIEM {
      int x, y;
      String mau;
      DIEM(){
            this.x = 0;
            this.y = 0;
            this.mau = "Trang";
      }
      DIEM(int x, int y, String m){
            this.x = x;
            this.y = y;
            this.mau = m;
      }
      void hienThiDiem(){
System.out.println("x="+this.x+", y="+this.y+",                                mau="+this.mau);
      }
}
public class Main {
      public static void main(String[] args) {
            DIEM objDiem1 = new DIEM();
            DIEM objDiem2 = new DIEM(100, 100, "Xanh");
           
            objDiem1.hienThiDiem();
            objDiem2.hienThiDiem();
      }
}

Bài 3:


Bài 4:


Bài 5: