Edhesive unit 2 lesson 5 coding activity 1 Write code which creates three regular polygons with 11, 14 and 19 sides respectively. All side lengths should be 1.0. The code should then print the three shapes, one on each line, in the order given (i.E. The one with 11 sides first and the one with 19 sides last). Sample run: regular hendecagon with side length 1.0 regular tetrakaidecagon with side length 1.0 regular enneadecagon with side length 1.0

Respuesta :

Answer:

public class Polygon {

   private String name;

   private int sides;

   private double sideLength;

   public Polygon(String name, int sides, double sideLength) {

       if (sideLength <= 0) throw new IllegalArgumentException("Length cannot be zero or negative.");

       if (sides <= 0) throw new IllegalArgumentException("Sides cannot be zero or negative.");

       this.name = name;

       this.sides = sides;

       this.sideLength = sideLength;

   }

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public double getSideLength() {

       return sideLength;

   }

   public void setSideLength(double sideLength) {

       if (sideLength <= 0) throw new IllegalArgumentException("Length cannot be zero or negative.");

       this.sideLength = sideLength;

   }

   public int getSides() {

       return sides;

   }

   public void setSides(int sides) {

       this.sides = sides;

   }

   (use the at sign here)Override

   public String toString() {

       return "regular " + name + " with side length " + String.format("%.1f", sideLength);

   }

}

public class TestPolygon {

   public static void main(String[] args) {

       Polygon sides11 = new Polygon("hendecagon", 11, 1);

       Polygon sides14 = new Polygon("tetrakaidecagon", 14, 1);

       Polygon sides19 = new Polygon("enneadecagon", 19, 1);

       System. out. println(sides11);

       System. out. println(sides14);

       System. out. println(sides19);

   }

}

Explanation:

This java source code defines a class that creates a regular polygon based on the number of sides given to it.

Below is a screenshot of the program code and output.

Ver imagen IfeanyiEze8899