Classes and Objects: Geometry: n-sided regular polygon

Question: 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:
o A private int data field named n that defines the number of sides in the polygon with default value 3 .
o A private double data field named side that stores the length of the side with default value 1 .
o A private double data field named x that defines the x-coordinate of the polygon’s center with default value 0 .
o A private double data field named y that defines the y-coordinate of the polygon’s center with default value 0 .
o A no-arg constructor that creates a regular polygon with default values.
o A constructor that creates a regular polygon with the specified number
of sides and length of side, centered at ( 0 , 0 ).
o A constructor that creates a regular polygon with the specified number
of sides, length of side, and x- and y-coordinates.
o The accessor and mutator methods for all data fields.o The method getPerimeter() that returns the perimeter of the polygon.
o The method getArea() that returns the area of the polygon. The formula for computing the area of a regular polygon is
Area=(nside^2)/[4tan (Pi/n)].
Draw the UML diagram for the class then implement the class. Write a test program that creates three RegularPolygon 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.

package Ch9;

/**
 * ********************** RegularPolygon **********************
 *
 * -n: int
 * -side: double
 * -x: double
 * -y: double
 *
 * __________________________________
 *
 * +RegularPolygon()
 * +RegularPolygon(int: n, double side)
 * +RegularPolygon(int: n, double: side, int: x, int: y)
 * +getN(): int
 * +setN(int: n)
 * +getSide(): double
 * +setSide(double: side)
 * +getX(): double
 * +setX(double: x)
 * +getY(): double
 * +setY(double: y)
 * getPerimeter(): double
 * getArea(): double
 *
 * *************************************************************
 *
 * Created by aarushi on 23/6/21.
 */
public class Ex09RegularPolygon {

    private int n; //number of sides of polygon
    private double side; //side length of polygon
    private double x; //x-coordinate of center of polygon
    private double y; //y-coordinate of center of polygon

    /*A no-arg constructor that creates a regular polygon with default values*/
    public Ex09RegularPolygon(){
        n=3;
        side=1;
        x=0;
        y=0;
    }

    /*A constructor that creates a regular polygon with the specified number of sides and length of side, centered at ( 0 , 0 ).*/
    public Ex09RegularPolygon(int n, double side){
        this.n=n;
        this.side= side;
        x=0;
        y=0;
    }

    /*A constructor that creates a regular polygon with the specified number of sides, length of side, and x- and y-coordinates.*/
    public Ex09RegularPolygon(int n, double side, double x, double y){
        this.n=n;
        this.side=side;
        this.x=x;
        this.y=y;
    }

    /*The accessor and mutator methods for all data fields.*/
    public int getN(){
        return n;
    }

    public void setN(int n){
        this.n=n;
    }

    public double getSide(){
        return side;
    }

    public void setSide(double side){
        this.side=side;
    }

    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    /*The method getPerimeter() that returns the perimeter of the polygon*/
    public double getPerimeter(){
        return n*side;
    }

    /*The method getArea() that returns the area of the polygon. The formula for computing the area of a regular polygon*/
    public double getArea(){
        return (n*Math.pow(side, 2))/(4*Math.tan(Math.PI/n));
    }
}
package Ch9;

/**
 * Q: 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:
     o A private int data field named n that defines the number of sides in
     the polygon with default value 3 .
     o A private double data field named side that stores the length of the
     side with default value 1 .
     o A private double data field named x that defines the x-coordinate of
     the polygon's center with default value 0 .
     o A private double data field named y that defines the y-coordinate of
     the polygon's center with default value 0 .
     o A no-arg constructor that creates a regular polygon with default values.
     o A constructor that creates a regular polygon with the specified number
     of sides and length of side, centered at ( 0 , 0 ).
     o A constructor that creates a regular polygon with the specified number
     of sides, length of side, and x- and y-coordinates.
     o The accessor and mutator methods for all data fields.o The method getPerimeter() that returns the perimeter of the polygon.
     o The method getArea() that returns the area of the polygon. The
     formula for computing the area of a regular polygon is
     Area=(n*side^2)/[4*tan (Pi/n)].
     Draw the UML diagram for the class then implement the class. Write a test
     program that creates three RegularPolygon 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.
 * Created by aarushi on 23/6/21.
 */
public class Ex09RegularPolygonTest {
    public static void main(String[] args){
        Ex09RegularPolygon polygon1= new Ex09RegularPolygon();
        Ex09RegularPolygon polygon2= new Ex09RegularPolygon(6,4);
        Ex09RegularPolygon polygon3= new Ex09RegularPolygon(10, 4, 5.6, 7.8);
        System.out.printf("Perimeter of polygon 1: %.3f", polygon1.getPerimeter());
        System.out.printf("\nArea of polygon 1: %.3f", polygon1.getArea());
        System.out.printf("\nPerimeter of polygon 2: %.3f", polygon2.getPerimeter());
        System.out.printf("\nArea of polygon 2: %.3f", polygon2.getArea());
        System.out.printf("\nPerimeter of polygon 3: %.3f", polygon3.getPerimeter());
        System.out.printf("\nArea of polygon 3: %.3f", polygon3.getArea());
    }
}

/*
    Output:
    Perimeter of polygon 1: 3.000
    Area of polygon 1: 0.433
    Perimeter of polygon 2: 24.000
    Area of polygon 2: 41.569
    Perimeter of polygon 3: 40.000
    Area of polygon 3: 123.107
 */

Leave a Reply

PHP JS HTML CSS BASH PYTHON CODE

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

search previous next tag category expand menu location phone mail time cart zoom edit close