MITS4002 Activity 09
MITS4002 Activity 09
OBJECT-ORIENTED SOFTWARE
DEVELOPMENT
Analysis/Design/UML:
By developing the base class Shape and its child classes, such as Square, Rectangle,
Circle, Triangle, and Trapezium, the major goal is to implement the concept of
inheritance. The base class Shape's attribute and methods are passed down to the
subclasses. Following the creation of the array of forms using the base class, the area and
perimeter of each shape are computed and presented.
Here, three methods—for figuring both area and perimeter—are crucially needed. For
each sub class, these methods have a unique definition.
getShapeColor()
findArea()
findPerimeter()
Coding:
1
public class Shape {
String shapeColor;
Shape(String shapeColor)
{
this.shapeColor = shapeColor;
}
Shape()
{
this.shapeColor="";
}
void getShapeColor()
{
}
void findArea()
{
}
void findPerimeter()
{
}
}
public class Square extends Shape{
double sideOfSquare;
Square(double sideOfSquare,String shapeColor)
{
this.sideOfSquare=sideOfSquare;
this.shapeColor=shapeColor;
}
void setSideOfSquare()
{
this.sideOfSquare=sideOfSquare;
}
double getSideOfSquare()
{
return sideOfSquare;
}
void getShapeColor()
{
System.out.println("The Color of Square is "+this.shapeColor);
}
void findArea()
{
System.out.println("Area of Square is
"+sideOfSquare*sideOfSquare);
}
void findPerimeter()
{
System.out.println("Perimeter of Square is "+4*sideOfSquare);
}
}
public class Rectangle extends Shape{
double lengthOfRect;
double breadthOfRect;
Rectangle(double lengthOfRect,double breadthOfRect,String
shapeColor)
{
this.lengthOfRect=lengthOfRect;
this.breadthOfRect=breadthOfRect;
2
this.shapeColor=shapeColor;
}
double getLengthOfRect()
{
return lengthOfRect;
}
double getBreadthOfRect()
{
return breadthOfRect;
}
void setLengthOfRect(double lengthOfRect)
{
this.lengthOfRect=lengthOfRect;
}
void setBreadthOfRect()
{
this.breadthOfRect=breadthOfRect;
}
void getShapeColor()
{
System.out.println("The Color of Rectangle is "+this.shapeColor);
}
void findArea()
{
System.out.println("Area of Rectangle is "+lengthOfRect *
breadthOfRect);
}
void findPerimeter()
{
System.out.println("Perimeter of Rectangle is "+
2*(lengthOfRect+breadthOfRect));
}
}
public class Circle extends Shape{
double radiusOfCircle;
Circle(double radiusOfCircle,String shapeColor)
{
this.radiusOfCircle=radiusOfCircle;
this.shapeColor = shapeColor;
}
void setCircleRadius(double radiusOfCircle)
{
this.radiusOfCircle=radiusOfCircle;
}
double getCircleRadius()
{
return this.radiusOfCircle;
}
void getShapeColor()
{
System.out.println("The Color of Circle is "+this.shapeColor);
}
void findArea()
{
System.out.println("Area of Circle is
"+Math.PI*radiusOfCircle*radiusOfCircle);
}
void findPerimeter()
3
{
System.out.println("Perimeter of Circle is
"+2*Math.PI*radiusOfCircle);
}
}
public class Trapezium extends Shape{
double sideOne,sideTwo,sideThree,sideFour,heightOfTrapezium;
Trapezium(double sideOne,double sideTwo,double sideThree,double
sideFour,double heightOfTrapezium,String shapeColor)
{
this.sideOne = sideOne;
this.sideTwo = sideTwo;
this.sideThree = sideThree;
this.sideFour = sideFour;
this.heightOfTrapezium = heightOfTrapezium;
this.shapeColor = shapeColor;
}
void getShapeColor()
{
System.out.println("The Color of Trapezium is "+this.shapeColor);
}
void findArea()
{
System.out.println("Area of Trapezium is
"+0.5*(sideOne+sideTwo)*heightOfTrapezium);
}
void findPerimeter()
{
System.out.println("Perimeter of Trapezium is "+
sideOne+sideTwo+sideThree+sideFour);
}
}
public class EquilateralTriangle extends Shape {
double sideOfTriangle;
EquilateralTriangle(double sideOfTriangle,String shapeColor)
{
this.sideOfTriangle=sideOfTriangle;
this.shapeColor=shapeColor;
}
double getSideOfTriangle()
{
return sideOfTriangle;
}
void setSideOfTriangle(double sideOfTriangle)
{
this.sideOfTriangle=sideOfTriangle;
}
void getShapeColor()
{
System.out.println("The Color of Equilateral Triangle is
"+this.shapeColor);
}
void findArea()
{
System.out.println("Area of Equilateral Triangle is "+
(Math.sqrt(3)/4)*sideOfTriangle*sideOfTriangle);
}
void findPerimeter()
{
4
System.out.println("Perimeter of Equilateral Triangle is
"+3*sideOfTriangle);
}
}