Python Forum
How do classes work? (rectangle)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do classes work? (rectangle)
#1
Hey just wanted to ask if you maybe know how to convert my current code that is below into one that uses classes im kinda confused about it.
it works with pass but im pretty sure its not the way it should be done ive tried with def __init__ but ive lost my self in it.
Im also a bit new to python.

import math


class Rectangle:
    pass
length = float(input('Please Enter the Length of a Rectangle: '))
height = float(input('Please Enter the Height of a Rectangle: '))
# calculate the area
Area = length * height
# calculate the Perimeter
Perimeter = 2 * (length + height)
#calculate diagonal of rectangle
Diagonal = (length**2) + (height**2)
Diagonal = math.sqrt(Diagonal)
print(" Diagonal of rectangle: %.4f" %Diagonal)
print(" Length of rectangle: %.f" %length)
print(" Area of a Rectangle is: %.f" %Area)
print(" Perimeter of Rectangle is: %.f" %Perimeter)
#points calculation:
Point1x = float(input("Enter X coordinates of your first point/corner: "))
Point1y = float(input("Enter Y coordinates of your first point/corner: "))
Point4x = Point1x + length
Point4y = Point1y - height
Point2x = Point4x - length
Point2y = Point1y - height
Point3x = Point1x + length
Point3y = Point4y + height
Centerx = length / 2 
Centery = height / 2
print ('Top Left corner point coordinates : %.2f ' %Point1x,", %.2f"%Point1y)
print ('Top Right corner point coordinates : %.2f ' %Point2x,", %.2f"%Point2y)
print ('Botoom Left corner point coordinates : %.2f ' %Point3x,", %.2f"%Point3y)
print ('Botoom Right corner point coordinates : %.2f ' %Point4x,", %.2f"%Point4y)
print ('Center point of a rectngle : %.2f ' %Centerx,", %2f"%Centery)
Reply
#2
What have you tried? We're not big on writing code for people here, but we would be happy to help you fix your code when you run into problems. Check out our class tutorial and give it a shot. When you do run into problems, please post your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thats ok ive managed to figure ropes around this thing
here is the result i came up with seems to work and prety happy with it if there is any improvements im open for sugestions :)

import math

class Rectangle:
    # Init function
    def __init__(self, length, height, Point1x, Point1y, Point4x, Point4y):
        # The only members are length and width
        self.length = length
        self.height = height
        self.Point1x = Point1x
        self.Point1y = Point1y
        self.Point4x = Point4x
        self.Point4y = Point4y
    def findarea(self):
        return self.length * self.height
    def perimeter(self):
        return 2*(self.length+self.height)
    def diagonal(self):
        return (math.sqrt((self.length ** 2) + (self.height ** 2)))
    def center(self):
        return self.length / 2 , self.height / 2
    def Point2(self):
        return self.Point4x - length, self.Point1y - height
    def Point3(self):
        return self.Point1x + length, self.Point4y + height


print('Find area and perimeter of rectangle enter lenght and width values bellow: ')
length=int(input('Please Enter the Length of a Rectangle: '))
height=int(input('Please Enter the Height of a Rectangle: '))
print('Input coordinates of your first point : ')
Point1x=int(input('Please Enter the x coordinate of a Rectangle: '))
Point1y=int(input('Please Enter the y coordinate of a Rectangle: '))
Point4x= Point1x + length
Point4y= Point1y - height


r1=Rectangle(length, height, Point1x, Point1y, Point4x, Point4y)
print('Area : ', r1.findarea())
print('Perimeter : ', r1.perimeter())
print('Diagonal : ', r1.diagonal())
print('Center of Rectangle : ', r1.center())
print ('Top Left corner point coordinates : %.f ' %Point1x,", %.f"%Point1y)
print ('Top Right corner point coordinates : ', r1.Point2())
print ('Botoom Left corner point coordinates : ', r1.Point3())
print ('Botoom Right corner point coordinates : %.f ' %Point4x, " %.f"%Point4y)
Reply
#4
you are using so called old-style string formatting.
Here is comparison between old-style and str.format() method
https://pyformat.info/
And with 3.6+ came even better - f-strings
One recommendation would be to use f-strings or str.format() method

Another recommendation - you need 1 point (x an y), height and width of the rectangle. These are 4 arguments to __init__()
There is no need to add second pair of x and y coordinates. You face the risk that there is discrepancy between x, y, height, width and supplied second point. In other words - select to request one point (e.g. upper-left, lower-right, lower-left or upper-right) and having also height and width calculate rest 3
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
In addition to what buran recommends, I would say document, document, document. Add a docstring to the class and each method:

class Dummy(object):
   """This is the docstring for the class."""
   
   def foo(self, bar):
      """This is the docstring for the foo method."""
In the docstring you put a description of the class or what the method does. You can also put in a list of methods and attributes for the class, or a list of parameters for a method. Docstrings help explain your code to other programmers (including yourself in six months) and are available with help(Class) or help(Class.method) in the interactive console.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
kay thanks ill try that
initially ive tried to do this with height and length and a single point but ive confused my self in the end so i ive did 4 point a bit strange
Reply


Forum Jump:

User Panel Messages

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