Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Defining Classes
#1
Hi, I have been doing well on Python coding so far but now Im stuck on this assignment.

'''This class represents regular polygons. The initialization method
creates a regular polygon with a set number of sides of a given length.
The default constructor creates an equilateral triangle of side length 1.
There are methods for changing the length or number of sides, calculating
the perimeter and area of a RegularPolygon object, and printing a string
representation of a RegularPolygon object'''
You will need the following six methods
__init__
addSides
setLength
getPerimeter
getArea
toString

The formula for the area of a regular polygon is as follows (A is area, l is length, and n is the number of sides):
Please note that 180/n is a degree measurement that needs to be converted to radians. The math module has a tan() method and a radians method (math.tan() and math.radians(), respectively).

This is what I have so far
import math

class RegularPolygon:
  def __init__(self, n = 1, l = 1):
    self.__n = n
    self.__l = l

  def addSides(self, x):
   x = self.__n + x

  def setLength(self, l ):
    self._l = l

  def setPerimeter(self):
    return (self.__n * self.__l )

  def getArea(self):
    return

  def toString(self):
    return



demo_object = RegularPolygon (3, 3)
print(demo_object.self.__n() , demo_object.self__l())
Reply
#2
(Dec-14-2020, 07:46 PM)javesike1262 Wrote: now Im stuck on this assignment.
what is the particular problem you have?

Also, note that the use of __some_name attributes (with leading dunder), like __n, is really not needed here
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
So basically the particular problem I have is on the def Addsides part. So basically the n reprsents the number of sides the polygon has and l represents the length of the sides. So on the add sides part if I had a triangle which features 3 sides and 3 of side lengths, like lets say for example I wanted to add 3 so its going to be a hexagon and that. I just need help on defining it. I also need help of defining the tostring and the area according to the assignment and I have it due tomorrow.
Reply
#4
I will stick to your names/attributes, although they are not best approach

  • addSides - you want to increase the attribute __n, not x, right?
  • getArea() - your shape will consist of n congruent triangles. How would you calculate the area of the tirangle, knowing the side and the opposite angle (hint: using trigonometry you can calculate the height).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
So our teacher has made us do this.

[Image: obHq6Sy.png]
Also, all internal variables should be private. I will still be using n and l as my variables since I put those as the two parameters in the code since my teacher told me that's correct. What I don't understand is the add sides which I know its n added by a number which i'll say three to make it a hexagon. I also don't know the toString function which is also required.

This is the triagnle right represented by n = 3 and length of sides = 3
demo_object = RegularPolygon (3, 3)

I want the addSides to function like this
demo_object = RegularPolygon (6, 3)

Here is a better understanding of what I refer to in the code
So the toString method should return a string representation of the object. The string should tell us at minimum how many sides the object has. It could also tell us how long the sides are or what area of the polygon object is.

The addSides method should increase the number of sides. For example, demo_object.addSides(3) should add 3 sides to demo_object. That means that if demo_object started off as a triangle, it would now be a hexagon. If demo_object started off as pentagon, it would now be an octagon.
Reply
#6
(Dec-15-2020, 02:01 PM)javesike1262 Wrote: all internal variables should be private
there is no such thing as "private" variables in python. A name with single leading underscore is considered "for internal use". But is still accessible. Double leading underscore (like in your case) will make so that the name is mangled with the class name, e.g. __name will become _Class__name. But is still visible and accessible.

https://stackoverflow.com/q/1301346/4046632
https://stackoverflow.com/a/8689983/4046632
https://stackoverflow.com/q/7456807/4046632

you have almost done addSies():
 def addSides(self, x):
   x = self.__n + x
you should just do
 def addSides(self, x):
   self.__n = self.__n + x # or self.__n += x
this will increase __n, not x, which is lost once the function ends.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Thanks so much for the better understanding on the addsides portion!! Now I just need the toString part of the code and I'll do the rest of it by myself.

Heres the getArea part of the code so far

  def getArea(self):
    return (self.__l ** 2 / 4 * math.tan(180/self.__n))
addSides part done

  def addSides(self, x):
    self.__n = self.__n + x
Here's what I have so far
import math

class RegularPolygon:
  def __init__(self, n = 1, l = 1):
    self.__n = n
    self.__l = l

  def set_n(self, n):
    self.__n = n

  def get_n(self):
    return self.__n

  def addSides(self, x):
    self.__n = self.__n + x

  def setLength(self, l ):
    self._l = l

  def getLength(self):
    return self.__l 

  def setPerimeter(self):
    return (self.__n * self.__l )

  def getArea(self):
    return (self.__l ** 2 / 4 * math.tan(180/self.__n))
    

  def toString(self):
    return


x = 3
demo_object = RegularPolygon (3, 1)
print(demo_object.get_n() , demo_object.getLength())
demo_object.addSides(x)
print(demo_object.get_n(), demo_object.getLength())
print(demo_object.getArea())
print(demo_object.setPerimeter())
Here's what it printed
3 1
6 1
-1.601332799161569
6

I know its not complete but so far it seems to working good. Now it just needs the toString defined and the getArea probably fixed since according to the assignment it says "Please note that 180/n is a degree measurement that needs to be converted to radians. The math module has a tan() method and a radians method (math.tan() and math.radians(), respectively)."
Reply
#8
Did you forget something?
(Dec-14-2020, 07:46 PM)javesike1262 Wrote: Please note that 180/n is a degree measurement that needs to be converted to radians. The math module has a tan() method and a radians method (math.tan() and math.radians(), respectively).
also note that sometimes you have single leading underscore and most of the time - double leading underscore
for the asString - just create and return string with the information you want
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
Yes I did forget something the 180 has to be converted into radians so something like
(self.__l ** 2 / 4 * math.tan(math.radians(180/self.__n)))
which I know its wrong and tostring method is basically return a string that has the values of the internal variables included in it. Think about it like a print statement where you're trying to print words with values stored in variables.
Reply
#10
(Dec-15-2020, 04:33 PM)javesike1262 Wrote: Think about it like a print statement where you're trying to print words with values stored in variables.
Maybe you confused. I don't have to think about it. It's your homework. :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020