116 lines
2.8 KiB
Java
116 lines
2.8 KiB
Java
package VL06.Aufgabe02;
|
|
|
|
/**
|
|
* A class to represent a Matrix.
|
|
*/
|
|
public class Matrix {
|
|
private int rows = 0;
|
|
private int columns = 0;
|
|
private int[][] matrix;
|
|
|
|
Matrix(int rows, int columns) {
|
|
this.rows = rows;
|
|
this.columns = columns;
|
|
this.matrix = new int[this.rows][this.columns];
|
|
|
|
// initialize all fields with zero.
|
|
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
|
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
|
|
this.matrix[rowIndex][columnIndex] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Method to get the number of columns.
|
|
*
|
|
* @return The number of columns.
|
|
*/
|
|
int getColumns() {
|
|
return this.columns;
|
|
}
|
|
|
|
/**
|
|
* Method to get the number of rows.
|
|
*
|
|
* @return The number of rows.
|
|
*/
|
|
int getRows() {
|
|
return this.rows;
|
|
}
|
|
|
|
/**
|
|
* Method to get the value of a specific field.
|
|
*
|
|
* @param row The row of the value.
|
|
* @param column The column of the value.
|
|
* @return The value of the given row and column.
|
|
*/
|
|
int getValue(int row, int column) {
|
|
return this.matrix[row][column];
|
|
}
|
|
|
|
/**
|
|
* Method to set the value of a specific field.
|
|
*
|
|
* @param row The row to set the value.
|
|
* @param column The column to set the value.
|
|
* @param value The value to be set in the field.
|
|
*/
|
|
void setValue(int row, int column, int value) {
|
|
this.matrix[row][column] = value;
|
|
}
|
|
|
|
/**
|
|
* Methode to get the state whether the given Matrix is same size.
|
|
*
|
|
* @param matrix The Matrix to check for same size.
|
|
* @return The state whether the given Matrix is same size.
|
|
*/
|
|
boolean isSameSize(Matrix matrix) {
|
|
return (this.rows == matrix.getRows()) && (this.columns == matrix.getColumns());
|
|
}
|
|
|
|
/**
|
|
* Method to add a Matrix to the current Matrix.
|
|
*
|
|
* @param matrix The Matrix to add to the current Matrix.
|
|
*/
|
|
void add(Matrix matrix) {
|
|
if (!this.isSameSize(matrix)) {
|
|
return;
|
|
}
|
|
|
|
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
|
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
|
|
this.matrix[rowIndex][columnIndex] += matrix.getValue(rowIndex, columnIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Method to multiply a scalar value to the Matrix.
|
|
*
|
|
* @param scalar The scalar value to multiply the Matrix.
|
|
*/
|
|
void multiply(int scalar) {
|
|
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
|
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
|
|
this.matrix[rowIndex][columnIndex] *= scalar;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Method to print the Matrix.
|
|
*/
|
|
void print() {
|
|
for (int rowIndex = 0; rowIndex < this.rows; rowIndex++) {
|
|
for (int columnIndex = 0; columnIndex < this.columns; columnIndex++) {
|
|
System.out.printf("%5d", this.getValue(rowIndex, columnIndex));
|
|
}
|
|
System.out.print("\n");
|
|
}
|
|
}
|
|
}
|