0% found this document useful (0 votes)
9 views3 pages

Employee Class Salary Management

The document outlines a Java program that defines an Employee class with attributes for ID, name, and salary, along with methods to read employee details and raise salaries by a given percentage. The main method demonstrates the functionality by allowing user input for multiple employees, displaying their details before and after salary raises. The program showcases the use of arrays and basic input/output operations in Java.

Uploaded by

tanushree9663
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Employee Class Salary Management

The document outlines a Java program that defines an Employee class with attributes for ID, name, and salary, along with methods to read employee details and raise salaries by a given percentage. The main method demonstrates the functionality by allowing user input for multiple employees, displaying their details before and after salary raises. The program showcases the use of arrays and basic input/output operations in Java.

Uploaded by

tanushree9663
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

/*

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.
*/
import [Link];

class Employee {
​ String ID;
​ String name;
​ double salary;

​ void read(Scanner scan) {
​ ​ [Link]("ID : ");
​ ​ ID = [Link]();
​ ​
​ ​ [Link]("Name : ");
​ ​ name = [Link]();
​ ​
​ ​ [Link]("Salary: ");
​ ​ salary = [Link]();
​ }

​ double raiseSalary(double p) {
​ ​ ​
​ ​ salary += salary * p;
​ ​ return salary;
​ } //End raiseSalary​

​ public void display() {


​ ​ [Link]("%5s %10s %10.2f \n",ID, name, salary);
​ }

} //End Employee

public class EmployeeDemo {


​ public static void main(String[] args) {

​ ​ int i, j;​ ​
​ ​
​ ​ Scanner scan = new Scanner([Link]);
​ ​
​ ​ [Link]("Enter number of employees: ");
​ ​ int n = [Link]();
​ ​ Employee emp[] = new Employee[n];
​ ​
​ ​ //Read Employee details
​ ​ [Link]("Enter employee details: ");
​ ​ for(i = 0; i < n; i++) {
​ ​ ​ [Link]("\nEmployee "+ (i+1) +": ");
​ ​ ​
​ ​ ​ emp[i] = new Employee();
​ ​ ​ emp[i].read(scan);
​ ​ }
​ ​
​ ​ //Display Employee details before Salary Raise
​ ​ [Link]("\nEmployee details(before Salary Raise): ");
​ ​ [Link]("%5s %10s %10s \n","ID","Name","Salary");
​ ​ for(i = 0; i < n; i++) {
​ ​ ​ emp[i].display();
​ ​ }
​ ​
​ ​ //Employee Salary Raise
​ ​ [Link]("\nEmployee Salary Raise(%): ");
​ ​ for(i = 0; i < n; i++) {
​ ​ ​ [Link]("Employee "+ (i+1) +": ");​ ​ ​
​ ​ ​
​ ​ ​ double raise = [Link]();
​ ​ ​ emp[i].raiseSalary(raise/100);
​ ​ }
​ ​
​ ​ //Display Employee details (after Salary Raise)
​ ​ [Link]("\nEmployee details(after Salary Raise): ");
​ ​ [Link]("%5s %10s %10s \n","ID","Name","Salary");
​ ​ for(i = 0; i < n; i++) {
​ ​ ​ emp[i].display();
​ ​ }
​ } //End main
} // End EmployeeDemo

/*
Output:
Enter number of employees: 3
Enter employee details:
Employee 1:
ID : 100
Name : Person1
Salary: 100000

Employee 2:
ID : 101
Name : Person2
Salary: 150000

Employee 3:
ID : 102
Name : Person3
Salary: 200000

Employee details(before Salary Raise):


ID Name Salary
100 Person1 100000.00
101 Person2 150000.00
102 Person3 200000.00

Employee Salary Raise(%):


Employee 1: 10
Employee 2: 15
Employee 3: 20

Employee details(after Salary Raise):


ID Name Salary
100 Person1 110000.00
101 Person2 172500.00
102 Person3 240000.00
*/

Common questions

Powered by AI

To enhance the functionality and security of the Employee class, several improvements can be implemented: 1) Use private access modifiers for the fields 'ID', 'name', and 'salary' to enforce encapsulation and prevent direct manipulation from outside the class. 2) Introduce getter and setter methods for accessing these fields. 3) Implement validation within the 'raiseSalary' method to ensure that the percentage increase is not negative. 4) Add data validation for the 'ID' and 'name' in the 'read' method to prevent incorrect data entry. 5) Consider the immutability of fields if the salary changes shouldn't affect historical records, and 6) Add error handling for cases when input types are invalid.

The main method in the EmployeeDemo class demonstrates object instantiation by first reading the number of employees and creating an array of Employee objects accordingly. This is achieved within a loop, where for each iteration, a new Employee object is instantiated with 'emp[i] = new Employee();'. It then uses the 'read' method to gather data for each employee. The method calls 'display', 'raiseSalary', and 'display' again to show the before and after effects of raising the salary. This sequence showcases the instantiation and usage of class methods to perform operations on the Employee objects.

The scalability of the Employee class design in handling a large number of Employee objects is primarily dependent on how instances are created and managed. Since it leverages an array for storage, the system faces a limitation because the array size is fixed once initialized, which could lead to inefficient memory use or require frequent resizing operations if adjustments are needed. Handling I/O operations like reading and writing employee data can become performance bottlenecks as input size increases. For improved scalability, transitioning to a dynamic data structure like ArrayList and implementing more sophisticated input/output strategies would be beneficial, as well as possibly introducing concurrency management for parallel processing of data.

The printf method in the display function of the Employee class plays a key role in formatting the output for display. It allows specifying the format for the string that represents employee details, ensuring that the ID, name, and salary are aligned properly for easier readability. In the format string "%5s %10s %10.2f \n", '%5s' and '%10s' set a width for the columns representing ID and name, while '%10.2f' specifies a floating-point number format for the salary, limiting it to two decimal places. This leads to a consistent and professional layout of employee data on the console.

The user interaction design in the EmployeeDemo class primarily relies on console input and output, which is straightforward but has notable limitations in usability. The method displays prompts on the console and reads user input sequentially, which can be error-prone given that it requires users to follow a specific input order. This design lacks input validation and error checking, increasing the chance of incorrect data entry leading to runtime errors. Users have minimal feedback on their actions, and navigation is non-existent in case a user needs to correct an error. For enhanced usability, the program could incorporate a more resilient input-handling mechanism with options for correcting mistakes, along with GUI elements to present information more intuitively.

The design of the Employee class aligns with object-oriented programming principles by encapsulating properties such as 'ID', 'name', and 'salary' and operations (methods) in a single entity (class). It uses methods like 'read', 'raiseSalary', and 'display' to manipulate and access the data fields, promoting data abstraction. However, the class could better adhere to encapsulation by restricting direct access to its fields using access control (e.g., private fields). The EmployeeDemo class demonstrates the principle of instantiation, where objects are created and their methods used, depicting interaction between objects.

When implementing the raiseSalary method in the Employee class, several edge cases should be considered: 1) Handling negative raise percentages, which could accidentally reduce the salary instead of increasing it, thus requiring validation checks to prevent this. 2) Considering zero percent raises, which should not alter the salary; the function needs to ensure such cases are handled gracefully without errors. 3) Very large percentages that could unreasonably increase a salary. 4) The potential for floating-point arithmetic errors during salary calculation that could lead to slight inaccuracies when manipulating very small or very large numbers.

The Employee class ensures encapsulation by directly manipulating its own fields, such as 'ID', 'name', and 'salary', within its methods like 'read', 'raiseSalary', and 'display'. However, it lacks the use of access modifiers such as 'private' for its fields to fully enforce encapsulation, which would restrict direct access to these fields from outside the class. Furthermore, the class provides a method, 'raiseSalary', to modify the 'salary', showing an intention to control how its data is accessed and modified.

The 'raiseSalary' method in the Employee class functions by increasing an employee's salary by a specified percentage. It requires a single parameter 'p', which represents the percentage increase expressed as a decimal (for example, a 10% raise would be passed as 0.10). The method calculates the new salary by multiplying the current salary with the percentage 'p' and adds the result to the existing salary, then returns the updated salary value.

Not handling user input validation in the Employee class implementation can lead to several issues. Inputs that don't meet expected formats (e.g., non-numeric values for salary) can cause runtime exceptions, such as InputMismatchException, disrupting the program's flow. Moreover, it could result in incorrect data being recorded, such as an invalid ID or name containing numbers or special characters. Without input validation, there's also a risk of logical errors like raising a salary by an unintended percentage if an incorrect decimal is entered. This lack of validation compromises both the reliability and robustness of the program.

You might also like