Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a class called Vec 2 D . The required methods are shown in the code snippet below. The comment indicates the action of the

Write a class called Vec2D. The required methods are shown in the code snippet below. The comment indicates the action of the method and what is returned.
A quick review of the dot product:
Take two vectors, each with Cartesian components x and y: a =(ax, ay) and b =(bx, by). The dot product of vectors a and b results in a scalar and is given by:
a b = axbx + ayby
class Vec2D():
def __init__(self, x, y):
"""Store x and y in a form of your choosing."""
def __str__(self):
"""Returns string of form '(x, y)'"""
def __repr__(self):
"""Returns string of form 'Vec2D(x, y)'"""
def get_x(self):
"""Getter for x. Returns x."""
def get_y(self):
"""Getter for y. Returns y."""
def set_x(self, x):
"""Setter for x."""
def set_y(self, y):
"""Setter for y."""
def __add__(self, b):
"""Vector addition of self and b. Returns a Vec2D object."""
def __sub__(self, b):
"""Vector subtraction: self - b. Returns a Vec2D object."""
def __mul__(self, scalar):
"""Multiplication on left of Vec2D and scalar: self * scalar. Returns Vec2D
,-> object."""
def __rmul__(self, scalar):
"""Multiplication on right of Vec2D and scalar: scalar * self. Returns Vec2D
,-> object."""
def length(self):
"""Computes length of the vector. Returns a float."""
def dot(self, b):
"""Computes dot product of self and b. Returns a float."""
Sample:
>>> a = Vec2D(3.0,0.0)
>>> a
Vec2D(3.0,0.0)
>>> print(a)
(3.0,0.0)
>>> a.get_x()
3.0
>>> a.get_y()
0.0
>>> b = Vec2D(0.0,0.0)
>>> b
Vec2D(0.0,0.0)
>>> b.set_x(0.0); b.set_y(4.0)
>>> b
Vec2D(0.0,4.0)
>>> c = a + b
>>> c
Vec2D(3.0,4.0)
>>> d = a - b
>>> d
Vec2D(3.0,-4.0)
>>> a*3
Vec2D(9.0,0.0)
>>>4.5*b
Vec2D(0.0,18.0)
>>> c.length()
5.0
>>> d.length()
5.0
>>> a.dot(b)
0.0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions