Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write the functions in P ython and also include explanation for each line of code and a snapshot of the code to allow for

Please write the functions in Python and also include explanation for each line of code and a snapshot of the code to allow for viewing of correct indentation, thank you.

Question 2 a):

Design a class named Polygon to represent an n-sided regular polygon. An n-sided regular polygons sides all have the same length and all of its angles have the same degree (i.e. the polygon is both equilateral and equiangular). The Polygon class contains the following:

  • A private data field named __number_of_sides that defines the number of sides in the polygon.
  • A private data field named __side_length that stores the length of the side.
  • A private data field named __x that defines the x-coordinate of the polygons centre.
  • A private data field named __y that defines the y-coordinate of the polygons centre.
  • A constructor/initializer that takes three parameters and creates a regular polygon object (default values: number of sides = 3, side length = 1, x = 0, y = 0).
  • The get_number_of_sides(self) method which returns the number of sides of a polygon.
  • The get_side_length(self) method which returns the side length of a polygon.
  • The get_x(self) method which returns the x-coordinate of the polygons centre.
  • The get_y(self) method which returns the y-coordinate of the polygons centre.
Test Result
polygon1 = Polygon() print(polygon1.get_number_of_sides(), polygon1.get_side_length(), polygon1.get_x(), polygon1.get_y() )
3 1 0 0
polygon4 = Polygon(10, 4, 10, 20) print(polygon4.get_number_of_sides(), polygon4.get_side_length(), polygon4.get_x(), polygon4.get_y() )
10 4 10 20

Question 2 b):

Extend the Polygon class by adding the following methods:

  • The set_number_of_sides(self, number_of_sides) method which takes an integer as a parameter and modifies the number of sides of a polygon. The method should only modify the value if it is valid. (i.e. > 2)
  • The set_side_length(self, side_length) method which takes a number as a parameter and modifies the side length of a polygon. The method should only modify the value if it is valid. (i.e. > 0)
  • The set_x(self, x) method which takes an integer as a parameter and modifies the x-coordinates of the polygons centre. The method should only modify the value if it is valid. (i.e. >= 0)
  • The set_y(self, y) method which takes an integer as a parameter and modifies the y-coordinate of the polygons centre. The method should only modify the value if it is valid. (i.e. >=0)
  • The special __str__ method which returns a string that represents a polygon object as shown in the following examples below.

Test Result
polygon2 = Polygon(6) print(polygon2) polygon2.set_number_of_sides(5) polygon2.set_x(10) polygon2.set_y(20) polygon2.set_side_length(50) print(polygon2)
A polygon at (0, 0) with 6 sides of length 1cm. A polygon at (10, 20) with 5 sides of length 50cm.
polygon2 = Polygon() print(polygon2) polygon2.set_number_of_sides(1) polygon2.set_x(-10) polygon2.set_y(-20) polygon2.set_side_length(0) print(polygon2)
A polygon at (0, 0) with 3 sides of length 1cm. A polygon at (0, 0) with 3 sides of length 1cm.

Question 2 c):

Add the following two methods to your Polygon class to extend its functionality.

  • The get_perimeter(self) method which returns the perimeter of a regular n-sided polygon.
  • The get_area(self) method which returns the area of a regular n-sided polygon. The formula for computing the area of a regular polygon is:
    (n x s^2) / (4 x tan( / n))

where n is the number of sides and s is the side length of a polygon

Test Result
polygon1 = Polygon() print('The perimeter is %0.2f.' % polygon1.get_perimeter()) print('The area is %0.2f.' % polygon1.get_area())
The perimeter is 3.00. The area is 0.43.
polygon4 = Polygon(10, 4, 10, 20) print('Perimeter is %0.2f.' % polygon4.get_perimeter()) print('Area is %0.2f.' % polygon4.get_area())
Perimeter is 40.00. Area is 123.11.

Question 2 d):

Extend the Polygon class by adding the get_exterior_angle_in_degree method. The get_exterior_angle_in_degree(self) method returns the exterior angle of a regular n-sided polygon. An exterior angle 'beta' of a polygon is the angle formed externally between two adjacent sides.

We know that the exterior angles of a regular polygon always add up to 360, so the exterior angle of a regular polygon is:

The measure of each exterior angle of an equiangular n-gon is 360 / n

Test Result
polygon1 = Polygon() print(polygon1.get_exterior_angle_in_degree())
120
polygon4 = Polygon(10, 4, 10, 20) print(polygon4.get_exterior_angle_in_degree())
36

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Fred R. McFadden, Jeffrey Slater, Mary B. Prescott

5th Edition

0805360549, 978-0805360547

More Books

Students also viewed these Databases questions

Question

Question Can a self-employed person adopt a profit sharing plan?

Answered: 1 week ago