You are supposed to calculate the area of a polygon based on number of inputs given bythe user. Polygon can be a square, a rectangle or a triangle.
/* Area of a polygon based on number of inputs given by the user */
import java.io.*;
import java.util.*;
class Polygon
{
void area(double a)
{
System.out.println("Area of Square = " + a*a);
}
void area(double l,double b)
{
System.out.println("Area of Rectangle = " + l*b);
}
void area(double a,double b,double c)
{
double s = (a+b+c)/2;
double d = s*(s-a)*(s-b)*(s-c);
System.out.println("Area of Triangle = " + String.format("%.2f",Math.sqrt(d)));
}
}
class PolygonArea_4B
{
public static void main(String args[])
{
Polygon p = new Polygon();
Scanner s = new Scanner(System.in);
System.out.print("Enter side of a square : ");
double a = s.nextDouble();
p.area(a);
System.out.print("Enter sides of a rectangle : ");
double l = s.nextDouble();
double b = s.nextDouble();
p.area(l,b);
System.out.print("Enter three sides of a triangle : ");
double x = s.nextDouble();
double y = s.nextDouble();
double z = s.nextDouble();
p.area(x,y,z);
}
}
Comments
Post a Comment