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

javaprogram3

Vtu sem 3 Java program 3

Uploaded by

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

javaprogram3

Vtu sem 3 Java program 3

Uploaded by

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

Program 03: Employee Class

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.

Java Code:

public class Employee {

private int id;

private String name;

private double salary;

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

this.id = id;

this.name = name;

this.salary = salary;

public void raiseSalary(double percent) {

if (percent > 0) {

double raiseAmount = salary * (percent / 100);

salary += raiseAmount;

System.out.println(name + "'s salary raised by " + percent + "%. New salary: $" + salary);

} else {

System.out.println("Invalid percentage. Salary remains unchanged.");

public String toString() {

return "Employee ID: " + id + ", Name: " + name + ", Salary: $" + salary;

public static void main(String[] args) {

// Creating an Employee object

Employee employee = new Employee(1, "John Doe", 50000.0);

// Displaying employee details


System.out.println("Initial Employee Details:");

System.out.println(employee);

// Raising salary by 10%

employee.raiseSalary(10);

// Displaying updated employee details

System.out.println("\nEmployee Details after Salary Raise:");

System.out.println(employee);

In this example, the Employee class has a constructor to initialize the employee’s ID, name, and

salary. The raiseSalary method takes a percentage as a parameter and raises the salary

accordingly. The toString method is overridden to provide a meaningful string representation of

the Employee object. The main method demonstrates the usage of the Employee class by creating

an instance, displaying its details, raising the salary, and then displaying the updated details.

Output:

Initial Employee Details:

Employee ID: 1, Name: John Doe, Salary: $50000.0

John Doe's salary raised by 10.0%. New salary: $55000.0

Employee Details after Salary Raise:

Employee ID: 1, Name: John Doe, Salary: $55000.0

You might also like