Ex
Ex
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)
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
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;
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);
}
}
this.designation=designation;
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);
}}
Output:
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";
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(){
}
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");
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.