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

Java Record 5

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

Java Record 5

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

DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

Ex no: 5
UML CLASS DIAGRAM
Date:

Question
Consider the following UML class diagram:

• Write a Java class for the Employee class based on the UML class
diagram.
• Write a main class Test Employee that creates two objects for the
Employee class and invoke all the methods.
Aim
To develop java class for the given UML class diagram.
Code
class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
public Employee(int id, String firstName, String lastName, int salary) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public int getID() {
return id;
}
public String getFirstName() {
return firstName;
10 717823P109
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

}
public String getLastName() {
return lastName;
}
public String getName() {
return firstName + " " + lastName;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getAnnualSalary() {
return salary * 12;
}
public int raiseSalary(int percent) {
salary += salary * percent / 100;
return salary;
}
@Override
public String toString() {
return "Employee ID: " + id + "\n" +
"Name: " + getName() + "\n" +
"Salary: " + salary;
}
}
public class TestEmployee {
public static void main(String[] args) {
Employee emp1 = new Employee(101, "John", "Doe", 5000);
Employee emp2 = new Employee(102, "Jane", "Smith", 6000);
System.out.println("Initial Employee Details:");
System.out.println(emp1);
System.out.println();
System.out.println(emp2);
emp1.setSalary(5500);
emp2.setSalary(6500);
System.out.println("\nModified Employee Details:");
System.out.println(emp1);
System.out.println();
System.out.println(emp2);
System.out.println("\nAnnual Salary:");
System.out.println(emp1.getName() + ": " + emp1.getAnnualSalary());
System.out.println(emp2.getName() + ": " + emp2.getAnnualSalary());
emp1.raiseSalary(10);
emp2.raiseSalary(15);
System.out.println("\nEmployee Details after Salary Raise:");
System.out.println(emp1);
System.out.println();
System.out.println(emp2);
}

11 717823P109
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

Output

12 717823P109
DEPARTMENT OF COMPUTERSCIENCE AND ENGINEERING 23CSR306 – JAVA PROGRAMMING

Result
Thus, the Java program for the given relationship has been successfully developed and the
output was verified.

13 717823P109

You might also like