Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides hav

ID: 3910850 • Letter: #

Question

(Geometry: n-sided regular polygon) In an n-sided regular polygon, all sides have the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). Design a class named RegularPolygon that contains A private int data field named n that defines the number of sides in the polygon with default value 3. A private double data field named side that stores the length of the side with default value 1. A private double data field named x that defines the x-coordinate of the polygon's center with default value 0 A private double data field named y that defines the y-coordinate of the polygon's center with default value 0 A no-arg constructor that creates a regular polygon with default values. A constructor that creates a regular polygon with the specified number of sides and length of side, centered at (0, 0) A constructor that creates a regular polygon with the specified number of sides, length of side, and x- and y-coordinates. -The accessor and mutator methods for all data fields. The method getPerimeter) that returns the perimeter of the polygon - The method getarea() that returns the area of the polygon The formula for computing the area of a regular polygon is: Area -n 2/ (4 tan(pi/n)) Draw the UML diagram for the class and then implement the class. Write a test program that creates three Regul arPolygon objects, created using the no-arg constructor, using RegularPolygon ( 6, 4 ), and using RegularPolygon (10, 4, 5.6, 7.8) For each object, display its perimeter and area. SAMPLE RUN Polygon 1 perimeter: 3.0

Explanation / Answer


ClassName
|RegualrPolygon
|-------------------------------------------------------------------
Attributes
|private int n
|private double side
|private double x
|private double y
|-------------------------------------------------------------------
Methods and constructors
|public RegualrPolygon(int n, double side)
|public RegualrPolygon(int n, double side,double x,double y)
|public getn()
|public setn(int n)
|public getside()
|public setside(double side)
|public getx()
|public setx(double x)
|public gety()
|public sety(double y)
|public double getArea()
|public double getPerimeter()
---------------------------------------------------------------------

///////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.lang.*;
public class RegularPolygon{
   private int n;
   private double side;
   private double x;
   private double y;

   // RegularPolygon(){
   //    this.setn(1);
   //    this.setside(1);
   //    this.setx(0);
   //    this.sety(0);
   // }

   RegularPolygon(int n, double side){
       this.setn(n);
       this.setside(side);
       this.setx(0);
       this.sety(0);
   }
   RegularPolygon(int n, double side,double x,double y){
       this.setn(n);
       this.setside(side);
       this.setx(x);
       this.sety(y);
   }

   public double getPerimeter(){
       return (this.getn()*this.getside());
   }

   public double getArea(){
      
       // System.out.println(((this.getn()*(this.getside()*this.getside()))/(4*Math.tan(Math.PI/this.getn()))));
       return ((this.getn()*(this.getside()*this.getside()))/(4*Math.tan(Math.PI/this.getn())));
   }

   public int getn(){
       return this.n;
   }
   public void setn(int n){
       this.n=n;
   }
   public double getside(){
       return this.side;
   }
   public void setside(double side){
       this.side=side;
   }
   public double getx(){
       return this.x;
   }
   public void setx(double x){
       this.x=x;
   }
   public double gety(){
       return this.y;
   }
   public void sety(double y){
       this.y=y;
   }
  

   public static void main(String[] args) {
       RegularPolygon p1=new RegularPolygon(3,4.0);
       System.out.println(p1.getArea());
       System.out.println(p1.getPerimeter());
       RegularPolygon p2=new RegularPolygon(4,5.0,1,2);
       System.out.println(p2.getArea());
       System.out.println(p2.getPerimeter());


   }
}