Aaron

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CS433P Programming Paradigm Lab

Register No. 2160302


EXPERIMENT NO 3
DATE – 25 Jan 2023

INHERITANCE

AIM: Implementation of Inheritance – how inheritance is handled using java keywords: extends
and implements.

PROGRAM
Create a super class Person. Declare the appropriate member variables and member methods.
create two derived classes Student and Faculty.

In class Person,

Write 1. a method to get the name of the person, Age of the person, Department of the person.

In student class,

Write 1.a method to get register number , the marks for student.( m1,m2,m3) .

It should call the parent class method to get name of the student, age of the student and
Department of the student

2. a method to calculate the total marks and average marks..

3. a method to display the name of the student, Age, department, register number, marks , total
marks and average in table format.

In Faculty Class,

Write 1. A method to get the faculty ID, Basic salary, HRA, DA, PF. .

It should call the parent class method to get name of the student, age of the student and
Department of the Faculty member.

2. Write a method to calculate the total salary of the faculty.

3. A method to display all the details of the faculty in table format.

Department of Computer Science and Engineering, Christ (Deemed to be University) 1


CS433P Programming Paradigm Lab

SOURCE CODE

import java.util.Scanner;

class Person
{
String name;
int age;
String Dept_name;
void read()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the name");
name =sc.next();
System.out.println("Enter the age");
age=sc.nextInt();
System.out.println("Enter the Deptarment name");
Dept_name= sc.next();

Department of Computer Science and Engineering, Christ (Deemed to be University) 1


CS433P Programming Paradigm Lab
void print()
{
System.out.println("Name"+name);
System.out.println("Age"+age);
System.out.println("dept"+Dept_name);
}
}

class Student extends Person


{
int reg_no;
int m1,m2,m3;
int total;
double avg;
void read(){
Scanner sc= new Scanner(System.in);
super.read();

System.out.println("Enter the Reg_no: ");


reg_no =sc.nextInt();
System.out.println("Enter the marks (m1)");
m1 =sc.nextInt();
System.out.println("Enter the marks (m2)");
m2 =sc.nextInt();
System.out.println("Enter the marks (m3)");
m3 =sc.nextInt();
}
void average(){
total=m1+m2+m3;
avg=total/3;

Department of Computer Science and Engineering, Christ (Deemed to be University) 1


CS433P Programming Paradigm Lab
void print(){
System.out.println("Name | | "+name);
System.out.println("Age | | "+age);
System.out.println("dept | |"+Dept_name);
System.out.println("Reg | |"+reg_no);
System.out.println("mark1 | |"+m1);
System.out.println("mark2 | |"+m2);
System.out.println("mark3 | |"+m3);
System.out.println("total | |"+total);
System.out.println("averg | |"+avg);

}
void Search_id(){
int Search_id;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the Reg_no: ");
Search_id =sc.nextInt();
if(Search_id==reg_no){
System.out.println("Name | | "+name);
System.out.println("Age | | "+age);
System.out.println("dept | |"+Dept_name);
System.out.println("Reg | |"+reg_no);
System.out.println("mark1 | |"+m1);
System.out.println("mark2 | |"+m2);
System.out.println("mark3 | |"+m3);
System.out.println("total | |"+total);
System.out.println("averg | |"+avg);

}
else

Department of Computer Science and Engineering, Christ (Deemed to be University) 1


CS433P Programming Paradigm Lab
System.out.println("Invalid reg_no");

class Faculty extends Person{


int faculty_ID;
int Basic_salary, HRA, DA, FA, salary;

void read()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter the name");
name =sc.next();
System.out.println("Enter the age");
age=sc.nextInt();
System.out.println("Enter the Deptarment name");
Dept_name= sc.next();
System.out.println("Enter the faculty id");
faculty_ID=sc.nextInt();
System.out.println("Enter the Basic Salary");
Basic_salary =sc.nextInt();
System.out.println("Enter the HRA");
HRA= sc.nextInt();
System.out.println("Enter the DA");
DA= sc.nextInt();
System.out.println("Enter the FA");
FA= sc.nextInt();
}

Department of Computer Science and Engineering, Christ (Deemed to be University) 1


CS433P Programming Paradigm Lab
void Tsalary(){
salary= Basic_salary+HRA+DA+FA;
}
void display(){
System.out.println("name|"+name);

System.out.println("age |"+age);

System.out.println("Dept| "+Dept_name);

System.out.println("fac |"+faculty_ID);

System.out.println("BS |"+Basic_salary);

System.out.println("HRA |"+HRA);

System.out.println("DA |"+DA);

System.out.println("FA |"+FA);

System.out.println("total |"+salary);

}
void Search_fid(){
int Search_fid;
Scanner sc= new Scanner(System.in);
System.out.println("Enter the Faculty_id: ");
Search_fid =sc.nextInt();
if(Search_fid==faculty_ID){
System.out.println("faculty id | | "+faculty_ID);
System.out.println("Basic salary | | "+Basic_salary);

Department of Computer Science and Engineering, Christ (Deemed to be University) 1


CS433P Programming Paradigm Lab
System.out.println("HRA | |"+HRA);
System.out.println("DA | |"+DA);
System.out.println("FA | |"+FA);

}
else
System.out.println("Invalid faculty id");

}
public class Main {

public static void main(String[] args) {


int choice;
Scanner sc= new Scanner(System.in);
Person p =new Person();
Student s =new Student();
Faculty f =new Faculty();
System.out.println("1. Student\n 2.Faculty");
choice = sc.nextInt();
if(choice==1){
s.read();

Department of Computer Science and Engineering, Christ (Deemed to be University) 1


CS433P Programming Paradigm Lab
s.average();
s.print();
s.Search_id();

}
else
if(choice == 2){
f.read();
f.Tsalary();
f.display();
f.Search_fid();
}
else
System.out.println("Invalid choice");

Department of Computer Science and Engineering, Christ (Deemed to be University) 1


CS433P Programming Paradigm Lab

OUTPUT

Department of Computer Science and Engineering, Christ (Deemed to be University) 1


CS433P Programming Paradigm Lab

Department of Computer Science and Engineering, Christ (Deemed to be University) 1

You might also like