HW7 with py, testpy, and two txt
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
class Vector_1:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, Vector_1):
|
||||
# Add the corresponding coordinates of both vectors
|
||||
new_x = self.x + other.x
|
||||
new_y = self.y + other.y
|
||||
return Vector_1(new_x, new_y)
|
||||
else:
|
||||
raise TypeError("Unsupported operand type: +")
|
||||
|
||||
def __str__(self):
|
||||
return f"Vector({self.x}, {self.y})"
|
||||
|
||||
# # Create two Vector instances
|
||||
# vector1 = Vector_1(2, 3)
|
||||
# vector2 = Vector_1(4, 5)
|
||||
|
||||
# # Add two Vector instances using the + operator
|
||||
# result1 = vector1 + vector2
|
||||
|
||||
# # Print the result
|
||||
# print(result1)
|
||||
|
||||
|
||||
|
||||
class Vector_2:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
def __add__(self, other):
|
||||
if isinstance(other, Vector_2):
|
||||
# Add the corresponding coordinates of both vectors
|
||||
new_x = self.x + other.x
|
||||
new_y = self.y + other.y
|
||||
return Vector_2(new_x, new_y)
|
||||
else:
|
||||
raise TypeError("Unsupported operand type: +")
|
||||
|
||||
# def __str__(self):
|
||||
# return f"Vector({self.x}, {self.y})"
|
||||
|
||||
# # Create two Vector instances
|
||||
# vector3 = Vector_2(2, 3)
|
||||
# vector4 = Vector_2(4, 5)
|
||||
|
||||
# # Add two Vector instances using the + operator
|
||||
# result2 = vector3 + vector4
|
||||
|
||||
# # Print the result
|
||||
# print(result2)
|
||||
|
||||
|
||||
def main():
|
||||
# Create two Vector instances
|
||||
vector1 = Vector_1(2, 3)
|
||||
vector2 = Vector_1(4, 5)
|
||||
# Add two Vector instances using the + operator
|
||||
result1 = vector1 + vector2
|
||||
# Print the result
|
||||
print("This is printout of the Class using __str__() method:")
|
||||
print(result1)
|
||||
|
||||
# Create two Vector instances
|
||||
vector3 = Vector_2(2, 3)
|
||||
vector4 = Vector_2(4, 5)
|
||||
# Add two Vector instances using the + operator
|
||||
result2 = vector3 + vector4
|
||||
# Print the result
|
||||
print("This is printout of the Class without using __str__() method:")
|
||||
print(result2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user