953622104068
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
Payroll of the Assistant_Professor :
Employee name : poovi
Employee id : 2
Address : nchennai
Employee mail_id :
[email protected] Employee mobile no : 7500
Gross salary = 12420.0
Net salary = 11694.0
Payroll of the Associative_Professor :
Employee name : anu
Employee id : 3
Address : nchennai
Employee mail_id :
[email protected] Employee mobile no : 7423
Gross salary = 16560.0
Net salary = 15592.0
Payroll of the Professor :
Employee name : hari
Employee id : 4
Address : nchennai
Employee mail_id :
[email protected] Employee mobile no : 5600
Gross salary = 21735.0
Net salary = 20464.5
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;
}
}
class Rectangle extends Figure
{
Rectangle(double length, double breadth)
{
super(length, breadth);
}
double area()
{
return dim1 * dim2;
}
}
class Triangle extends Figure
{
public Triangle(double base, double height)
{
super(base, height);
}
double area()
{
return 0.5 * dim1 * dim2;
}
}
public class Area
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the dim 1");
double dim1=sc.nextDouble();
System.out.println("Enter the dim 2");
double dim2=sc.nextDouble();
Rectangle r = new Rectangle(dim1,dim2);
953622104068
Nesamanikandan S
Triangle t = new Triangle(dim1,dim2);
System.out.println("Area of Rectangle : " + r.area());
System.out.println("Area of Triangle : " + t.area());
}
}
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;
}
public void starting()
{
System.out.println("The " + type + " is starting.");
}
public void stopping()
{
System.out.println("The " + type + " is stopping.");
}
public void accelerating()
{
System.out.println("The " + type + " is accelerating.");
}
}
class Car extends Vehicle
{
int passengers;
Car(int speed,String type,int passengers)
953622104068
Nesamanikandan S
{
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;
}
}
class Exam extends Student
{
int Tamil;
int English;
int Maths;
Exam(int rollNumber, String name, String branch,int Tamil,int English,int Maths)
{
super(rollNumber,name,branch);
this.Tamil=Tamil;
this.English=English;
this.Maths=Maths;
}
}
class Result extends Exam
{
int total;
String result;
Result(int rollNumber, String name, String branch, int Tamil, int English, int
Maths)
{
super(rollNumber, name, branch,Tamil,English,Maths);
this.total=Tamil+English+Maths;
this.result=result;
}
public void display()
{
System.out.println("Roll Number: " + rollNumber);
System.out.println("Name: " + name);
System.out.println("Branch: " + branch);
System.out.println("Subject 1 Marks: " +Tamil);
System.out.println("Subject 2 Marks: " +English);
System.out.println("Subject 3 Marks: " +Maths);
System.out.println("Total Marks: " + total);
if(total>=150)
953622104068
Nesamanikandan S
{
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