In: Computer Science
In java, implement a program that returns a value of the area of
the geometric objects listed below.
Use the technique of method overloading concept and also uses the
interfaces for dynamic method
invocation.
Triangle: √?(? − ?)(? − ?)(? − ?)
where s=(a+b+c)/2
a,b,c are the sides of the
triangle.
Square: A^2 where A is the side
Rectangle: (a*b) where a ,b are sides
Circle: ? = ??2 where r is the radius
Cube: 6a^2 where a is the side
AreaInterface.java (Interface)
public interface AreaInterface {
public double getArea();
}
Triangle.java
public class Triangle implements AreaInterface{
private double side1, side2, side3;
public Triangle()
{
this.side1 = 0;
this.side2 = 0;
this.side3 = 0;
}
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double getSide1() {
return side1;
}
public double getSide2() {
return side2;
}
public double getSide3() {
return side3;
}
@Override
public double getArea() {
double s = (side1 + side2 + side3) / 2;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s -
side3));
return area;
}
}
Square.java
public class Square implements AreaInterface{
private double side;
public Square()
{
this.side = 0;
}
public Square(double side)
{
this.side = side;
}
public double getSide(){ return this.side; }
@Override
public double getArea() {
return (side * side);
}
}
Rectangle.java
public class Rectangle implements AreaInterface{
private double width, height;
public Rectangle()
{
this.width = 0;
this.height = 0;
}
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
public double getWidth() {
return width;
}
public double getHeight() {
return height;
}
@Override
public double getArea() {
return (width * height);
}
}
Circle.java
public class Circle implements AreaInterface{
private double radius;
public Circle()
{
this.radius = 0;
}
public Circle(double radius)
{
this.radius = radius;
}
public double getRadius(){ return this.radius; }
@Override
public double getArea() {
return(Math.PI * Math.pow(radius, 2));
}
}
Cube.java
public class Cube implements AreaInterface{
private double side;
public Cube()
{
this.side = 0;
}
public Cube(double side)
{
this.side = side;
}
public double getSide(){ return this.side;}
@Override
public double getArea() {
return (6 * Math.pow(side, 2));
}
}
ShapesTest.java (Main class)
public class ShapesTest {
public static void main(String[]args)
{
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("Area of triangle: " + String.format("%.2f",
triangle.getArea()));
Square square = new Square(4);
System.out.println("Area of sqaure: " + String.format("%.2f",
square.getArea()));
Rectangle rectangle = new Rectangle(4, 6);
System.out.println("Area of rectangle: " + String.format("%.2f",
rectangle.getArea()));
Circle circle = new Circle(5);
System.out.println("Area of circle: " + String.format("%.2f",
circle.getArea()));
Cube cube = new Cube(7);
System.out.println("Area of cube: " + String.format("%.2f",
cube.getArea()));
}
}
Note: Here AreaInterface is the interface class used having only one public function "getArea()" which is implemented in each of the class (except the main class). This is where the concept of Interface comes. Also, the same method (getArea) is used throughout the classes but different classes have different implementation (having different set of parameters) of the method. This is where the concept of method overloading (same method name, but different implementation) comes.
***************************************************************** SCREENSHOT **********************************************************