Java class definition create Employee class

Question

We would like to create a class called Employee.

It includes three instance variables-a first name (type String), a last name (type String) and a monthly salary (double).

Provide a constructor that initializes the three instance variables.

Provide a set and a get method for each instance variable.

If the monthly salary is not positive, do not set its value.

Write a test app named Main that demonstrates class Employee's capabilities.

Create two Employee objects and display each object's yearly salary.

Then give each Employee a 10% raise and display each Employee's yearly salary again.

class Employee {//from  w ww .  j  av a2 s.co  m
  //your code here
}

public class Main {
  public static void main(String[] args) {
    Employee employee1 = new Employee("A", "CSS", -50);
    Employee employee2 = new Employee("B", "HTML", 1500);

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee1.getFistName(), employee1.getLastName(),
        employee1.getSalary());

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee2.getFistName(), employee2.getLastName(),
        employee2.getSalary());

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee1.getFistName(), employee1.getLastName(),
        employee1.getSalary() * 1.10);

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee2.getFistName(), employee2.getLastName(),
        employee2.getSalary() * 1.10);

    employee1.setSalary(1200);

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee1.getFistName(), employee1.getLastName(),
        employee1.getSalary() * 1.10);

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee2.getFistName(), employee2.getLastName(),
        employee2.getSalary() * 1.10);
  }
}


class Employee {
  private String fistName;
  private String lastName;
  private double salary;

  public Employee(String fistName, String lastName, double salary) {
    this.fistName = fistName;
    this.lastName = lastName;

    if (salary > 0)
      this.salary = salary;
  }

  public void setFistName(String fistName) {
    this.fistName = fistName;
  }

  public String getFistName() {
    return fistName;
  }

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

  public String getLastName() {
    return lastName;
  }

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

  public double getSalary() {
    return salary;
  }
}

public class Main {
  public static void main(String[] args) {
    Employee employee1 = new Employee("A", "CSS", -50);
    Employee employee2 = new Employee("B", "HTML", 1500);

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee1.getFistName(), employee1.getLastName(),
        employee1.getSalary());

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee2.getFistName(), employee2.getLastName(),
        employee2.getSalary());

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee1.getFistName(), employee1.getLastName(),
        employee1.getSalary() * 1.10);

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee2.getFistName(), employee2.getLastName(),
        employee2.getSalary() * 1.10);

    employee1.setSalary(1200);

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee1.getFistName(), employee1.getLastName(),
        employee1.getSalary() * 1.10);

    System.out.printf("Name: %s %s%nSalary: %.2f%n%n", employee2.getFistName(), employee2.getLastName(),
        employee2.getSalary() * 1.10);
  }
}



PreviousNext

Related