0% found this document useful (0 votes)
49 views5 pages

MITS4002 Activity 09

The document discusses object-oriented software development using inheritance in Java. It defines a base Shape class with subclasses for specific shapes like Square, Rectangle, Circle, etc. Each subclass inherits attributes and methods from the base class and overrides methods like findArea() and findPerimeter() to provide unique definitions for that shape. The code provided implements the Shape class hierarchy with subclasses calculating and printing the area and perimeter for each shape.

Uploaded by

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

MITS4002 Activity 09

The document discusses object-oriented software development using inheritance in Java. It defines a base Shape class with subclasses for specific shapes like Square, Rectangle, Circle, etc. Each subclass inherits attributes and methods from the base class and overrides methods like findArea() and findPerimeter() to provide unique definitions for that shape. The code provided implements the Shape class hierarchy with subclasses calculating and printing the area and perimeter for each shape.

Uploaded by

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

MITS4002

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);
}
}

Screenshots of your output and testing:

You might also like