Question

(Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all...

(Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all of its 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. ■ A private float data field named side that stores the length of the side.

■ A private float data field named x that defines the x-coordinate of the center of the polygon with default value 0. 238 Chapter 7

Objects and Classes

■ A private float data field named y that defines the y-coordinate of the center of the polygon with default value 0.

■ A constructor that creates a regular polygon with the specified n (default 3),

side (default 1), x (default 0), and y (default 0).

■ 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

n * s 2 computing the area of a regular polygon is Area = .

p 4 * tan¢ ≤ n Draw the UML diagram for the class, and then implement the class. Write a test program that creates three RegularPolygon objects, created using RegularPolygon(), using RegularPolygon(6, 4) and RegularPolygon(10, 4, 5.6, 7.8). For each object, display its perimeter and area.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

UML Class Diagram:

​​​​​​

Python Program:

'''
Python program that computes area and perimeter of a n-sided regular polygon
'''

import math;

class RegularPolygon:

def __init__(self, n=3, side=1, x=0, y=0):
'''
Constructor that assigns values to private variables
'''
self.__n = n;
self.__side = side;
self.__x = x;
self.__y = y;

def getN(self):
'''
Accessor method for getting value of n
'''
return self.__n;

def getSide(self):
'''
Accessor method for getting value of side
'''
return self.__side;

def getX(self):
'''
Accessor method for getting value of x
'''
return self.__x;

def getY(self):
'''
Accessor method for getting value of y
'''
return self.__y;

def setN(self, n):
'''
Mutator method for setting value of n
'''
self.__n = n;

def setSide(self, side):
'''
Mutator method for setting value of side
'''
self.__side = side;

def setX(self, x):
'''
Mutator method for setting value of x
'''
self.__x = x;

def setY(self, y):
'''
Mutator method for setting value of y
'''
self.__y = y;

def getArea(self):
'''
Method that calculates and returns the area of polygon
'''
return (self.__n pow(self.__side,2)) / (4 math.tan(math.pi/self.__n));

def getPerimeter(self):
'''
Method that calculates and returns the perimeter of polygon
'''
return self.__n * self.__side;


#Test Program

#Polygon object 1
polygon1 = RegularPolygon();
print("\n\n Polygon 1: \n\n Area: " + str(polygon1.getArea()) + " \n Perimeter: " + str(polygon1.getPerimeter()) + " \n");

#Polygon object 2
polygon2 = RegularPolygon(6, 4);
print("\n\n Polygon 2: \n\n Area: " + str(polygon2.getArea()) + " \n Perimeter: " + str(polygon2.getPerimeter()) + " \n");

#Polygon object 3
polygon3 = RegularPolygon(10, 4, 5.6, 7.8);
print("\n\n Polygon 3: \n\n Area: " + str(polygon3.getArea()) + " \n Perimeter: " + str(polygon3.getPerimeter()) + " \n");

Sample Output:

Kindly Upvote if Helpful :
HOPE THIS MAY HELP YOU----------------------

------------------------------THANK YOU---------------------------

Add a comment
Know the answer?
Add Answer to:
(Geometry: n-sided regular polygon) An n-sided regular polygon’s sides all have the same length and all...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of the same length...

    //include comments Opts (Regular polygon) An n-sided regular polygon has n sides of 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: Aprivate 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...

  • Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on...

    Problem 2 9.9 Geometry: n-sided regular polygon pg. 362 (25 points) Follow instructions as provided on page 362, reprinted below 9.9 (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 of 3 A...

  • A regular polygon is an n-sided polygon in which all sides are of the same length...

    A regular polygon is an n-sided polygon in which all sides are of the same length and all angles have the same degree (i.e., the polygon is both equilateral and equiangular). The formula for computing the area of a regular polygon isArea =Here, s is the length of a side. Write a program that prompts the user to enter the number of sides and their length of a regular polygon and displays its area. Here is a sample run:

  • N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and...

    N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and whose angles have the same degree. Write a class called NSidedPolygon that contains the following components: Private members to store the name of a polygon, the number of sides, and the length of each side. (You are expected to select the appropriate data types for each member.) A constructor that accepts the number of sides and the length of each side. A private method...

  • Write a program that displays a convex regular polygon with m or n sides, where m...

    Write a program that displays a convex regular polygon with m or n sides, where m is the last digit of your student number plus 3, n is the second last digit plus 3. Use two buttons named “before” and “after” to change the number of sides of the polygon. Click on “before” button will show a convex regular polygon with m sides and click on “after” will show a convex regular polygon with n sides. For example, a student...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

  • Introduction to Java:Design a class named Triangle that extends GeometricObject.

    Please include comments in the program .The program must be able to be compiled.Design a class named Triangle that extends GeometricObject. This class contains:* Three double data fields named side1, side2, and side3 with default values 1.0 to denote the three sides of a triangle.* A no-arg constructor that creates a default triangle.* A constructor that creates a triangle with the specified side1, side2, and side3.* The accessor methods for all three data fields.* A method named getArea() that returns...

  • NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

    NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...

  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT