0% found this document useful (0 votes)
2 views2 pages

Program 3

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views2 pages

Program 3

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

Program 3: A class called Employee, which models an employee with an ID, name and salary, is designed as shown

in the following class diagram. The method raiseSalary (percent) increases the salary by the given percentage.
Develop the Employee class and suitable main method for demonstration.

Aim: Demonstrating creation of java classes, object, declaration and initialization of variables

import java.util.Scanner;
public class Employee_Sal_Inc {
private int id;
private String name;
private double salary;

public Employee_Sal_Inc(int id, String name, double salary) {


this.id = id;
this.name = name;
this.salary = salary;
}

public int getId() {


return id;
}

public String getName() {


return name;
}

public double getSalary() {


return salary;
}

public void raiseSalary(double percent) {


salary += salary * percent / 100;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
'}';
}

public static void main(String[] args) {


Employee_Sal_Inc employee = new Employee_Sal_Inc(1, "John", 50000);
System.out.println("Employee before salary increase: " + employee);
Scanner sc = new Scanner(System.in);
System.out.println("Enter the percentage increment for the employee:");
employee.raiseSalary(sc.nextDouble());
System.out.println("Employee after salary increase: " + employee);

}
}
Output:
Employee before salary increase: Employee{id=1, name='John', salary=50000.0}
Enter the percentage increment for the employee:
25
Employee after salary increase: Employee{id=1, name='John', salary=62500.0}

You might also like