Vorlesung 6 / Aufgabe 2
This commit is contained in:
parent
2723cb4288
commit
c14cedfcd9
@ -1,35 +0,0 @@
|
||||
class Aufgabe {
|
||||
public static void main(String[] args) {
|
||||
Matrix matrix1 = new Matrix(3,1);
|
||||
matrix1.setValue(1,1,5);
|
||||
matrix1.setValue(2,1,4);
|
||||
matrix1.setValue(3,1,8);
|
||||
|
||||
Matrix matrix2 = new Matrix(3,1);
|
||||
matrix2.setValue(1,1,9);
|
||||
matrix2.setValue(2,1,3);
|
||||
matrix2.setValue(3,1,4);
|
||||
|
||||
System.out.println("Matrix 1:");
|
||||
matrix1.print();
|
||||
|
||||
System.out.println("Matrix 2:");
|
||||
matrix2.print();
|
||||
|
||||
System.out.println("Matrix 1 nach Multiplikation mit 5:");
|
||||
matrix1.multiply(5);
|
||||
matrix1.print();
|
||||
|
||||
System.out.println("Matrix 1 nach Addition mit Matrix 2:");
|
||||
matrix1.add(matrix2);
|
||||
matrix1.print();
|
||||
|
||||
System.out.println("Matrix 2 nach Multiplikation mit Matrix 1:");
|
||||
try {
|
||||
matrix2.multiply(matrix2);
|
||||
matrix2.print();
|
||||
} catch(MatrixException e) {
|
||||
System.out.println("Fehler beim Multiplizieren zweier Matrizen: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
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.init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
this.matrix = new int[this.rows][this.columns];
|
||||
|
||||
for(int r = 0; r < this.rows; r++) {
|
||||
for(int c = 0; c < this.columns; c++) {
|
||||
this.setValue(r+1, c+1, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int getValue(int row, int column) {
|
||||
return this.matrix[row-1][column-1];
|
||||
}
|
||||
|
||||
int getNumberOfColumns() {
|
||||
return this.columns;
|
||||
}
|
||||
|
||||
int getNumberOfRows() {
|
||||
return this.rows;
|
||||
}
|
||||
|
||||
void setValue(int row, int column, int value) {
|
||||
this.matrix[row-1][column-1] = value;
|
||||
}
|
||||
|
||||
void multiply(int scalar) {
|
||||
for(int r = 0; r < this.rows; r++) {
|
||||
for(int c = 0; c < this.columns; c++) {
|
||||
this.matrix[r][c] *= scalar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void multiply(Matrix matrix) {
|
||||
if(isSameSize(matrix)) {
|
||||
for(int r = 0; r < this.rows; r++) {
|
||||
for(int c = 0; c < this.columns; c++) {
|
||||
this.matrix[r][c] *= matrix.getValue(r+1, c+1);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new MatrixException();
|
||||
}
|
||||
}
|
||||
|
||||
boolean isSameSize(Matrix matrix) {
|
||||
return this.getNumberOfColumns() == matrix.getNumberOfColumns() && this.getNumberOfRows() == matrix.getNumberOfRows();
|
||||
}
|
||||
|
||||
void add(Matrix matrix) {
|
||||
if(this.getNumberOfColumns() == matrix.getNumberOfColumns() && this.getNumberOfRows() == matrix.getNumberOfRows()) {
|
||||
for(int r = 0; r < this.rows; r++) {
|
||||
for(int c = 0; c < this.columns; c++) {
|
||||
this.matrix[r][c] += matrix.getValue(r+1, c+1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print() {
|
||||
for(int r = 0; r < this.rows; r++) {
|
||||
for(int c = 0; c < this.columns; c++) {
|
||||
System.out.printf("%5d", this.getValue(r+1, c+1));
|
||||
}
|
||||
System.out.print("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MatrixException extends RuntimeException {
|
||||
|
||||
}
|
40
VL06/Aufgabe02/Aufgabe02.java
Normal file
40
VL06/Aufgabe02/Aufgabe02.java
Normal file
@ -0,0 +1,40 @@
|
||||
package VL06.Aufgabe02;
|
||||
|
||||
/**
|
||||
* Vorlesung 6 / Aufgabe 2
|
||||
*
|
||||
* @author Sebastian Brosch
|
||||
*/
|
||||
class Aufgabe02 {
|
||||
public static void main(String[] args) {
|
||||
Matrix matrix1 = new Matrix(3, 2);
|
||||
matrix1.setValue(0, 0, 5);
|
||||
matrix1.setValue(1, 0, 4);
|
||||
matrix1.setValue(2, 0, 8);
|
||||
matrix1.setValue(0, 1, 7);
|
||||
matrix1.setValue(1, 1, 3);
|
||||
matrix1.setValue(2, 1, 9);
|
||||
|
||||
Matrix matrix2 = new Matrix(3, 2);
|
||||
matrix2.setValue(0, 0, 9);
|
||||
matrix2.setValue(1, 0, 3);
|
||||
matrix2.setValue(2, 0, 4);
|
||||
matrix2.setValue(0, 1, 8);
|
||||
matrix2.setValue(1, 1, 6);
|
||||
matrix2.setValue(2, 1, 3);
|
||||
|
||||
System.out.println("\nMatrix 1:");
|
||||
matrix1.print();
|
||||
|
||||
System.out.println("\nMatrix 2:");
|
||||
matrix2.print();
|
||||
|
||||
System.out.println("\nMatrix 1 nach Multiplikation mit 5:");
|
||||
matrix1.multiply(5);
|
||||
matrix1.print();
|
||||
|
||||
System.out.println("\nMatrix 1 nach Addition mit Matrix 2:");
|
||||
matrix1.add(matrix2);
|
||||
matrix1.print();
|
||||
}
|
||||
}
|
115
VL06/Aufgabe02/Matrix.java
Normal file
115
VL06/Aufgabe02/Matrix.java
Normal file
@ -0,0 +1,115 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user