In this task, you will extend the functionality of a Matrix
class to support matrix addition and subtraction operations using operator overloading. The Matrix
class already includes basic functionalities such as accessing and modifying elements.
Your objective is to implement addition and subtraction operations so that you can use matrix data correctly.
- The
Matrix
class constructor requires two arguments:rows
andcolumns
, indicating the dimensions of the matrix. - Matrix data is stored in a 2D array of integers.
- Access to matrix elements is facilitated through provided
get
andset
operators. - The
toString
method is overridden for easy printing of matrix contents.
Implement operator overloading for addition (+
) and subtraction (-
) operations within the Matrix
class. These operations should return a new Matrix
object representing the result.
Below is an example illustrating the usage of your extended Matrix
class:
val matrix1 = Matrix(2, 2)
matrix1[0, 0] = 1
matrix1[0, 1] = 2
matrix1[1, 0] = 3
matrix1[1, 1] = 4
val matrix2 = Matrix(2, 2)
matrix2[0, 0] = 5
matrix2[0, 1] = 6
matrix2[1, 0] = 7
matrix2[1, 1] = 8
val sumMatrix = matrix1 + matrix2
val differenceMatrix = matrix1 - matrix2
println("Sum of Matrices:\n$sumMatrix")
println("Difference of Matrices:\n$differenceMatrix")