85.5k views
1 vote
Write code which takes inputs from the user for the number of sides (int) and side length (double), then creates a regular polygon with these characteristics. The program should then print a sentence with the name of the shape and the area and perimeter as shown in the sample run. You should use the appropriate RegularPolygon methods to obtain the perimeter and area of the shape rather than attempting to calculate these values yourself.

///////

Sample run:


Enter number of sides:

> 5

Enter side length:

> 3.5

A regular pentagon with side length 3.5 has perimeter 17.5 and area 21.075848157214846

/////////


Hint: You can approach this problem by saving the int and double as variables, and then creating the RegularPolygon, or you can create the polygon and use methods to set the values of the sides and sidelength.


In Java

Write code which takes inputs from the user for the number of sides (int) and side-example-1
User Rksh
by
4.8k points

1 Answer

7 votes

public static void main(String[] args)

{

Scanner scanner = new Scanner(System.in);

int sides = scanner.nextInt();

double length = scanner.nextDouble();

RegularPolygon(sides, length);

}

public static void RegularPolygon(int side, double length)

{

double area = 0;

double perimeter = 0;

String shape;

String sentence;

for(int i = 0; i <= side; i++)

{

perimeter = length*side;

area = side * (length * length) / (4.0 * Math.tan(Math.PI / side));

}

switch (side)

{

case 1:

shape = "Line";

System.out.println("There is no ability for area or perimeter.");

break;

case 2:

shape = "Angle";

System.out.println("There is no ability for area or perimeter.");

break;

case 3:

shape = "Triangle";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

case 4:

shape = "Square";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

case 5:

shape = "Pentagon";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

case 6:

shape = "Hexagon";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

case 7:

shape = "Heptagon";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

case 8:

shape = "Octagon";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

case 9:

shape = "Nonagon";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

case 10:

shape = "Decagon";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

case 11:

shape = "Hendecagon";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

case 12:

shape = "Dodecagon";

sentence= "A regular " + shape +" with a side length of " + length +" has a perimeter " + perimeter + " " + "and an area of " + area;

System.out.println(sentence);

break;

}

}

User Abhishek Gangwar
by
3.9k points