I am having a problem in simple code I wrote
Class Point has x and y values
Class square (Point) includes 4 points : p1,p2,p3,p4
I am trying to update x value of p1 for example with sq1.p1.set_x(num1) and failed.
I tried to work with public defnition but also failed
Whot is my problem ? Thanks
class Point:
def __init__(self,x=0,y=0):
self.__x = x
self.__y = y
def get_x(self):
return self.__x
def get_y(self):
return self.__y
def set_x(self,x=0):
self.__x = x
def set_y(self,y=0):
self.__y = y
class Square (Point):
def __init__(self,p1,p2,p3,p4):
self.__p1 = p1
self.__p2 = p2
self.__p3 = p3
self.__p4 = p4
def get_p1(self):
return self.__p1
def get_p2(self):
return self.__p2
def get_p3(self):
return self.__p3
def get_p4(self):
return self.__p4
def set_p1(self,p1):
self.__p1 = p1
def set_p2(self,p2):
self.__p2 = p2
def set_p3(self,p1):
self.__p3 = p3
def set_p4(self,p4):
self.__p4 = p4
def main():
p1 = Point(2,0)
p2 = Point(2,2)
p3 = Point(0,2)
p4 = Point(0,0)
sq1 = Square(p1,p2,p3,p4)
sq1.p1.set_x(4) # this line failing
if __name__ == "__main__":
main()
Replace
sq1.p1.set_x(4) # this line failing
with
sq1.get_p1().set_x(4)
This is because class Square doesn't have a p1 property (it has a __p1 property, but not p1).
Related
I am studying Python Class and method. But I don't know whats wrong with my code. Need help. Result for P3, P4 do not work.
I made Point class and Rectangle class. The code works out for r1 = Rectangle(p1, p2). But, does not work out for r2 = Rectangle(p3, p4). Can someone please explain me what is wrong and how to fix the code. Thank you
class Point:
def __init__(self, x=0, y=0):
self.x = x
self.y = y
class Rectangle:
def __init__(self, p1, p2):
self.p1 = p1
self.p2 = p2
def get_area(self):
w = max(p1.x, p2.x)
h = max(p1.y, p2.y)
return w*h
def get_perimeter(self):
w = max(p1.x, p2.x)
h = max(p1.y, p2.y)
if w == h:
return w*4
else:
return abs(p2.x - p1.x)*2 + abs(p2.y - p1.y)*2
def is_square(self):
w = max(p1.x, p2.x)
h = max(p1.y, p2.y)
if w == h:
return True
else:
return False
p1 = Point(1,3)
p2 = Point(3,1)
r1 = Rectangle(p1, p2)
print(r1.get_area())
print(r1.get_perimeter())
print(r1.is_square())
p3 = Point(3,7)
p4 = Point(6,4)
r2 = Rectangle(p3, p4)
print(r2.get_area())
print(r2.get_perimeter())
print(r2.is_square())
Inside of the Rectangle class you are accessing the global scope p1 and p2 variables. All the class functions should use self.p1 and self.p2
I have a problem with a task. I need to write an python code which calculates a quadratic distance between two points.
The formula is:
D^2 = (x1 - x2)^2 + (y1 - y2)^2
and my code:
def quadratic_distance(p1: Point, p2: Point) -> float:
# YOUR CODE HERE
class p1:
def __init__(self, x1, y1):
self.x = x1
self.y = y1
class p2:
def __init__(self, x2, y2):
self.x = x2
self.y = y2
result1 = p1.x - p2.x
result2 = result1**2
result3 = p1.y - p2.y
result4 = result3**2
result5 = result2 + result4
return result5
but my problem is that i get an attribute error
AttributeError: type object 'p1' has no attribute 'x'
I am fairly new in the object oriented programming and have been stuck at this task. I hope someone can help me
assert quadratic_distance(Point(0, 0),Point(1, 1)) == 1.75
should be the solution
According to your formula, quadratic distance between Point(0,0) and Point(1,1) is 2. Not 1.75
This is my code. Try this
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
P1 = Point(0,0)
P2 = Point(1,1)
def quadratic_distance(p1: Point, p2: Point) -> float:
result1 = p1.x - p2.x
result2 = result1**2
result3 = p1.y - p2.y
result4 = result3**2
result5 = result2 + result4
return result5
print(quadratic_distance(P1, P2))
You are getting an error because you have not created an object of the class. In python, x is not an attribute of a class but x is the attribute of its object.
So you can do it as:
class p1:
def __init__(self, x1, y1):
self.x = x1
self.y = y1
class p2:
def __init__(self, x2, y2):
self.x = x2
self.y = y2
p1_obj = p1(5,5)
p2_obj = p2(10,10)
result1 = p1_obj.x - p2_obj.x
result2 = result1**2
result3 = p1_obj.y - p2_obj.y
result4 = result3**2
result5 = result2 + result4
return results
You can further improve it as p1 and p2 have the same properties (data member and member function) so we can just use one class named p (or any other name) and create two object p1 and p2 of the class
Although you have declared the class p1 and p2 but you haven't created any object. So, you are getting this error.
x and y are the instances of class p1 and p2 you can't access by their class name.
Either define x and y as class variables inside the class or define a object each of class p1 and p2 like given below.
p1ob=p1(4,5)
p2ob=p2(5,6)
I have this simple code to calculate the height and width of a rectangle defining 3 classes:
PointL which creates 1 point with x and y attributes.
Polyline: which just contains 4 points from the class Point.
Rectangle: which is supposed to call the points from Polyline and return the height and width.
However I'm getting 0.0 for both height and width all the time. I have to use the super() method for this.
class Point:
def __init__(self, x=0.0, y=0.0):
self.__x = x
self.__y = y
def set_x(self, x):
self.__x = x
def set_y(self, y):
self.__y = y
def set(self, x, y):
self.__x = x
self.__y = y
def get_x(self):
return self.__x
def get_y(self):
return self.__y
def __str__(self): # <---- New __str__ method
return "Point("+str(self.__x)+", "+str(self.__y)+")"
x = property(get_x, set_x)
y = property(get_y, set_y)
class Polyline:
def __init__(self, n=None):
self.__p0 = Point()
self.__p1 = Point()
self.__p2 = Point()
self.__p3 = Point()
self.__n = n
def get_p0(self):
return self.__p0
def get_p1(self):
return self.__p1
def get_p2(self):
return self.__p2
def get_p3(self):
return self.__p3
def set_p0(self, p):
self.__p0 = p
def set_p1(self, p):
self.__p1 = p
def set_p2(self, p):
self.__p2 = p
def set_p3(self, p):
self.__p3 = p
def __str__(self):
return "Line 1 from: " + str(self.__p0) + " to " + str(self.__p2) + "\n" + "Line 2 from: " + str(self.__p1) + " to " + str(self.__p3) + "\n"
p0 = property(get_p0, set_p0)
p1 = property(get_p1, set_p1)
p2 = property(get_p2, set_p2)
p3 = property(get_p3, set_p3)
class Rectangle(Point):
def __init__(self, n=None, height=0.0, width=0.0):
super().__init__(n) # <--- Call inherited constructor of Point-class
self.__height = height
self.__width = width
def get_height(self):
p0 = super().get_p0()
p1 = super().get_p1()
self.__height = p0.y - p1.y
def get_width(self):
p0 = super().get_p0()
p1 = super().get_p1()
self.__width = p0.x - p1.x
def __str__(self):
return "Rectangle dimensions: \n" + "Width: " + str(self.__width) + ", " + "Height: " + str(self.__height) + "\n"
width = property(get_width)
height = property(get_height)
These are my inputs:
polyline = Polyline()
polyline.p0.x = 0.0
polyline.p0.y = 2.0
polyline.p1.x = 3.0
polyline.p1.y = 4.0
print(polyline)
rectangle = Rectangle()
print(rectangle)
And these are my outputs:
Line 1 from: Point(0.0, 2.0) to Point(0.0, 4.0)
Line 2 from: Point(3.0, 4.0) to Point(3.0, 2.0)
Rectangle dimensions:
Width: 0.0, Height: 0.0
I have been working on an assignment for one of my introductory classes. I am almost done with my code and but I keep getting "AttributeError: 'tuple' object has no attribute 'dist'." I understand that the problem starts from midpt function and that I need to return an instance of the class instead of the tuple; however, I have not been able to do that. Could you please take a look at my code? It would be much appreciated.
'''
import math
class Point(object):
# The contructor for Point class
def __init__(self, x = 0, y = 0):
self.x = float(x)
self.y = float(y)
# The getter for x
#property
def x(self):
return self._x
# The setter for x
#x.setter
def x(self, value):
self._x = value
# The getter for y
#property
def y(self):
return self._y
# The setter for y
#y.setter
def y(self, value):
self._y = value
# Function for getting the distance between two points
def dist(self, other):
xVar = (other.x - self.x) ** 2
yVar = (other.y - self.x) ** 2
equation = xVar + yVar
distance = math.sqrt(equation)
return distance
# Function for getting the midpoint
def midpt(self, other):
xVar = (other.x - self.x) / 2
yVar = (other.y - self.y) / 2
midpoint = (xVar,yVar)
return midpoint
# Magic function for printing
def __str__(self):
return "({},{})".format(self.x, self.y)
##########################################################
# ***DO NOT MODIFY OR REMOVE ANYTHING BELOW THIS POINT!***
# Create some points
p1 = Point()
p2 = Point(3, 0)
p3 = Point(3, 4)
# Display them
print("p1:", p1)
print("p2:", p2)
print("p3:", p3)
# Calculate and display some distances
print("distance from p1 to p2:", p1.dist(p2))
print("distance from p2 to p3:", p2.dist(p3))
print("distance from p1 to p3:", p1.dist(p3))
# Calculate and display some midpoints
print("midpt of p1 and p2:", p1.midpt(p2))
print("midpt of p2 and p3:", p2.midpt(p3))
print("midpt of p1 and p3:", p1.midpt(p3))
# Just a few more things...
p4 = p1.midpt(p3)
print("p4:", p4)
print("distance from p4 to p1:", p4.dist(p1))
You might want to change midpoint = (xVar,yVar) to midpoint = Point(xVar, yVar).
In this way, p4 is a Point (and not a tuple!) instance, and you can call the dist method on it.
I create an instance of class Vector2 with line AB = Vector2.from_points(A, B)
But python errors out with TypeError: object() takes no parameters
on line AB = Vector2.from_points(A,B)
and on line return Vector2(cls, P2[0]-P1[0], P2[1]-P1[1])
so I figured maybe the book is wrong (I'm looking at examples in a book). I subtract the Vector2 and cls from the def from_points statement so that...
this is how the new line reads: return (P2[0]-P1[0], P2[1]-P1[1])
When I do this a receive the vector value from def from_points equal too (5, 10)
But then python errors out on:
print AB.get_magnitude()
with AttributeError: 'tuple' object has no attribute 'get_magnitude'
so without the code related to Vector2 and cls the program won't read AB as a class object but it seems that I'm not formatting it right so it won't go through.
I have been stuck on this for days.
#Vector Test
import math
class Vector2(object):
def _init_(self, x=0.0,y=0.0):
self.x = x
self.y = y
def _str_(self):
return"(%s,%s)"%(self.x,self.y)
#classmethod
def from_points(cls, P1, P2):
return Vector2(cls, P2[0]-P1[0],P2[1]-P1[1])
def get_magnitude(self):
return math.sqrt(self.x**2 + self.y**2)
A = (15.0, 20.0)
B = (20.0, 30.0)
AB = Vector2.from_points(A, B)
print AB
print AB.get_magnitude()
CHANGED CODE:
#Vector Test
import math
class Vector2(object):
def _init_(self, x=0.0,y=0.0):
self.x = x
self.y = y
def _str_(self):
return"(%s,%s)"%(self.x,self.y)
#classmethod
def from_points(cls, P1, P2):
return (P2[0]-P1[0],P2[1]-P1[1])
def get_magnitude(self):
return math.sqrt(self.x**2 + self.y**2)
A = (15.0, 20.0)
B = (20.0, 30.0)
AB = Vector2.from_points(A, B)
print AB
print AB.get_magnitude()
that's (probably) what you mean.
#Vector Test
import math
class Vector2(object):
def __init__(self, x=0.0,y=0.0):
self.x = x
self.y = y
def __str__(self):
return"(%s,%s)"%(self.x,self.y)
#classmethod
def from_points(cls, P1, P2):
return Vector2(P2.x-P1.x,P2.y-P1.y)
def get_magnitude(self):
return math.sqrt(self.x**2 + self.y**2)
A = Vector2(15.0, 20.0)
B = Vector2(20.0, 30.0)
AB = Vector2.from_points(A, B)
print( AB )
print( AB.get_magnitude() )