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

Popular posts from this blog

Design GUI to handle Choice Control event.

Write a Java program that reads an integer number (between 1 and 255) from the user and prints the binary representation of the number. The answer should be printed as a String. Note: The output displayed should contain 8 digits and should be padded with leading 0s(zeros), in case the returned String contains less than 8 characters. For example, if the user enters the value 16, then the output should be 00010000 and if the user enters the value 100, the output should be 01100100 (Hint : You may use String.format() method for the expected output)