CSCI 240 | Spring 2025 |
For this assignment, implement a class representing a 3D vector which will make extensive use of overloading class methods. This means there will be multiple versions of methods defined that each take different arguments. This assignment rewards one for being "smart but also lazy" when it comes to implementing each of these methods, as they can readily use previously defined methods to reduce the amount of code necessary to write to complete the next one.
int main() has already been written for this assignment. It is available for download from the autograder, Blackboard or by using the following link:
http://faculty.cs.niu.edu/~byrnes/csci240/pgms/assign9.cpp
The cpp file that is submitted for grading must be named assign9.cpp.
The Vector class declaration should be placed at the top of the source code file while the method implementations should be placed after the closing curly brace for main().
This class will only have one private data member: an array of integers with a maximum capacity of three, meant to represent the coefficients of the i, j, and k components of this 3D vector.
The default constructor should initialize the values of the private array of integers to all be equal to zero.
This alternate constructor will allow for the three values of the private array of integers to be initialized from values found in an array of integers passed as an argument. For this assignment, we may assume this argument always has exactly three values to be found.
The following methods are required for the Vector class. They should all be public.
This accessor method will unconditionally copy the values from the array of integers passed as an argument to the private array of integers belonging to this object. Note: It is strongly recommended that you simply invoke this method within the alternate constructor described above, to avoid repeating the same code in two locations.
This method will return a Vector object that contains the sum of the corresponding values from this Vector's private array of integer values and the array of integers passed as an argument.
This method will do exactly the same as the above method, except the argument is another Vector object. Note: It is strongly recommended to only formally implement one of these two methods and to utilize the alternate constructor to "typecast" an array of integers as a Vector object and then call the overloaded version. Again, we want to avoid redundantly repeating code whenever possible.
This method will return a Vector object containing the same values as the Vector object the invoked this method and multiplied by the integer passed as an argument. So <1, 2, 3> * 3 = <3, 6, 9>.
This method will return a Vector object containing the Cross Product of the Vector object that invoked this method and the "vector" argument passed as an array of integers.
To calculate the coefficients of this cross product Vector object, use the following formula, where a and b are two vectors each with three coefficient values:
a X b = < ( (a2*b3) - (a3*b2) ), ( (a3*b1) - (a1*b3) ), ( (a1*b2) - (a2*b1) ) >
Note how mathematicians and physicists start counting their subscripts from 1 as seen above.
This method will do exactly the same as the above method, except the argument is another Vector object.
This method will return a true or false value depending on whether the values of this Vector object's array of integers match exactly to those found in the array of integers being passed as an argument.
This method will do exactly the same as the above method, except the argument is another Vector object.
This method will return a string value representing this Vector object's mathematical vector. So if this Vector contained the values 1, 2, and 3, then the return value should be:
<1, 2, 3>
Note: There is a to_string(int) function available in the string library that can be helpful for converting integer values into a string representing the same number.
Each constructor and method MUST have a documentation box like a function.
Hand in a copy of the source code (the CPP file) on the autograder and Blackboard.
Test Number? 1 Test #1: Testing default constructor and to_string(): alpha == <0, 0, 0> bravo == <0, 0, 0>
Test Number? 2 Test #2: Testing alternate constructor and to_string: charlie == <1, 2, 3> delta == <2, 3, 4>
Test Number? 3 Test #3: Testing set method: initially echo == <0, 0, 0> after call to set method echo == <1, 2, 3> after second call to set method echo == <-9, 3, 15>
Test Number? 4 Test #4: Testing add methods: add with int [] argument: <1, 2, 3> + <5, 14, -6> == <6, 16, -3> add with Vector argument: <1, 2, 3> + <6, 16, -3> == <7, 18, 0>
Test Number? 5 Test #5: Testing scalar multiplication: <14, 2, -9> * 5 == <70, 10, -45> <14, 2, -9> * 3 == <42, 6, -27>
Test Number? 6 Test #6: Testing cross-product multiplication methods: multiply with int [] argument: <1, 2, 3> X <-4, 3, 5> == <1, -17, 11> multiply with Vector argument: <2, -3, 6> X <4, 13, -5> == <-63, 34, 38>
Test Number? 7 Test #7: Testing isEqual methods isEqual with int [] argument: comparing <1, 2, 3> and <1, 2, 3> equal -- SUCCESS isEqual with int [] argument: comparing <1, 2, 3> and <0, 1, 4> not equal -- SUCCESS isEqual with Vector argument: comparing <1, 2, 3> and <1, 2, 3> equal -- SUCCESS isEqual with Vector argument: comparing <1, 2, 3> and <0, 2, 6> not equal -- SUCCESS