Bài tập Java OOP: tạo các interface và class tương ứng | Codelearn

  • TuanLQ7
  • Easy
  • 100 Points

Task

Create interface and class as in the following class diagram:

Explanation:

The above class diagram describes a simple program used to calculate the Full Time and Part Time wages of an employee

interface IEmployee

  • calculateSalary() is an abstract method used to calculate employee salary.
  • getName() is an abstract method that returns the employee name. 

Employee abstract class

  • This class inherits from interface IEmployee
  • This is an abstract class that stores general information of employees.
  • name, paymentPerHour are the names and amount of money the employee receives for more than an hour of work.
  • Employee(name: String, paymentPerHour: int) is a parameter constructor used for initializing values to properties.
  • setName(), getName(), setPaymentPerHour(), getPaymentPerHour() are setter and getter.

PartTimeEmployee class

  • This class inherits from Employee abstract class.
  • workingHours is the attribute of the number of working hours per day of an employee.
  • calculateSalary() là phương thức ghi đè từ lớp trừ tượng Employee, phương thức này trả về tiền lương của nhân viên part-time theo công thức: workingHours * paymentPerHour.

FullTimeEmployee class

  • This class inherits from Employee abstract class
  • calculateSalary() method overrides Employee abstract class, it returns the full-time wage of an employee accroding to this formula: 8 * paymentPerHour (Full-time employees always work 8 hours a day so FullTimeEmployee class does not include workingHours attribute)

A program to test the above classes:

public class Entry {
	public static void main(String[] args) {
		IEmployee employee1 = new PartTimeEmployee("Trung", 45000, 7);
		System.out.println("Name: " + employee1.getName());
		System.out.println("Salary per day: " + employee1.calculateSalary());

		IEmployee employee2 = new FullTimeEmployee("Linh", 65000);
		System.out.println("Name: " + employee2.getName());
		System.out.println("Salary per day: " + employee2.calculateSalary());
	}
}

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

Name: Trung
Salary per day: 315000
Name: Linh
Salary per day: 520000

Instruction

IEmployee.java file

public interface IEmployee {
	int calculateSalary();
	String getName();
}

Employee.java file 

public abstract class Employee implements IEmployee {
	private String name;
	private int paymentPerHour;

	public Employee(String name, int paymentPerHour) {
		this.name = name;
		this.paymentPerHour = paymentPerHour;
	}

	@Override
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getPaymentPerHour() {
		return paymentPerHour;
	}

	public void setPaymentPerHour(int paymentPerHour) {
		this.paymentPerHour = paymentPerHour;
	}
}

PartTimeEmployee.java file 

public class PartTimeEmployee extends Employee {
	private int workingHours;

	public PartTimeEmployee(String name, int paymentPerHour, int workingHours) {
		super(name, paymentPerHour);
		this.workingHours = workingHours;
	}

	@Override
	public int calculateSalary() {
		return workingHours * getPaymentPerHour();
	}
}

FullTimeEmployee.java file

public class FullTimeEmployee extends Employee {
	public FullTimeEmployee(String name, int paymentPerHour) {
		super(name, paymentPerHour);
	}

	@Override
	public int calculateSalary() {
		return 8 * getPaymentPerHour();
	}
}