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

Employee Details Display Program

The document outlines a Java program that defines an 'Employee' class with subclasses 'Officer' and 'Manager'. It includes methods for displaying employee details and allows user input for creating instances of each class. The program successfully executes and displays the details of employees, officers, and managers based on user input.

Uploaded by

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

Employee Details Display Program

The document outlines a Java program that defines an 'Employee' class with subclasses 'Officer' and 'Manager'. It includes methods for displaying employee details and allows user input for creating instances of each class. The program successfully executes and displays the details of employees, officers, and managers based on user input.

Uploaded by

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

PROGRAM

import [Link].*;
class employee {
String Name; int Ph_no, Age, Salary;
String Address;
Scanner input=new Scanner([Link]);
employee(String name,int age,int ph_no,int salary,String address) {
Name=name;
Age=age;
Ph_no=ph_no;
Salary=salary;
Address=address;
}
void printsalary() {
[Link]("Salary : "+Salary);
}
void display() {
[Link]();
[Link]("Name : "+Name);
[Link]("Age : "+Age);
[Link]("Phone : "+Ph_no);
[Link]("Salary : "+Salary);
[Link]("Address : "+Address);
}}
class officer extends employee {
String Sp;
officer(String name,int age,int ph_no,int salary,String address,String sp) {
super(name,age,ph_no,salary,address);
Sp=sp;
}
void display() {
Date : 11 /09/2024

PROGRAM NO: 26

PROGRAM TO DISPLAY DETAILS OF MEMBERS OF AN


ORGANISATION

AIM
To write a program which creates a class named 'Employee' having the following members:
Name, Age, Phone number, Address, Salary. It also has a method named print-Salary() which
prints the salary of the Employee. Two classes 'Officer' and ‘Manager' inherit the 'Employee'
class. The 'Officer' and "Manager' classes have data members 'specialization' and 'department'
respectively. Now assign name, age, phone number, address and salary to an officer and a
manager by making an object of both of these classes and print the same.
ALGORITHM
I. START
Class: employee
1. Declare instance variables 'Name' of type String, 'Age' of type int, 'Ph_no' of type
int, 'Salary' of type int, and 'Address' of type String.
2. Declare a Scanner object 'input' to read user input.
3. Define a parameterized constructor 'employee' with parameters 'String name', 'int
age', 'int ph_no', 'int salary', and 'String address'
a. Assign the values of 'name', 'age', 'ph_no', 'salary', and 'address' to the instance
variables.
4. Define a method 'printsalary' with no parameters
a. Display "Salary: " followed by the value of 'Salary'
5. Define a method 'display' with no parameters
a. Display a new line.
b. Display "Name: " followed by the value of 'Name'
c. Display "Age: " followed by the value of 'Age'
d. Display "Phone: " followed by the value of 'Ph_no'
e. Display "Salary: " followed by the value of 'Salary'
f. Display "Address: " followed by the value of 'Address'
[Link]();
[Link]();
[Link]("Specialisation : "+Sp);
}}
class manager extends employee {
String Department;
manager(String name,int age,int ph_no,int salary,String address,String department) {
super(name,age,ph_no,salary,address);
Department=department;
}
void display() {
[Link]();
[Link]();
[Link]("Department : "+Department);
}}
class organisation {
public static void main(String args[]) {
String Name;
int Age, Ph_no, Salary, n,x,y;
String Address;
String Sp;
String Department;
Scanner input=new Scanner([Link]);
[Link]("Enter no of Employee : ");
n=[Link]();
employee e[]=new employee[n];
[Link]("Enter no of Manager : ");
x=[Link]();
manager m[]=new manager[x];
[Link]("Enter no of Officer : ");
y=[Link]();
officer o[]=new officer[y];
if (n>0) {
for (int i=0;i<n;i++) {
[Link]("Employee Details");
[Link]("Enter name : ");
Name=[Link]();
[Link]("Enter Age : ");
Class: officer extends employee
[The class officer will inherit the employee class]
1. Declare an instance variable 'Sp' of type String.
2. Define a parameterized constructor 'officer' with parameters 'String name', 'int age', 'int
ph_no', 'int salary', 'String address', and 'String sp'
a. Call the superclass constructor using 'super(name, age, ph_no, salary, address)'
b. Assign the value of 'sp' to the instance variable 'Sp'
3. Define a method 'display' with no parameters
a. Display a new line.
b. Call the superclass 'display' method using '[Link]()'
c. Display "Specialisation: " followed by the value of 'Sp'

Class: manager extends employee


[The class manager will inherit the employee class]
1. Declare an instance variable 'Department' of type String.
2. Define a parameterized constructor 'manager' with parameters 'String name', 'int age', 'int
ph_no', 'int salary', 'String address', and 'String department'
a. Call the superclass constructor using 'super(name, age, ph_no, salary, address)'
b. Assign the value of 'department' to the instance variable 'Department'
3. Define a method 'display' with no parameters
a. Display a new line.
b. Call the superclass 'display' method using '[Link]()'
c. Display "Department: " followed by the value of 'Department'
Age=[Link]();
[Link]("Enter phone : ");
Ph_no=[Link]();
[Link]("Enter Salary : ");
Salary=[Link]();
[Link]("Enter Address : ");
Address=[Link]();
e[i]=new employee(Name,Age,Ph_no,Salary,Address);
}}
if (y>0) {
for (int i=0;i<y;i++) {
[Link]("Officer Details");
[Link]("Enter name : ");
Name=[Link]();
[Link]("Enter Age : ");
Age=[Link]();
[Link]("Enter phone : ");
Ph_no=[Link]();
[Link]("Enter Salary : ");
Salary=[Link]();
[Link]("Enter Address : ");
Address=[Link]();
[Link]("Enter Specialisation : ");
Sp=[Link]();
o[i]=new officer(Name,Age,Ph_no,Salary,Address,Sp);
}}
if (x>0) {
for (int i=0;i<x;i++){
[Link]("Manager Details");
[Link]("Enter name : ");
Name=[Link]();
[Link]("Enter Age : ");
Age=[Link]();
[Link]("Enter phone : ");
Ph_no=[Link]();
[Link]("Enter Salary : ");
Salary=[Link]();
[Link]("Enter Address : ");
Class: organisation
1. Define the main method
a. Declare local variables 'Name', 'Age', 'Ph_no', 'Salary', 'Address' of types String and int.
b. Declare local variables 'n', 'x', and 'y' of type int.
c. Declare local variables 'Sp' and 'Department' of type String.
d. Create an array of 'employee' objects named 'e' with a size of 'n'.
e. Create an array of 'manager' objects named 'm' with a size of 'x'.
f. Create an array of 'officer' objects named 'o' with a size of 'y'.
g. Display "Enter no of Employee: "
h. Read the number of employees 'n' from the user.
i. Display "Enter no of Manager: "
j. Read the number of managers 'x' from the user.
k. Display "Enter no of Officer: "
l. Read the number of officers 'y' from the user.
m. Use a conditional statement to check if 'n' is greater than 0
i. Use a for loop to iterate from 0 to 'n'
- Display "Employee Details"
- Read employee details from the user and create an object 'e[i]' of type 'employee'
n. Use a conditional statement to check if 'y' is greater than 0
i. Use a for loop to iterate from 0 to 'y'
- Display "Officer Details"
- Read officer details from the user and create an object 'o[i]' of type 'officer'
o. Use a conditional statement to check if 'x' is greater than 0
i. Use a for loop to iterate from 0 to 'x'
- Display "Manager Details"
- Read manager details from the user and create an object 'm[i]' of type 'manager'
p. Use a for loop to iterate over 'o' and call the 'display' method on each officer object.
q. Use a for loop to iterate over 'm' and call the 'display' method on each manager object
r. Use a for loop to iterate over 'e' and call the 'display' method on each employee object

II. END
Address=[Link]();
[Link]("Enter Department : ");
Department=[Link]();
m[i]=new manager(Name,Age,Ph_no,Salary,Address,Department);
}}
for (int i=0;i<y;i++)
o[i].display();
for (int i=0;i<x;i++)
m[i].display();
for (int i=0;i<n;i++)
e[i].display();
}}

OUTPUT

Enter no of Employee : 0
Enter no of Manager : 1
Enter no of Officer : 1
Officer Details
Enter name : Serah
Enter Age : 23
Enter phone : 987868686
Enter Salary : 23000
Enter Address : efgh
Enter Specialisation : Communication
Manager Details
Enter name : Rohit
Enter Age : 33
Enter phone : 913579246
Enter Salary : 1500000
Enter Address : ijklm
Enter Department : HR
Name : Sera
Age : 23
Phone : 987868686
Salary : 23000
Address : efgh
Specialisation : Communication
Name : Rohit
Age : 33
Phone : 913579246
Salary : 1500000
Address : ijklm
Department : HR
RESULT
The program is successfully executed, and the output is obtained.

Common questions

Powered by AI

Class inheritance in this program promotes code reusability by allowing 'officer' and 'manager' subclasses to inherit common properties and methods from the 'employee' superclass. This eliminates the need to rewrite shared code (like 'Name', 'Age', 'Ph_no', etc.) in each subclass, reducing redundancy and potential errors, and easing future modifications since shared code resides in a single location .

Using a single 'Scanner' object throughout the program can lead to issues with input buffering and unwanted newline characters left in the buffer after integer inputs, which can affect subsequent input reads. Additionally, using 'Scanner' with 'next()' may unintentionally skip user input lines if not properly managed by adding '.nextLine()' calls to consume newline characters after using numeric inputs .

Polymorphism in this Java program is demonstrated through method overriding and object-oriented usage of base class references to call overridden methods in derived classes. The 'display' method is overridden in 'officer' and 'manager' classes. When iterating through arrays of 'officer' and 'manager' objects, the overridden 'display' method is called on each object, executing the code appropriate for their respective classes (printing 'Specialisation' for officers and 'Department' for managers).

The 'super' keyword in constructors of the 'officer' and 'manager' classes is used to call the constructor of the superclass 'employee'. This allows the subclasses to initialize the inherited fields (Name, Age, Ph_no, Salary, Address) using the parameters passed to them. Without this, the inherited properties could not be properly initialized in the subclass constructors .

The 'display' method implementation is effective for readability as it clearly segregates the inherited employee information from the role-specific information in subclasses through calls to 'super.display()' and additional statements. The explicit segmentation improves clarity and maintenance, as changes to display logic in employee details do not affect subclass implementations. However, it could benefit from reducing redundant code by centralizing repeated system outputs .

Conditional statements in the main method control the execution flow based on user input for the number of employees, officers, and managers. The program checks if the counts are greater than zero, branching to create and populate instances only when necessary. This prevents unnecessary operations and allows dynamic allocation based on actual data needs, improving efficiency and alignment with user requirements .

Encapsulation in this implementation is demonstrated by grouping data (Name, Age, Ph_no, Salary, Address) and behaviors (methods like 'display' and 'printsalary') into single entities, i.e., classes. Access to class data is managed through methods, ensuring controlled interaction with data. This promotes data integrity and security by restricting external access directly to the internal variables, allowing only allowed manipulations via class methods .

Encapsulation of employee details into separate classes like 'manager' and 'officer' allows for the specific needs and behaviors of these roles to be managed independently. Each class manages additional fields specific to its role, such as 'Specialisation' and 'Department'. This design promotes single responsibility and enhances the maintainability of the code by separating concerns, allowing future extensions or changes specific to roles without affecting other parts of the program .

The program uses dynamic arrays and loops to handle different numbers of employees, officers, and managers. It prompts users to enter the number of each type, uses the values to initialize array sizes, and iterates through each array to populate and display objects. This approach allows the program to scale based on input, accommodating varying numbers of each role dynamically .

Object arrays for employees, officers, and managers facilitate organized storage and management of these objects. Arrays allow easy iteration, indexing, and access to methods across all elements. This structure is beneficial for batch operations like displaying details where each object can be accessed and manipulated in a loop, promoting efficiency and code brevity in handling collections of similar objects .

You might also like