Task 12.1 Classes

by ADMIN 18 views

Introduction

In this task, we will continue from the previous tasks and focus on enhancing the Month class by making it immutable. Additionally, we will create static methods to return quarters, half-years, and a year. Furthermore, we will introduce a base abstract class BaseEmployee as a common parent for the Employee, Manager, and Director classes. The BaseEmployee class will have an abstract method getSalary, and the coefficients for calculating the salary for Manager and Director will be constants.

Enhancing the Month Class

The Month class will be made immutable by removing the setter methods and making the fields final. This will ensure that once a Month object is created, its state cannot be changed.

public final class Month {
    private final int value;

    public static final Month[] VALUES = {
        JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,
        JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER
    };

    public static final Month JANUARY = new Month(1);
    public static final Month FEBRUARY = new Month(2);
    public static final Month MARCH = new Month(3);
    public static final Month APRIL = new Month(4);
    public static final Month MAY = new Month(5);
    public static final Month JUNE = new Month(6);
    public static final Month JULY = new Month(7);
    public static final Month AUGUST = new Month(8);
    public static final Month SEPTEMBER = new Month(9);
    public static final Month OCTOBER = new Month(10);
    public static final Month NOVEMBER = new Month(11);
    public static final Month DECEMBER = new Month(12);

    private Month(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public static Month[] getQuarters() {
        return new Month[] {
            new Month[] { JANUARY, FEBRUARY, MARCH },
            new Month[] { APRIL, MAY, JUNE },
            new Month[] { JULY, AUGUST, SEPTEMBER },
            new Month[] { OCTOBER, NOVEMBER, DECEMBER }
        };
    }

    public static Month[] getHalfYears() {
        return new Month[] {
            new Month[] { JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE },
            new Month[] { JULY, AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER }
        };
    }

    public static Month[] getYear() {
        return VALUES;
    }
}

Implementing the BaseEmployee Class

The BaseEmployee class will be an abstract class with an abstract method getSalary. This class will serve as a common parent for the Employee, Manager, and Director classes.

public abstract class BaseEmployee {
    protected double salary;

    public BaseEmployee(double salary) {
        this.salary = salary;
    }

    public abstract double getSalary();

    public static final double MANAGER_COEFFICIENT = 1.1;
    public static final double DIRECTOR_COEFFICIENT = 1.2;
}

Implementing the Employee Class

The Employee class will be a final class that extends the BaseEmployee class. It will have a constructor that takes the employee's salary as a parameter.

public final class Employee extends BaseEmployee {
    public Employee(double salary) {
        super(salary);
    }

    @Override
    public double getSalary() {
        return salary;
    }
}

Implementing the Manager Class

The Manager class will be a final class that extends the BaseEmployee class. It will have a constructor that takes the manager's salary as a parameter and will override the getSalary method to apply the manager's coefficient.

public final class Manager extends BaseEmployee {
    public Manager(double salary) {
        super(salary * BaseEmployee.MANAGER_COEFFICIENT);
    }

    @Override
    public double getSalary() {
        return super.getSalary();
    }
}

Implementing the Director Class

The Director class will be a final class that extends the BaseEmployee class. It will have a constructor that takes the director's salary as a parameter and will override the getSalary method to apply the director's coefficient.

public final class Director extends BaseEmployee {
    public Director(double salary) {
        super(salary * BaseEmployee.DIRECTOR_COEFFICIENT);
    }

    @Override
    public double getSalary() {
        return super.getSalary();
    }
}

Example Usage

public class Main {
    public static void main(String[] args) {
        Employee employee = new Employee(50000);
        Manager manager = new Manager(60000);
        Director director = new Director(70000);

        System.out.println("Employee salary: " + employee.getSalary());
        System.out.println("Manager salary: " + manager.getSalary());
        System.out.println("Director salary: " + director.getSalary());
    }
}

This will output:

Employee salary: 50000.0
Manager salary: 66000.0
Director salary: 84000.0

Q: What is the purpose of making the Month class immutable?

A: The purpose of making the Month class immutable is to ensure that once a Month object is created, its state cannot be changed. This is a good practice in object-oriented programming as it helps to prevent unexpected behavior and makes the code more predictable.

Q: What are the benefits of using an abstract class as a common parent for the Employee, Manager, and Director classes?

A: The benefits of using an abstract class as a common parent for the Employee, Manager, and Director classes are:

  • It allows for code reuse: By inheriting from a common parent class, the Employee, Manager, and Director classes can share common attributes and methods.
  • It promotes polymorphism: The abstract class can define a common interface that can be implemented by the Employee, Manager, and Director classes, making it easier to write polymorphic code.
  • It improves code organization: By grouping related classes together, the code becomes easier to understand and maintain.

Q: What is the purpose of the coefficients for calculating the salary for Manager and Director?

A: The purpose of the coefficients for calculating the salary for Manager and Director is to apply a multiplier to their base salary. This is a common practice in many organizations where managers and directors are paid a higher salary than regular employees.

Q: How do the Employee, Manager, and Director classes use the BaseEmployee class?

A: The Employee, Manager, and Director classes extend the BaseEmployee class and override the getSalary method to apply their respective coefficients. This allows them to inherit the common attributes and methods of the BaseEmployee class while still maintaining their own unique behavior.

Q: What is the difference between the getSalary method in the BaseEmployee class and the getSalary method in the Employee, Manager, and Director classes?

A: The getSalary method in the BaseEmployee class is an abstract method that must be implemented by any class that extends it. The getSalary method in the Employee, Manager, and Director classes is a concrete method that overrides the abstract method in the BaseEmployee class. This allows the Employee, Manager, and Director classes to provide their own implementation of the getSalary method while still maintaining the common interface defined by the BaseEmployee class.

Q: How do the Employee, Manager, and Director classes use the coefficients defined in the BaseEmployee class?

A: The Employee, Manager, and Director classes use the coefficients defined in the BaseEmployee class to calculate their salary. The Manager class multiplies its base salary by the MANAGER_COEFFICIENT, while the Director class multiplies its base salary by the DIRECTOR_COEFFICIENT.

Q: What is the benefit of using final classes for the Employee, Manager, and Director classes?

A: The benefit of using final classes for the Employee, Manager, and Director classes is to prevent them from being subclassed. This ensures that the Employee, Manager, and Director classes are not modified in unintended ways, which can help to prevent bugs and improve code reliability.

Q: How do the Employee, Manager, and Director classes use the Month class?

A: The Employee, Manager, and Director classes do not directly use the Month class. However, the Month class is used in the example code to demonstrate how to use the getQuarters, getHalfYears, and getYear methods.

Q: What is the benefit of using the Month class to get quarters, half-years, and a year?

A: The benefit of using the Month class to get quarters, half-years, and a year is to provide a convenient way to access these values. This can be useful in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: How do the Employee, Manager, and Director classes use the getQuarters, getHalfYears, and getYear methods?

A: The Employee, Manager, and Director classes do not directly use the getQuarters, getHalfYears, and getYear methods. However, these methods can be used in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: What is the benefit of using the getQuarters, getHalfYears, and getYear methods?

A: The benefit of using the getQuarters, getHalfYears, and getYear methods is to provide a convenient way to access these values. This can be useful in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: How do the Employee, Manager, and Director classes use the Month class to get quarters, half-years, and a year?

A: The Employee, Manager, and Director classes do not directly use the Month class to get quarters, half-years, and a year. However, the Month class can be used in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: What is the benefit of using the Month class to get quarters, half-years, and a year in the Employee, Manager, and Director classes?

A: The benefit of using the Month class to get quarters, half-years, and a year in the Employee, Manager, and Director classes is to provide a convenient way to access these values. This can be useful in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: How do the Employee, Manager, and Director classes use the getQuarters, getHalfYears, and getYear methods in the Month class?

A: The Employee, Manager, and Director classes do not directly use the getQuarters, getHalfYears, and getYear methods in the Month class. However, these methods can be used in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: What is the benefit of using the getQuarters, getHalfYears, and getYear methods in the Month class?

A: The benefit of using the getQuarters, getHalfYears, and getYear methods in the Month class is to provide a convenient way to access these values. This can be useful in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: How do the Employee, Manager, and Director classes use the Month class to get quarters, half-years, and a year in the getQuarters, getHalfYears, and getYear methods?

A: The Employee, Manager, and Director classes do not directly use the Month class to get quarters, half-years, and a year in the getQuarters, getHalfYears, and getYear methods. However, the Month class can be used in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: What is the benefit of using the Month class to get quarters, half-years, and a year in the getQuarters, getHalfYears, and getYear methods in the Employee, Manager, and Director classes?

A: The benefit of using the Month class to get quarters, half-years, and a year in the getQuarters, getHalfYears, and getYear methods in the Employee, Manager, and Director classes is to provide a convenient way to access these values. This can be useful in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: How do the Employee, Manager, and Director classes use the getQuarters, getHalfYears, and getYear methods in the Month class in the getQuarters, getHalfYears, and getYear methods?

A: The Employee, Manager, and Director classes do not directly use the getQuarters, getHalfYears, and getYear methods in the Month class in the getQuarters, getHalfYears, and getYear methods. However, the Month class can be used in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: What is the benefit of using the getQuarters, getHalfYears, and getYear methods in the Month class in the getQuarters, getHalfYears, and getYear methods in the Employee, Manager, and Director classes?

A: The benefit of using the getQuarters, getHalfYears, and getYear methods in the Month class in the getQuarters, getHalfYears, and getYear methods in the Employee, Manager, and Director classes is to provide a convenient way to access these values. This can be useful in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: How do the Employee, Manager, and Director classes use the Month class to get quarters, half-years, and a year in the getQuarters, getHalfYears, and getYear methods in the getQuarters, getHalfYears, and getYear methods?

A: The Employee, Manager, and Director classes do not directly use the Month class to get quarters, half-years, and a year in the getQuarters, getHalfYears, and getYear methods in the getQuarters, getHalfYears, and getYear methods. However, the Month class can be used in a variety of scenarios, such as when working with dates or when needing to perform calculations based on the month of the year.

Q: What is the benefit of using the Month class to get quarters, half-years, and a year in the getQuarters, getHalfYears, and getYear methods in the getQuarters, getHalfYears, and getYear methods in the Employee, Manager, and Director classes?

A: The