×
FREE ASSISTANCE FOR THE INQUISITIVE PEOPLE
Tutorial Topics
X
softetechnologies
JAVA Lab Assignment - 2017 Java Package Examples
Java Inheritance Programs - Java
27456    Arnab De    01/08/2017

In This post, i describe some inheritance related program. Inheritance is also known as parent-child relationship or IS-A relationship. Protected member of the super class acts as a public member in the child class. All super class constructor is call before executing the child class constructor and super keyword is used to access the super class version of methods and veriables.

softetechnologies

Write an inheritance hierarchy for classes Quadrilateral, Trapezoid, Parallelogram, Rectangle and Square. Use Quadrilateral as the super class of the hierarchy. The instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. Write a program that instantiates objects of your classes and outputs each object's area (except Quadrilateral).

Diagram Of The Program
Diagram Of The Program

public class Quadrilateral
{
protected int x1,x2,x3,x4,y1,y2,y3,y4;
protected void setCoordinate(int a,int b,int c,int d,int e,int f,int g,int h)
{
x1=a;
y1=b;
x2=c;
y2=d;
x3=e;
y3=f;
x4=g;
y4=h;
}
}

public class Square extends Quadrilateral
{
Square(int a,int b,int c,int d,int e,int f,int g,int h)
{
setCoordinate(a,b,c,d,e,f,g,h);
}
int area()
{
int d=(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return d*d;
}
}

public class Rectangle extends Quadrilateral
{
Rectangle(int a,int b,int c,int d,int e,int f,int g,int h)
{
setCoordinate(a,b,c,d,e,f,g,h);
}
int area()
{
int d1=(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
int d2=(int)Math.sqrt((x1-x4)*(x1-x4)+(y1-y4)*(y1-y4));
return d1*d2;
}
}

softetechnologies

public class Trapezoid extends Quadrilateral
{
private int height;
Trapezoid(int a,int b,int c,int d,int e,int f,int g,int h,int height)
{
setCoordinate(a,b,c,d,e,f,g,h);
this.height=height;
}
int area()
{
int d1=(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
int d2=(int)Math.sqrt((x3-x4)*(x3-x4)+(y3-y4)*(y3-y4));
return (int)((d1+d2)*height)/2;
}
}

public class Parallelogram extends Quadrilateral
{
private int height;
Parallelogram(int a,int b,int c,int d,int e,int f,int g,int h,int height)
{
setCoordinate(a,b,c,d,e,f,g,h);
this.height=height;
}
int area()
{
int d1=(int)Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return d1*height;
}
}

softetechnologies


public class TestQuadrilateral
{
public static void main(String []args)
{
Square sq=new Square(10,10,20,10,20,20,10,20);
System.out.println("Area Of The Square is " + sq.area());
Rectangle rec=new Rectangle(10,10,30,10,30,20,10,20);
System.out.println("Area Of The Rectangle is " + rec.area());
Parallelogram p=new Parallelogram(10,10,30,10,20,20,0,20,8);
System.out.println("Area Of The Parallelogram is " + p.area());
Trapezoid t=new Trapezoid(10,10,30,10,40,20,0,20,8);
System.out.println("Area Of The Trapezoid is " + t.area());
}
}

softetechnologies

Output:

Area Of The Square is 100
Area Of The Rectangle is 200
Area Of The Parallelogram is 160
Area Of The Trapezoid is 240

JAVA Lab Assignment - 2017 Java Package Examples
softetechnologies
Author Details
Arnab De
I have over 16 years of experience working as an IT professional, ranging from teaching at my own institute to being a computer faculty at different leading institute across Kolkata. I also work as a web developer and designer, having worked for renowned companies and brand. Through tutorialathome, I wish to share my years of knowledge with the readers.
Enter New Comment
Comment History
No Comment Found Yet.
Albert Einstein
I have no special talent. I am only passionately curious.
Albert Einstein
1832
57.19
Today So Far
Total View (Lakh)
softetechnologies
26/05/2018     43517
01/01/2018     36472
25/06/2018     35466
28/06/2017     34546
02/08/2017     32969
01/08/2017     27456
06/07/2017     27201
15/05/2017     26835
14/07/2017     22453
21/04/2018     21105
softetechnologies