Implement extension functions plus
and minus
to perform vector addition and subtraction.
Task Description:
- data class
Vector
is given with a propertycoordinates
, a list of integers representing the vector’s coordinates. - Implement a function
plus
for theVector
class to add two vectors. The function should return a newVector
representing the sum. - Implement a function
minus
for theVector
class to subtract two vectors. The function should return a newVector
representing the difference. - Ensure both functions handle cases where vectors have different dimensions by returning
null
or an appropriate error message. - Make operator invocations possible:
Example usage:
Vector(listOf(1, 2, 3)) + Vector(listOf(4, 5, 6)) == Vector(listOf(5,7,9)
Vector(listOf(-1, -3)) - Vector(listOf(-2, -2)) == Vector(listOf(1, -1))
Vector(listOf(3, 4, 5)) - Vector(listOf(1, 2)) == null
- Fill in the code.
- You can copy the code into IntelliJ/Android Studio, but it shouldn’t be necessary.
- Click “run” – the green triangle. The code will compile and several tests will be run.
- In the logs, you will see “try again” if the solution is incorrect, or “success” and a special code.
- Provide this code in the form below along with your email and click “send”.