Create a class called Employee that 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 EmployeeTest 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.

Respuesta :

ijeggs

Answer:

public class Employee {

   private String Fname;

   private String Lname;

   private double monthlySalary;

   //Constructor

   public Employee(String fname, String lname, double monthlySalary) {

       Fname = fname;

       Lname = lname;

       this.monthlySalary = monthlySalary;

   }

   //Set and Get Methods

   public String getFname() {

       return Fname;

   }

   public void setFname(String fname) {

       Fname = fname;

   }

   public String getLname() {

       return Lname;

   }

   public void setLname(String lname) {

       Lname = lname;

   }

   public double getMonthlySalary() {

       return monthlySalary;

   }

   public void setMonthlySalary(double monthlySalary) {

       if(monthlySalary>0) {

           this.monthlySalary = monthlySalary;

       }

   }

}

public class EmployeeTest {

   public static void main(String[] args) {

       Employee employeeOne = new Employee("Dave","Jones",1800);

       Employee employeeTwo = new Employee("Junely","Peters",1200);

       double empOnecurrentYrSalary =(employeeOne.getMonthlySalary()*12);

               System.out.println("Employee One Yearly salary is " +

               ""+empOnecurrentYrSalary);

       double empTwocurrentYrSalary =(employeeTwo.getMonthlySalary()*12);

       System.out.println("Employee Two Yearly salary is " +

               ""+empTwocurrentYrSalary);

// Incremments of 10%

        double newSalaryOne = empOnecurrentYrSalary+(empOnecurrentYrSalary*0.1);

        double newSalaryTwo = empTwocurrentYrSalary+(empTwocurrentYrSalary*0.1);

       employeeOne.setMonthlySalary(newSalaryOne);

       employeeTwo.setMonthlySalary(newSalaryTwo);

       System.out.println("Employee One's New Yearly Salary is " +

               ""+(employeeOne.getMonthlySalary()));

       System.out.println("Employee Two's New Yearly Salary is " +

               ""+(employeeTwo.getMonthlySalary()));

   }

}

Explanation:

As required by the question Two Java classes are created Employee and EmployeeTest

The required fields, constructor, get and set methods are all created in the Employee class, pay attention to the comments in the code

In the EmployeeTest, two objects of the class are created as required by the question.

The current salaries are printed as initialized by the constructor

Then an increase of 10% is added and the new salary is printed.

Note the use of the setMonthlySalary and getMonthlySalary for the update and display of the salaries respectively

ACCESS MORE