Add 8 3 math is very wrong, programming works

This commit is contained in:
Matti 2024-04-15 17:04:13 +02:00
parent 3831badb99
commit 1cefc38b78
2 changed files with 93 additions and 1 deletions

View File

@ -19,5 +19,7 @@ Dateien wie Bilder die zur Lösung einer Aufgabe gehören sind in <br><ins>Medie
- Keine - Keine
## Notizen
- part8 aufg3 matrix.multiplyMatrix is mathematically completely wrong
## Fragen ## Fragen
- Was können die in Interfaces definierten Variablen? - Was können die in Interfaces definierten Variablen?

View File

@ -0,0 +1,90 @@
package part8.aufg3;
public class matrix {
private int rows;
private int cols;
int[][] field;
matrix(int rows, int cols){
this.rows = rows;
this.cols = cols;
field = new int[rows][cols];
}
public int getCols() {
return cols;
}
public int getRows() {
return rows;
}
void print(){
for (int i=0; i<this.rows; i++){
for (int j=0; j<this.cols; j++){
System.out.print(field[i][j]);
if (j != this.cols-1) {
System.out.print(" ; ");
}
}
System.out.println();
}
System.out.println("===========");
}
void setValue(int row, int col, int value){field[row][col] = value;}
int getValue(int row, int col){return field[row][col];}
void scalarMultiply(int factor){
for (int i=0; i<this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
field[i][j] *= factor;
}
}
}
void addMatrix(matrix matrix2){
for (int i=0; i<this.rows; i++) {
for (int j = 0; j < this.cols; j++) {
field[i][j] += matrix2.field[i][j];
}
}
}
boolean multiplyMatrix(matrix matrix2){
boolean compatible = false;
if(this.getRows() == matrix2.getCols()){
compatible = true;
matrix result = new matrix(this.getRows(), matrix2.getCols());
result.print();
}
return compatible;
}
public static void main(String[] args){
matrix nr1 = new matrix(2,3);
matrix nr2 = new matrix(2,3);
nr1.setValue(1,1,1);
nr2.setValue(0,0,4);
nr2.addMatrix(nr1);
nr2.print();
nr2.scalarMultiply(8);
nr2.print();
nr2.scalarMultiply(7);
nr2.print();
matrix nr3 = new matrix(7,4);
matrix nr4 = new matrix(6,7);
System.out.println(nr3.multiplyMatrix(nr4));
}
}