Add 9 3 & 10 1

This commit is contained in:
Matti 2024-04-19 13:25:49 +02:00
parent 359653949a
commit 8e9e9c497e
9 changed files with 305 additions and 12 deletions

View File

@ -13,6 +13,7 @@ Dateien wie Bilder die zur Lösung einer Aufgabe gehören sind in <br><ins>Medie
- Vorlesung 7 komplett
- Vorlesung 8 komplett
- Vorlesung 9 Aufgabe 2
- Vorlesung 10 Aufgabe 1
## Angefangene unfertige Lösungen:
- Damenproblem: VL 5 Aufgabe 2

View File

@ -0,0 +1,37 @@
package part10.aufg1;
import part10.aufg1.Exceptions.*;
public class Anwendung {
public static void main(String[] args){
matrix nr1 = new matrix(2,3);
matrix nr2 = new matrix(3,3);
nr1.setValue(1,1,1);
nr2.setValue(0,0,4);
try{
nr2.addMatrix(nr1);
}
catch (MatrixIncommpatibleForAdditionException e){
System.out.println("===========================================");
System.out.println("Details: \n" + e.getMessage());
System.out.println("===========================================");
}
matrix nr3 = new matrix(2,3);
nr3.setRow(new int[]{1,2,3},0);
nr3.setRow(new int[]{4,5,6},1);
//matrix nr4 = new matrix(3,2); // works fine
matrix nr4 = new matrix(3,3); // causes Runtime Exception
nr4.setCol(new int[]{7,9,11},0);
nr4.setCol(new int[]{8,10,12},1);
System.out.println(nr3.multiplyMatrix(nr4));
}
}

View File

@ -0,0 +1,8 @@
package part10.aufg1.Exceptions;
public class MatrixIncommpatibleForAdditionException extends Exception{
MatrixIncommpatibleForAdditionException(String s){
super(s);
}
}

View File

@ -0,0 +1,8 @@
package part10.aufg1.Exceptions;
class MatrixIncompatibleForMultiplicationException extends RuntimeException{
MatrixIncompatibleForMultiplicationException(String s){
super(s);
}
}

View File

@ -0,0 +1,171 @@
package part10.aufg1.Exceptions;
public class matrix {
private int numberOfRows;
private int numberOfCols;
int[][] field;
public matrix(int numberOfRows, int numberOfCols){
this.numberOfRows = numberOfRows;
this.numberOfCols = numberOfCols;
field = new int[numberOfRows][numberOfCols];
}
public int getNumberOfCols() {
return numberOfCols;
}
public int getNumberOfRows() {
return numberOfRows;
}
public void print(){
for (int i = 0; i<this.numberOfRows; i++){
for (int j = 0; j<this.numberOfCols; j++){
System.out.print(field[i][j]);
if (j != this.numberOfCols - 1) {
System.out.print(" ; ");
}
}
System.out.println();
}
System.out.println("===========");
}
public void setValue(int row, int col, int value){field[row][col] = value;}
int getValue(int row, int col){return field[row][col];}
public int[] getRow(int row){
return this.field[row];
}
public void setRow(int[] values, int row) {
if (values.length == this.getNumberOfCols()) {
for (int i = 0; i < values.length; i++){
this.setValue(row,i,values[i]);
}
}
}
public int[] getCol(int col){
int[] result = new int[this.getNumberOfRows()];
for (int i=0; i<this.getNumberOfRows(); i++){
result[i] = this.getValue(i, col);
}
return result;
}
public void setCol(int[] values, int col){
if (values.length == this.getNumberOfRows()){
for (int i=0; i<values.length; i++){
this.setValue(i, col, values[i]);
}
}
}
public void scalarMultiply(int factor){
for (int i = 0; i<this.numberOfRows; i++) {
for (int j = 0; j < this.numberOfCols; j++) {
field[i][j] *= factor;
}
}
}
public void addMatrix(matrix matrix2) throws MatrixIncommpatibleForAdditionException {
if ((this.getNumberOfRows() == matrix2.getNumberOfRows()) && (this.getNumberOfCols() == matrix2.getNumberOfCols())) {
for (int i = 0; i < this.numberOfRows; i++) {
for (int j = 0; j < this.numberOfCols; j++) {
this.field[i][j] += matrix2.field[i][j];
}
}
} else {
throw new MatrixIncommpatibleForAdditionException("Wrong size for addition, matrix1 is " + getNumberOfRows() +
"x" + getNumberOfCols() + " and matrix2 is " +
matrix2.getNumberOfRows() + "x" + matrix2.getNumberOfCols());
}
}
public boolean multiplyMatrix(matrix matrix2) throws MatrixIncompatibleForMultiplicationException {
boolean compatible = false;
if(this.getNumberOfRows() == matrix2.getNumberOfCols()){
compatible = true;
matrix result = new matrix(this.getNumberOfRows(), matrix2.getNumberOfCols());
for (int i=0; i<this.field.length; i++){
int[] currentRow = this.getRow(i);
for (int j=0; j<matrix2.field[0].length; j++){
int[] currentCol = matrix2.getCol(j);
int dotProduct = dotProduct(currentRow, currentCol);
result.setValue(i,j,dotProduct);
}
}
result.print();
}
else {
throw new MatrixIncompatibleForMultiplicationException("Wrong Sizes");
}
return compatible;
}
private int dotProduct(int[] vector1, int[] vector2){
int result = 0;
if (vector1.length == vector2.length){
for (int i=0; i<vector1.length; i++){
result += vector1[i] * vector2[i];
}
}
else {
// Vectors not compatible Exception
// TODO
}
return result;
}
public static void main(String[] args){
matrix nr1 = new matrix(2,3);
matrix nr2 = new matrix(3,3);
nr1.setValue(1,1,1);
nr2.setValue(0,0,4);
try{
nr2.addMatrix(nr1);
}
catch (MatrixIncommpatibleForAdditionException e){
System.out.println("===========================================");
System.out.println("===========================================");
System.out.println("Details: " + e.getMessage());
}
nr2.scalarMultiply(8);
nr2.print();
nr2.scalarMultiply(7);
nr2.print();
System.out.println("========================");
matrix nr3 = new matrix(2,3);
nr3.setRow(new int[]{1,2,3},0);
nr3.setRow(new int[]{4,5,6},1);
nr3.print();
matrix nr4 = new matrix(3,2);
nr4.setCol(new int[]{7,9,11},0);
nr4.setCol(new int[]{8,10,12},1);
nr4.print();
System.out.println(nr3.multiplyMatrix(nr4));
System.out.println(nr4.multiplyMatrix(nr3));
}
}

View File

@ -0,0 +1,7 @@
package part10;
public class placeholder {
public static void main(String[] args){
System.out.println("Sup nerd!");
}
}

View File

@ -1,5 +1,10 @@
package part8.aufg3;
/**
* No Exception Handling in this version.
* Variante with 'some' is in "part10/aufg1"
*/
public class matrix {
private int numberOfRows;
private int numberOfCols;
@ -47,10 +52,6 @@ public class matrix {
this.setValue(row,i,values[i]);
}
}
else {
//
// Amount of given Values is not compatible Exception
}
}
public int[] getCol(int col){
@ -69,10 +70,6 @@ public class matrix {
this.setValue(i, col, values[i]);
}
}
else {
//
// Amount of given Values is not compatible Exception
}
}
void scalarMultiply(int factor){
@ -122,10 +119,7 @@ public class matrix {
result += vector1[i] * vector2[i];
}
}
else {
// Vectors not compatible Exception
// TODO
}
return result;
}

55
src/part9/Fibonacci.java Normal file
View File

@ -0,0 +1,55 @@
package part9;
//
// DHBW Stuttgart Campus Horb
// Vorlesung: Programmierung I 1.Semester
// Teil 7, Aufgabe 2
// Musterlösung
// Autor: Olaf Herden
//
public class Fibonacci {
// Methode fiboRekursiv
// Berechnet die Fibonacci-Zahl des Arguments n rekursiv
static long fiboRekursiv(long n){
if (n == 1 || n == 2)
{return 1;}
else
{return fiboRekursiv(n-1) + fiboRekursiv(n-2);}
}
// Methode fiboIterativ
// Berechnet die Fibonacci-Zahl des Arguments n iterativ
static long fiboIterativ(long n){
long ergebnis =1;
long vorletzter = 1;
long letzter = 1;
if (n == 1 || n == 2)
{return 1;}
else
{
for (int i=3; i<n+1; i++)
{
ergebnis = vorletzter + letzter;
letzter = vorletzter;
vorletzter = ergebnis;
}
return ergebnis;
}
}
// Methode main
// Aufrufen der beiden Fibonacci-Zahl-Methoden von 1 bis 40
public static void main(String[]args){
for (int i=1;i<=46;i++){
System.out.println(i + " Fibonacci iterativ : " + fiboIterativ(i) + " Fibonacci rekursiv : " + fiboRekursiv(i));
}
}
}

12
src/part9/aufg3 Normal file
View File

@ -0,0 +1,12 @@
Zur Untersuchung (optischer Aspekte) von Quellcode gibt stehen eine Reihe von Programmen zur Verfügung,
so z.B. das Programm Checkstyle, das Quelltext auf den Programmierstil überprüft
und die Regeln der Java COde Conventions beherrscht.
Laden Sie das Programm Checkstyle herunter, installieren SIe es,
und überprüfen Sie den obenstehenden Quelltext.
Sie finden das Programm unter https://checkstyle.org/
Neben dem eigentlichen Programm gibt es auf der Seite auch Beschreibungen und Hinweise
über Plugins für gängige IDE.
====================
Auf dem Firmenlaptop ist die Installation nicht erlaubt.