# Python OOP with Dynamic Typing

I need help with this short Python homework about shapes, link to the assignment page is here https://sites.google.com/cs.stonybrook.edu/cse-216/assignments_1/assignment-5-oop-with-dynamic-typing

You can download the code on the page or you can also see the attached files below.

Some clarifications- in order to check equality of shapes for each class, you want them to have the same type and same coordinates. For the TwoDPoint Class,where the TODO magic method is, p is self and q is other, and adding/subtracting would mean x1-x2 and so on.

##### Additional Instructions:

hw5codebase/quadrilateral.py
from .two_d_point import TwoDPoint
from typing import Tuple
class Quadrilateral:
def __init__(self, *floats):
points = TwoDPoint.from_coordinates(list(floats))
self.__vertices = tuple(points[0:3])
@property
def vertices(self):
return self.__vertices
def side_lengths(self):
"""Returns a tuple of four floats, each denoting the length of a side of this quadrilateral. The value must be
ordered clockwise, starting from the top left corner."""
return 0, 0, 0, 0 # TODO
def smallest_x(self):
"""Returns the x-coordinate of the vertex with the smallest x-value of the four vertices of this
quadrilateral."""
return 0 # TODO
hw5codebase/rectangle.py
from .quadrilateral import Quadrilateral
from .two_d_point import TwoDPoint
class Rectangle(Quadrilateral):
def __init__(self, *floats):
super().__init__(*floats)
if not self.__is_member():
raise TypeError("A rectangle cannot be formed by the given coordinates.")
def __is_member(self):
"""Returns True if the given coordinates form a valid rectangle, and False otherwise."""
return False # TODO
def center(self):
"""Returns the center of this rectangle, calculated to be the point of intersection of its diagonals."""
return TwoDPoint(0, 0) # TODO
def area(self):
"""Returns the area of this rectangle. The implementation invokes the side_lengths() method from the superclass,
and computes the product of this rectangle's length and width."""
return 0 # TODO
hw5codebase/square.py
from .rectangle import Rectangle
from .quadrilateral import Quadrilateral
class Square(Rectangle):
def __init__(self, *floats):
super().__init__(*floats)
if not self.__is_member():
raise TypeError("A square cannot be formed by the given coordinates.")
def snap(self):
"""Snaps the sides of the square such that each corner (x,y) is modified to be a corner (x',y') where x' is the
integer value closest to x and y' is the integer value closest to y. This, of course, may change the shape to a
general quadrilateral, hence the return type. The only exception is when the square is positioned in a way where
this approximation will lead it to vanish into a single point. In that case, a call to snap() will not modify
this square in any way."""
return Quadrilateral() # TODO
hw5codebase/two_d_point.py
from typing import List
class TwoDPoint:
def __init__(self, x, y) -> None:
self.__x = x
self.__y = y
@property
def x(self):
return self.__x
@property
def y(self):
return self.__y
def __eq__(self, other: object) -> bool:
return False # TODO
def __ne__(self, other: object) -> bool:
return not self.__eq__(other)
def __str__(self) -> str:
return '(%g, %g)' % (self.__x, self.__y)
# TODO: add magic methods such that two TwoDPoint objects can be added and subtracted coordinate-wise just by using
# syntax of the form p + q or p - q
@staticmethod
def from_coordinates(coordinates: List[float]):
if len(coordinates) % 2 != 0:
raise Exception("Odd number of floats given to build a list of 2-d points")
points = []
it = iter(coordinates)
for x in it:
points.append(TwoDPoint(x, next(it)))
return points