0% found this document useful (0 votes)
34 views

Code For Employee Class in Java

The Employee class contains private variables to store an employee's first name, last name, and monthly salary. It includes getter and setter methods for each variable, as well as methods to calculate the employee's yearly salary and yearly salary with a 10% raise. The constructor initializes the name and salary variables, and sets the salary to 0 if a negative value is passed.

Uploaded by

Ted Barancira
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Code For Employee Class in Java

The Employee class contains private variables to store an employee's first name, last name, and monthly salary. It includes getter and setter methods for each variable, as well as methods to calculate the employee's yearly salary and yearly salary with a 10% raise. The constructor initializes the name and salary variables, and sets the salary to 0 if a negative value is passed.

Uploaded by

Ted Barancira
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

public class Employee

{
private String firstName; // instance variable that stores the first name
private String lastName; // instance variable that stores the last name
private double monthlySalary; // instance variable

// constructor initializes firstName, lastName and monthlySalary with String


and double supplied as argument
public Employee (String fname, String lname, double msalary)
{
firstName = fname; // initialize firstName
lastName = lname; // initialize lastName
monthlySalary = msalary; // initialize monthlySalary

// if the monthly salary is not positive, set it to 0.0.


if (msalary < 0.0)
monthlySalary = 0.0;
} // end constructor

// method to set the first name


public void setFirstName (String fname)
{
firstName = fname; // store the first name

} // end method setFirstName

// method to retrieve first name


public String getFirstName ()
{
return firstName;
} // end method getFirstName

// method to set the last name


public void setLastName (String lname)
{
lastName = lname; // store the last name
} // end method setLastName

// method to retrieve last name


public String getLastName ()
{
return lastName;
} // end method getLastName

// method to set the monthly salary


public void setMonthlySalary (double msalary)
{
monthlySalary = msalary; // store the monthly salary

} // end method setMonthlySalary

// method to retrieve monthly salary


public double getMonthlySalary ()
{
return monthlySalary;
} // end method getMonthlySalary

// method to retrieve yearly salary


public double getYearlySalary()
{
double yearlySalary = monthlySalary * 12;
return yearlySalary;
} // end method getYearlySalary

// method to retrieve yearly salary after giving 10% raise


public double getRaiseSalary()
{
double raise = monthlySalary * 0.1 ;
double raiseSalary = ( monthlySalary + raise ) * 12;
return raiseSalary;
} // end method getRaiseSalary

} // end class Employee

You might also like