Bài tập Java OOP: tạo các setter, getter tương ứng cho lớp Employee | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create an Employee class in Employee.java file with the following information:

Explanation:

  • id, firstName, lastName, salary attributes describe the ID, first name, last name and salary of the employee object.
  • Employee(id: int, firstName: String, lastName: String, salary: int) constructor parameter is used to initialize values to attributes.
  • getFullName() method returns the firstName + lastName.
  • The other methods are setter and getter.

A program to test the above Employee class:

class Entry {
	public static void main(String[] args) {
		Employee e = new Employee(1, "Kien", "Hoang", 1000);
		System.out.println("Id: " + e.getId());
		System.out.println("Name: " + e.getFullName());
		System.out.println("Salary: " + e.getSalary());
	}
}

When the above code is compiled and executed, it produces the following result:

Id: 1
Name: Kien Hoang
Salary: 1000

Instruction

You need to create an Employee class as below:

public class Employee {
	private int id;
	private String firstName;
	private String lastName;
	private int salary;

	public Employee(int id, String firstName, String lastName, int salary) {
		super();
		this.id = id;
		this.firstName = firstName;
		this.lastName = lastName;
		this.salary = salary;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getFullName() {
		return firstName + " " + lastName;
	}
	
	public int getSalary() {
		return salary;
	}

	public void setSalary(int salary) {
		this.salary = salary;
	}
}