fbpx

#kotlinDevChallenge – 17

Implement extension functions plus and minus to perform vector addition and subtraction.

Task Description:

  1. data class Vector is given with a property coordinates, a list of integers representing the vector’s coordinates.
  2. Implement a function plus for the Vector class to add two vectors. The function should return a new Vector representing the sum.
  3. Implement a function minus for the Vector class to subtract two vectors. The function should return a new Vector representing the difference.
  4. Ensure both functions handle cases where vectors have different dimensions by returning null or an appropriate error message.
  5. 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
  1. Fill in the code.
  2. You can copy the code into IntelliJ/Android Studio, but it shouldn’t be necessary.
  3. Click “run” – the green triangle. The code will compile and several tests will be run.
  4. In the logs, you will see “try again” if the solution is incorrect, or “success” and a special code.
  5. Provide this code in the form below along with your email and click “send”.

more insights

Uncategorized
Jarosław Michalik

#kotlinDevChallenge 2023 summary

The KotlinDevChallenge was not just a competition, but a journey of learning, growth, and community engagement. Every participant in this challenge is a winner in

Read More »

AndroidPro newsletter 🚀

join 3057 developers reading AndroidPro newsletter every week

👉 tips & tricks, articles, videos

👉 every Thursday in your inbox

🎁 bonus on signup – Clean Architecture blueprint

brought to you by Jarek Michalik, Kotlin GDE

You can unsubscribe any time. For details visit our privacy policy.

android pro newsletter tips and tricks how to learn android