Write a Java program that has a Polygon interface, which has abstract methods, a
ID: 3541058 • Letter: W
Question
Write a Java program that has a Polygon interface, which has abstract methods, area( ), and perimeter( ). Implement classes for Triangle, and Quadrilateral, which implement this interface, with the obvious meanings for the area( ) and perimeter( ) methods. Also implement classes IsoscelesTriangle, Rectangle, and Square, which have the appropriate inheritance relationships. Finally, write a simple tester class, which allows users to create polygons of the various types, input their geometric dimensions, and then output their area and perimeter.
Explanation / Answer
import java.util.*;
interface Polygon
{
abstract double area();
abstract double perimeter();
}
class Triangle implements Polygon
{
private int a;
private int b;
private int c;
public Triangle()
{
a = 0;
b = 0;
c = 0;
}
public Triangle(int x,int y,int z)
{
a = x;
b = y;
c = z;
}
public double area()
{
double s = (a+b+c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
public double perimeter()
{
return (a+b+c);
}
}
class IsoscelesTriangle extends Triangle
{
public IsoscelesTriangle()
{
super(0,0,0);
}
public IsoscelesTriangle(int x,int y)
{
super(x,x,y);
}
}
class Square implements Polygon
{
private int a;
public Square()
{
a = 0;
}
public Square(int x)
{
a = x;
}
public double area()
{
return a*a;
}
public double perimeter()
{
return 4*a;
}
}
class Rectangle implements Polygon
{
private int a;
private int b;
public Rectangle()
{
a = 0;
b = 0;
}
public Rectangle(int x,int y)
{
a = x;
b = y;
}
public double area()
{
return a*b;
}
public double perimeter()
{
return 2*(a+b);
}
}
public class polygon_tester
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a=0,b=0,c=0;
System.out.println("Enter 3 sides of traingle ");
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
Triangle T1 = new Triangle(a,b,c);
System.out.println("Triangle T1 area given by " + T1.area());
System.out.println("Triangle T1 perimeter given by " + T1.perimeter());
System.out.println("Enter 2 sides of IsoscelesTriangle ");
a = in.nextInt();
b = in.nextInt();
IsoscelesTriangle IT1 = new IsoscelesTriangle(a,b);
System.out.println("IsoscelesTriangle IT1 area given by " + IT1.area());
System.out.println("IsoscelesTriangle IT1 perimeter given by " + IT1.perimeter());
System.out.println("Enter 2 sides of Rectangle ");
a = in.nextInt();
b = in.nextInt();
Rectangle R1 = new Rectangle(a,b);
System.out.println("Rectangle R1 area given by " + R1.area());
System.out.println("Rectangle R1 perimeter given by " + R1.perimeter());
System.out.println("Enter side of Square ");
a = in.nextInt();
Square S1 = new Square(a);
System.out.println("Square S1 area given by " + S1.area());
System.out.println("Square S1 perimeter given by " + S1.perimeter());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.