135k views
3 votes
Write code that takes an integer input and a double input from the user, and creates a RegularPolygon object where the integer is the number of sides and the double is the side length. The code should calculate the area of the Regularpolygon, print the area, then increment the number of sides of the RegularPolygon by two and print the new area. Sample run: Enter number of sides: →4 Enter the side length: >5.6 Area with 4 sides: 31.36 Incrementing the number of sides by two Area with 6 sides: 81.47566998803998 Hint: You can use either the addisides or setwunsides methods from the RegularPolygon class to increment the number of sides by 2. To reference the documentation for the Circle class, click here et;

User Mezoni
by
8.1k points

1 Answer

5 votes

When you run the code and enter 4 as the number of sides and 5.6 as the side length, the output will be:

```
Area with 4 sides: 31.36
Area with 6 sides: 81.47566998803998
```

To write code that accomplishes the given task, you will need to create a class called "Regular Polygon" with the following methods:

1. `__init__(self, sides, length)`: This method is called a constructor and is used to initialize the object's attributes. In this case, it should take two arguments: `sides` (an integer representing the number of sides) and `length` (a double representing the length of each side). Inside the method, you should store these values as instance variables.

2. `get_area(self)`: This method calculates and returns the area of the regular polygon. The formula for the area of a regular polygon is `area = (sides * length^2) / (4 * tan(pi/sides))`. You can use the `math` module in Python to access the `tan` and `pi` functions.

3. `increment_sides(self)`: This method increments the number of sides of the regular polygon by two. To do this, you can simply add 2 to the `sides` instance variable.

4. `print_area(self)`: This method prints the area of the regular polygon. You can call the `get_area` method to calculate the area and then print it using the `print` function.

Now, you can write the main code that takes user inputs and performs the desired operations:

1. Prompt the user to enter the number of sides using the `input` function. Convert the input to an integer using the `int` function and store it in a variable called `num_sides`.

2. Prompt the user to enter the side length using the `input` function. Convert the input to a float using the `float` function and store it in a variable called `side_length`.

3. Create an instance of the `RegularPolygon` class, passing `num_sides` and `side_length` as arguments.

4. Call the `print_area` method on the created `RegularPolygon` object to print the initial area.

5. Call the `increment_sides` method on the created `RegularPolygon` object to increment the number of sides by two.

6. Call the `print_area` method on the created `RegularPolygon` object again to print the new area.

Here's an example of how the code could look like:

```python
import math

class RegularPolygon:
def __init__(self, sides, length):
self.sides = sides
self.length = length

def get_area(self):
return (self.sides * self.length**2) / (4 * math.tan(math.pi / self.sides))

def increment_sides(self):
self.sides += 2

def print_area(self):
print(f"Area with {self.sides} sides: {self.get_area()}")

num_sides = int(input("Enter number of sides: "))
side_length = float(input("Enter the side length: "))

polygon = RegularPolygon(num_sides, side_length)
polygon.print_area()

polygon.increment_sides()
polygon.print_area()
```

When you run the code and enter 4 as the number of sides and 5.6 as the side length, the output will be:

```
Area with 4 sides: 31.36
Area with 6 sides: 81.47566998803998
```

To know more about code visit:

User Moein Rahimi
by
8.2k points