A vector in 2-dimensional space, as used in math an physics, is just a pair of numbers: delta-x and delta-y. We call these numbers the components of the vector. Vectors are drawn as arrows. For example, the vector in the figure has a delta-x of 2, and a delta-y of 3. In physics, vectors represent forces. Adding two vectors represents placing those two forces on an object. The resulting force is just the addition of the two vectors. You can add two vectors by simply adding their delta-x and delta-y values. That is, the new delta-x is the sum of the delta-x of the two other vectors.
More formally, for vectors
v1 and v2 with components v1.dx, v1.dy and v2.dx, v2.dy, we can define various vector operations:- Vector addition:
v1 + v2 = v3is implemented asv3.dx = v1.dx + v2.dxandv3.dy = v1.dy + v2.dy - Vector subtraction:
v1 - v2 = v3is implemented asv3.dx = v1.dx - v2.dxandv3.dy = v1.dy - v2.dy - Vector scalar multiplication:
v1 * c = v2wherecis a constant is implemented asv2.dx = v1.dx * candv2.dy = v1.dy * c - Vector magnitude of
v1is a number equal to the square root ofv1.dx*v1.dx + v1.dy*v1.dy. - A vector
v1can be normalized by settingv1.dx = v1.dx / v1.magnitudeandv1.dy = v1.dy / v1.magnitude
Vector class that has the following data members:- dx, a double
- dy, a double
- A constructor that takes as argument two numbers which will become the delta-x and delta-y
public void addVector(Vector otherVector)which addsotherVectorto this vectorpublic void subtractVector(Vector otherVector)which subtractsotherVectorfrom this vectorpublic void multiply(int a)does a scalar multiplication of this vector byapublic double getMagnitude()returns the magnitude of this vector.public void normalize()normalizes this vector.public String toString()returns a printable version of the vector.
You must also implement a main function that tests all your methods to make sure they are correct.
One very important rule you must follow is Don't Repeat Yourself (DRY) which means that:
“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system ”In other words, any value that can be calculated from data member values must be calculated from data members values. This homework is due Tuesday, October 6 @ noon.
