Java: Employee Management System
Java OOP: Exercise-6 with Solution
Write a Java program to create a class called "Employee" with a name, job title, and salary attributes, and methods to calculate and update salary.
Sample Solution:
Java Code:
//Employee.java
public class Employee {
private String name;
private String jobTitle;
private double salary;
public Employee(String name, String jobTitle, double salary) {
this.name = name;
this.jobTitle = jobTitle;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJobTitle() {
return jobTitle;
}
public void setJobTitle(String jobTitle) {
this.jobTitle = jobTitle;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public void raiseSalary(double percentage) {
salary = salary + salary * percentage / 100;
}
public void printEmployeeDetails() {
System.out.println("Name: " + name);
System.out.println("Job Title: " + jobTitle);
System.out.println("Salary: " + salary);
}
}
The above class has three private attributes: name, jobTitle, and salary. It has a constructor that initializes these attributes with the values passed as arguments. It also has getter and setter methods to access and modify these attributes. In addition, it provides methods for raising salaries by a certain percentage and printing employee information.
//Main.java
public class Main {
public static void main(String[] args) {
Employee employee1 = new Employee("Franziska Waltraud", "HR Manager", 40000);
Employee employee2 = new Employee("Hubertus Andrea", "Software Engineer", 60000);
System.out.println("\nEmployee Details:");
employee1.printEmployeeDetails();
employee2.printEmployeeDetails();
employee1.raiseSalary(8);
employee2.raiseSalary(12);
System.out.println("\nAfter raising salary:");
System.out.println("\n8% for 'Franziska Waltraud':");
employee1.printEmployeeDetails();
System.out.println("\n12% for 'Hubertus Andrea':");
employee2.printEmployeeDetails();
}
}
In the above example code, we create two instances of the "Employee" class and print their details using the ’printEmployeeDetails()’ method. We then raise their salary using the ‘raiseSalary()’ method and print the updated details of the employees.
Sample Output:
Employee Details: Name: Franziska Waltraud Job Title: HR Manager Salary: 40000.0 Name: Hubertus Andrea Job Title: Software Engineer Salary: 60000.0 After raising salary: 8% for 'Franziska Waltraud': Name: Franziska Waltraud Job Title: HR Manager Salary: 43200.0 12% for 'Hubertus Andrea': Name: Hubertus Andrea Job Title: Software Engineer Salary: 67200.0
Flowchart:
Java Code Editor:
Improve this sample solution and post your code through Disqus.
Java OOP Previous: Create a Circle class and calculate its area and circumference.
Java OOP Next: Bank Account Management.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics