Experiment 4
Experiment 4
Nesamanikandan S
Experiment-4
Programs Using Inheritance
1. Employee
Code:
class Employee
{
String emp_name;
int emp_id;
String address;
String mail_id;
int mob;
Employee(String emp_name,int emp_id,String address,String mail_id,int
mob)
{
this.emp_name=emp_name;
this.emp_id=emp_id;
this.address=address;
this.mail_id=mail_id;
this.mob=mob;
}
void display()
{
System.out.println("Employee name : " +emp_name);
System.out.println("Employee id : " +emp_id);
System.out.println("Address : n" +address);
System.out.println("Employee mail_id : " +mail_id);
System.out.println("Employee mobile no : " +mob);
}
}
class Programmer extends Employee
{
double BP;
Programmer(String n,int id,String add,String mail,double BP,int mob)
{
super(n,id,add,mail,mob);
this.BP=BP;
}
void calculate()
{
double DA=0.97*BP;
double HRA=0.1*BP;
double PF=0.12*BP;
double SCF=0.001*BP;
double gross=BP+HRA+DA;
double net=gross-PF-SCF;
953622104068
Nesamanikandan S
display();
System.out.println("Gross salary = " +gross);
System.out.println("Net salary = " +net);
}
}
class Assistant_Professor extends Employee
{
double BP;
Assistant_Professor(String n,int id,String add,String mail,double BP,int mob)
{
super(n,id,add,mail,mob);
this.BP=BP;
}
void calculate()
{
double DA=0.97*BP;
double HRA=0.1*BP;
double PF=0.12*BP;
double SCF=0.001*BP;
double gross=BP+HRA+DA;
double net=gross-PF-SCF;
display();
System.out.println("Gross salary = " +gross);
System.out.println("Net salary = " +net);
}
}
class Associative_Professor extends Employee
{
double BP;
Associative_Professor(String n,int id,String add,String mail,double BP,int
mob)
{
super(n,id,add,mail,mob);
this.BP=BP;
}
void calculate()
{
double DA=0.97*BP;
double HRA=0.1*BP;
double PF=0.12*BP;
double SCF=0.001*BP;
double gross=BP+HRA+DA;
double net=gross-PF-SCF;
display();
System.out.println("Gross salary = " +gross);
System.out.println("Net salary = " +net);
953622104068
Nesamanikandan S
}
}
class Professor extends Employee
{
double BP;
Professor(String n,int id,String add,String mail,double BP,int mob)
{
super(n,id,add,mail,mob);
this.BP=BP;
}
void calculate()
{
double DA=0.97*BP;
double HRA=0.1*BP;
double PF=0.12*BP;
double SCF=0.001*BP;
double gross=BP+HRA+DA;
double net=gross-PF-SCF;
display();
System.out.println("Gross salary = " +gross);
System.out.println("Net salary = " +net);
}
}
class Test
{
public static void main(String[] args)
{
System.out.println("Payroll of the Programmer : ");
Programmer p=new
Programmer("Abi",1,"rjpm","[email protected]",5000,9990);
p.calculate();
System.out.println();
System.out.println("Payroll of the Assistant_Professor : ");
Assistant_Professor a=new
Assistant_Professor("poovi",2,"chennai","[email protected]",6000,7500);
a.calculate();
System.out.println();
System.out.println("Payroll of the Associative_Professor : ");
Associative_Professor v=new
Associative_Professor("anu",3,"chennai","[email protected]",8000,7423);
v.calculate();
System.out.println();
System.out.println("Payroll of the Professor : ");
Professor s=new
Professor("hari",4,"chennai","[email protected]",10500,5600);
s.calculate();
953622104068
Nesamanikandan S
}
}
Output:
Payroll of the Programmer :
Employee name : Abi
Employee id : 1
Address : nrjpm
Employee mail_id : [email protected]
Employee mobile no : 9990
Gross salary = 10350.0
Net salary = 9745.0
2. Hiarichical
Code:
import java.util.Scanner;
class Figure
{
double dim1;
953622104068
Nesamanikandan S
double dim2;
Figure(double dim1, double dim2)
{
this.dim1 = dim1;
this.dim2 = dim2;
}
double area()
{
System.out.println("Area of the figure");
return 0;
}
}
Output:
Enter the dim 1
12
Enter the dim 2
13
Area of Rectangle : 156.0
Area of Triangle : 78.0
3. Single
Code:
import java.util.Scanner;
class Vehicle
{
int speed;
String type;
Vehicle(int speed, String type)
{
this.speed = speed;
this.type = type;
}
{
super(speed,type);
this.passengers = passengers;
}
public void displayInfo()
{
System.out.println("This is a " + type + " with speed of " + speed + " mph and
can carry " + passengers + " passengers.");
}
}
public class single
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the speed");
int speed=sc.nextInt();
sc.nextLine();
System.out.println("Enter the type");
String type=sc.nextLine();
System.out.println("Enter the passengers");
int passengers=sc.nextInt();
Car c = new Car(speed,type,passengers);
c.starting();
c.accelerating();
c.displayInfo();
c.stopping();
}
}
Output:
Enter the speed
72
Enter the type
Bus
Enter the passengers
65
The Bus is starting.
The Bus is accelerating.
This is a Bus with speed of 72 mph and can carry 65 passenger
4. Mutilevel
Code:
import java.util.Scanner;
class Student
{
953622104068
Nesamanikandan S
int rollNumber;
String name;
String branch;
Student(int rollNumber, String name, String branch)
{
this.rollNumber = rollNumber;
this.name = name;
this.branch = branch;
}
}
{
System.out.println("Result : Pass");
}
else
{
System.out.println("Result : Fail");
}
}
}
class school
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the roll number ");
int rollNumber=sc.nextInt();
sc.nextLine();
System.out.println("Enter the name");
String name=sc.nextLine();
System.out.println("Enter the branch");
String branch=sc.nextLine();
System.out.println("Enter the tamil mark");
int Tamil=sc.nextInt();
System.out.println("Enter the english mark ");
int English=sc.nextInt();
System.out.println("Enter the Maths");
int Maths=sc.nextInt();
Result r = new Result(rollNumber,name,branch,Tamil,English,Maths);
r.display();
}
}
Output:
Enter the roll number
68
Enter the name
Nesamanikandan s
Enter the branch
CSE
Enter the tamil mark
80
Enter the english mark
82
Enter the Maths
92
953622104068
Nesamanikandan S
Roll Number: 68
Name: Nesamanikandan s
Branch: CSE
Subject 1 Marks: 80
Subject 2 Marks: 82
Subject 3 Marks: 92
Total Marks: 254
Result : Pass