java_26
java_26
import java.util.*;
class employee {
String Name; int Ph_no, Age, Salary;
String Address;
Scanner input=new Scanner(System.in);
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() {
System.out.println("Salary : "+Salary);
}
void display() {
System.out.println();
System.out.println("Name : "+Name);
System.out.println("Age : "+Age);
System.out.println("Phone : "+Ph_no);
System.out.println("Salary : "+Salary);
System.out.println("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
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'
System.out.println();
super.display();
System.out.println("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() {
System.out.println();
super.display();
System.out.println("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(System.in);
System.out.print("Enter no of Employee : ");
n=input.nextInt();
employee e[]=new employee[n];
System.out.print("Enter no of Manager : ");
x=input.nextInt();
manager m[]=new manager[x];
System.out.print("Enter no of Officer : ");
y=input.nextInt();
officer o[]=new officer[y];
if (n>0) {
for (int i=0;i<n;i++) {
System.out.println("Employee Details");
System.out.print("Enter name : ");
Name=input.next();
System.out.print("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 'super.display()'
c. Display "Specialisation: " followed by the value of 'Sp'
II. END
Address=input.next();
System.out.print("Enter Department : ");
Department=input.next();
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.