0% found this document useful (0 votes)
18 views9 pages

Ex

The document describes a class hierarchy for Person, Student, and Faculty classes. It includes: - A class diagram showing the inheritance relationships between the classes - Constructors defined at each level of the hierarchy - Getter and setter methods to access attributes - A method to calculate salary for the Faculty class - A TestInheritance class with a main method to test the classes The class hierarchy allows representing personal details with the Person class, with Student and Faculty classes extending it to include education/employment specific details. Methods are provided to set/get attributes and calculate values like GPA and salary consistently across the hierarchy.

Uploaded by

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

Ex

The document describes a class hierarchy for Person, Student, and Faculty classes. It includes: - A class diagram showing the inheritance relationships between the classes - Constructors defined at each level of the hierarchy - Getter and setter methods to access attributes - A method to calculate salary for the Faculty class - A TestInheritance class with a main method to test the classes The class hierarchy allows representing personal details with the Person class, with Student and Faculty classes extending it to include education/employment specific details. Methods are provided to set/get attributes and calculate values like GPA and salary consistently across the hierarchy.

Uploaded by

harikumar2210451
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Name: G Harikumar Date: 05.10.

2023
Reg no: 3122225001033

Inheritance

Question No.:01
Create a class hierarchy for the classes defined be-
low. The notations to denote the access specifiers for the at-
tributes and methods are as follows:
- Private members
+ Public members
# Protected members
~ Default (Package private)

Note the following:


● Person -> Student (or) Person -> Faculty is a single-level
inheritance.
● The type of the entire class hierarchy (Person -> Student ,
Person -> Faculty)
is hierarchical Inheritance.
● Note the use of constructors at all levels of class hierar-
chy.

Things to do:
● Draw the class diagram of the above class hierarchy.
● Write a test driver called TestInheritance to test all the
public methods
that display the student and the faculty details.
● Use the following to calculate the Net Salary of a faculty:
Gross salary = Basic pay + DA as 60% of basic + HRA as 10% of
basic
Deductions = Medical Insurance as 8.5% of basic + PF as 8% of
basic
Net salary = Gross salary – Deductions

Department of Computer Science and Engineering


Name: G Harikumar Date: 05.10.2023
Reg no: 3122225001033

Class Diagram:

Person
-name:String
-address:String
-gender:char
-aadhaar:int
+Person(aadhaar,name,ad-
dress,gender)
+getName():String
+getAddress():String
+setAddress(address):void
+getGender():char

Student Faculty
-program:String -designation:String
-year:int -department:String
-totalmark:float -basicpay:float
+Student(aadhaar,name,ad- +Faculty(aadhaar,name,ad-
dress,gender,program,year,to- dress,gender,designa-
tal) tion,dept,pay)
+getProgram():String +getDesig():String
+getYear():int +setDesig(desig):void
+setYear(year):void +setBasic(basic):void
+getTotal():float +getBasic():float
+setTotal(tot):void +calSalary():float
+calGPA():float

SourceCode:

import java.util.*;
class Person{
private int aadhar;
private String name;
private String address;
private char gender;

//Scanner in=new Scanner(System.in);


public Person(int aadhar,String name,String address,char gender)
{
this.aadhar=aadhar;
this.name=name;
this.address=address;

Department of Computer Science and Engineering


Name: G Harikumar Date: 05.10.2023
Reg no: 3122225001033

this.gender=gender;
}
public String getName(){
return(this.name);
}
public String getAddress(){
return(this.address);
}
public void setAddress(String address){
this.address=address;
}
public char getGender(){
return(this.gender);
}
}

class Student extends Person{


private String program;
private int year;
private float totalmark;

public Student(int aadhar,String name,String address,char gender,String


program,int year,int total){
super(aadhar,name,address,gender);
this.program=program;
this.year=year;
this.totalmark=total;
}
public String getProgram(){
return this.program;
}
public int getYear(){
return this.year;
}
public void setYear(int year){
this.year=year;
}
public float getTotal(){
return this.totalmark;
}
public void setTotal(int total){
this.totalmark=total;
}
public float calGPA(){
Scanner in=new Scanner(System.in);
System.out.print("Enter the total marks:");
int tot=in.nextInt();
return((this.totalmark/tot)*10);
}
}

class Faculty extends Person{


private String designation;
private String department;
private float basicpay;

public Faculty(int aadhar,String name,String address,char gender,String


designation,String dept,int pay)
{ super(aadhar,name,address,gender);

this.designation=designation;

Department of Computer Science and Engineering


Name: G Harikumar Date: 05.10.2023
Reg no: 3122225001033

this.department=dept;
this.basicpay=pay;
}
public String getDesig(){
return this.designation;
}
public void setDesig(String desig){
this.designation=desig;
}
public void setBasic(float basic){
this.basicpay=basic;
}
public float getBasic(){
return this.basicpay;
}
public float calSalary(){
float gross=this.basicpay+(this.basicpay*0.6f)+(this.basicpay*0.1f);
float deduction=(this.basicpay*0.085f)+(this.basicpay*0.08f);
float netsal=gross-deduction;
return netsal;
}

class TestInheritance{
public static void main(String[] args){
Student st=new Student(6836,"hari","dharmapuri",'m',"cse",2022,85);
Faculty fc=new Faculty( 5174,"paul","chennai",'m',"professor","bme",60000);
Scanner in=new Scanner(System.in);

System.out.println("testing Student class");


String name=st.getName();
System.out.println(name);
String address=st.getAddress();
System.out.println(address);
System.out.print("Enter the new address:");
String newaddress=in.next();
st.setAddress(newaddress);
address=st.getAddress();
System.out.println(address);
char gender=st.getGender();
System.out.println(gender);
String prog=st.getProgram();
System.out.println(prog);
int year=st.getYear();
System.out.println(year);
System.out.print("Enter the new year:");
int newyear=in.nextInt();
st.setYear(newyear);
year=st.getYear();
System.out.println(year);
float tot=st.getTotal();
System.out.println(tot);
System.out.print("enter the new total:");
int newtot=in.nextInt();
st.setTotal(newtot);
tot=st.getTotal();
System.out.println(tot);
float gpa=st.calGPA();
System.out.println("the GPA is"+gpa);

Department of Computer Science and Engineering


Name: G Harikumar Date: 05.10.2023
Reg no: 3122225001033

System.out.println("testing Faculty class");


String name1=fc.getName();
System.out.println(name1);
String address1=fc.getAddress();
System.out.println(address1);
System.out.print("Enter the new address:");
String newaddress1=in.next();
fc.setAddress(newaddress1);
address1=fc.getAddress();
System.out.println(address1);
char gender1=fc.getGender();
System.out.println(gender1);
String desig=fc.getDesig();
System.out.println(desig);
System.out.print("enter the new designation:");
String newdesig=in.next();
fc.setDesig(newdesig);
desig=fc.getDesig();
System.out.println(desig);
float basic=fc.getBasic();
System.out.println(basic);
System.out.print("enter the new basic pay:");
float newbasic=in.nextInt();
fc.setBasic(newbasic);
basic=fc.getBasic();
System.out.println(basic);
float salary=fc.calSalary();
System.out.println("the salary is "+salary);

}}

Output:

Department of Computer Science and Engineering


Name: G Harikumar Date: 05.10.2023
Reg no: 3122225001033

Question No.:02
Create a class hierarchy for the classes/interface as defined below:

Class Diagram:

Shape
#color:String=”red”
+Shape()
+Shape(color)
+getColor():String
+setColor(color):void

Circle Rectangle
#radius:float=1.0 #width:float=1.0
#length:float=1.0
+Circle()
+Rectangle()
+Circle(radius)
+Rectangle(width,length)
+Circle(radius,color)
+Rectangle(width,length,color)
+getRadius():float
+getWidth():float
+setRadius(radius):void
+setWidth(width):void
+getArea():float
+getLength():float
+getPerimeter():float
+setLength(length):void
+getArea():float
+getPerimeter():float

Square
+Square()
+Square(side)
+Square(side,color)
+getSide():float
+setSide(side):void

SourceCode:

package unit1;
import java.util.*;
class Shape{
protected String colour="red";

Department of Computer Science and Engineering


Name: G Harikumar Date: 05.10.2023
Reg no: 3122225001033

Shape(){
this.colour="";
};
Shape(String colour){
this.colour=colour;
}
String getcolour() {
return this.colour; }
void setcolour(String colour) {
this.colour=colour;
}
float getArea()
{
return 0; }
float getPerimeter()
{
return 0;
}
float getradius() {
return 0;
}
float getlength() {
return 0;
}
float getwidth() {
return 0;
}
float getSide() {return 0;}

}
class Circle extends Shape{
protected float radius=0.1f;
Circle(){

}
Circle(float radius){
this.radius=radius;
}
Circle(float radius,String colour){
super(colour);
this.radius=radius;

}
float getradius() {
return radius;
}
void setradius(float radius) {
this .radius=radius;
}
float getArea() {
return (float)(3.14*(Math.pow(radius, 2)));
}
float getPerimeter() {
return (float)(3.14*2*radius);
}
}
class Rectangle extends Shape{
protected float width=0.1f;
protected float length=0.1f;
Rectangle(){

Department of Computer Science and Engineering


Name: G Harikumar Date: 05.10.2023
Reg no: 3122225001033

}
Rectangle(float width,float length){
this.width=width;
this.length=length;
}
Rectangle(float width,float length,String colour){
super(colour);
this.width=width;
this.length=length;

}
void setwidth(float width) {
this.width=width;
}
void setlength(float length) {
this.length=length;
}
float getwidth() {
return this.width;
}
float getlength() {
return this.length;
}
float getArea() {
return(float)(length*width);
}
float getPerimeter() {
return(float)2*(length+width);
}

}
class Square extends Rectangle{
Square(){

}
Square(float size){
super(size,size);
}
Square(float size,String colour){

super(size,size,colour);
}
float getSide() {
return super.getlength();
}
void setSide(float side) {
super.setlength(side);
}

}
class TestShape{
public static void main(String[] args) {
Shape []ob=new Shape[3];
ob[0]=new Circle(1.0f,"blue");
System.out.print("\n");
System.out.println("radius of circle:"+ob[0].getradius());
System.out.println("Area of circle:"+ob[0].getArea());
System.out.println("Perimeter of circle:"+ob[0].getPerimeter());

ob[1]=new Rectangle(2.0f,3.0f,"yellow");

Department of Computer Science and Engineering


Name: G Harikumar Date: 05.10.2023
Reg no: 3122225001033

System.out.print("\n");
System.out.println("length of rectangle:"+ob[1].getlength());
System.out.println("width of rectangle:"+ob[1].getwidth());
System.out.println("Area of rectangle:"+ob[1].getArea());
System.out.println("Perimeter of rectangle:"+ob[1].getPerimeter());

ob[2]=new Square(2.0f,"green");
System.out.print("\n");
System.out.println("side of square"+ob[2].getSide());
System.out.println("Area of square:"+ob[2].getArea());
System.out.println("Perimeter of square:"+ob[2].getPerimeter());

}
}

Output

Learning Outcomes:
From this exercise, I have learnt inheritance in java.

Department of Computer Science and Engineering

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy