Compare commits
17 Commits
5875ee95ce
...
main
Author | SHA1 | Date | |
---|---|---|---|
80253dc3e8 | |||
|
8a8589a25d | ||
|
b06c0ffcab | ||
|
c7aa18990e | ||
|
2a7ba7393c | ||
|
318025d3df | ||
|
00200af26b | ||
|
9ff0ded7ea | ||
|
9aaeaeed5b | ||
|
4286d01786 | ||
|
f53f29200e | ||
|
7c7a91a762 | ||
|
3aa56bd3fb | ||
|
b0b248ca9d | ||
|
7fd326360f | ||
|
c2cc7330d5 | ||
|
8e9e9c497e |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -29,3 +29,4 @@ bin/
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
/Medien/Bilder & Co/Goldfrosch.pptx
|
/Medien/Bilder & Co/Goldfrosch.pptx
|
||||||
/Medien/Bilder & Co/Goldfrosch.xlsx
|
/Medien/Bilder & Co/Goldfrosch.xlsx
|
||||||
|
/src/HZ/
|
||||||
|
3
.idea/misc.xml
generated
3
.idea/misc.xml
generated
@@ -1,5 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-22" project-jdk-type="JavaSDK">
|
||||||
<output url="file://$PROJECT_DIR$/out" />
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@@ -8,15 +8,14 @@ Dateien wie Bilder die zur Lösung einer Aufgabe gehören sind in <br><ins>Medie
|
|||||||
## Bisher vollständig vorhanden:
|
## Bisher vollständig vorhanden:
|
||||||
- Vorlesung 3 komplett
|
- Vorlesung 3 komplett
|
||||||
- Vorlesung 4 komplett
|
- Vorlesung 4 komplett
|
||||||
- Vorlesung 5 Aufgabe 1 & 3 von 3
|
- Vorlesung 5 komplett
|
||||||
- Vorlesung 6 komplett
|
- Vorlesung 6 komplett
|
||||||
- Vorlesung 7 komplett
|
- Vorlesung 7 komplett
|
||||||
- Vorlesung 8 komplett
|
- Vorlesung 8 komplett
|
||||||
- Vorlesung 9 Aufgabe 2
|
- Vorlesung 9 Aufgabe 2
|
||||||
|
- Vorlesung 10 komplett
|
||||||
|
|
||||||
## Angefangene unfertige Lösungen:
|
## Angefangene unfertige Lösungen:
|
||||||
- Damenproblem: VL 5 Aufgabe 2
|
|
||||||
<br>Nicht die gewünschte rekursive Backtracking-Implementierung
|
|
||||||
- JavaDoc: VL 9 Aufgabe 1
|
- JavaDoc: VL 9 Aufgabe 1
|
||||||
<br>Das Tool mag mich nicht
|
<br>Das Tool mag mich nicht
|
||||||
|
|
||||||
|
37
src/part10/aufg1/Anwendung.java
Normal file
37
src/part10/aufg1/Anwendung.java
Normal 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(2,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);
|
||||||
|
|
||||||
|
nr4.multiplyMatrix(nr3);
|
||||||
|
nr3.multiplyMatrix(nr4);
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,8 @@
|
|||||||
|
package part10.aufg1.Exceptions;
|
||||||
|
|
||||||
|
public class MatrixIncommpatibleForAdditionException extends Exception{
|
||||||
|
MatrixIncommpatibleForAdditionException(String s){
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@@ -0,0 +1,8 @@
|
|||||||
|
package part10.aufg1.Exceptions;
|
||||||
|
|
||||||
|
|
||||||
|
class MatrixIncompatibleForMultiplicationException extends RuntimeException{
|
||||||
|
MatrixIncompatibleForMultiplicationException(String s){
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
}
|
170
src/part10/aufg1/Exceptions/matrix.java
Normal file
170
src/part10/aufg1/Exceptions/matrix.java
Normal file
@@ -0,0 +1,170 @@
|
|||||||
|
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 {
|
||||||
|
throw new RuntimeException("Vectors are not the sime size");
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
7
src/part10/aufg2/NegativeValueException.java
Normal file
7
src/part10/aufg2/NegativeValueException.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package part10.aufg2;
|
||||||
|
|
||||||
|
public class NegativeValueException extends RuntimeException{
|
||||||
|
NegativeValueException(String s) {
|
||||||
|
super(s);
|
||||||
|
}
|
||||||
|
}
|
94
src/part10/aufg2/PositiveVector.java
Normal file
94
src/part10/aufg2/PositiveVector.java
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
package part10.aufg2;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class PositiveVector {
|
||||||
|
double[] values;
|
||||||
|
|
||||||
|
PositiveVector(int size){
|
||||||
|
this.values = new double[size];
|
||||||
|
Arrays.fill(values, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int length(){
|
||||||
|
return this.values.length;
|
||||||
|
}
|
||||||
|
public void setValues(double[] values) {
|
||||||
|
for (int i=0; i<values.length; i++){
|
||||||
|
assert (values[i] >= 0) : "Value cannot be negative";
|
||||||
|
}
|
||||||
|
this.values = values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double[] getValues() {
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSingleValue(double value, int index){
|
||||||
|
if (value < 0){
|
||||||
|
System.out.println(value + " is negativ and cannot be assigned!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.values[index] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getSingleValue(int index){
|
||||||
|
return this.values[index];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void print(){
|
||||||
|
System.out.println(Arrays.toString(this.values));
|
||||||
|
}
|
||||||
|
|
||||||
|
public PositiveVector vectorAddition(PositiveVector vector2){
|
||||||
|
PositiveVector result = new PositiveVector(this.length());
|
||||||
|
|
||||||
|
if (this.values.length == vector2.getValues().length){
|
||||||
|
for (int i=0; i<this.values.length; i++){
|
||||||
|
result.setSingleValue((this.getSingleValue(i) + vector2.getSingleValue(i)), i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public PositiveVector vectorSubtraction(PositiveVector vector2) throws NegativeValueException {
|
||||||
|
PositiveVector result = new PositiveVector(this.length());
|
||||||
|
|
||||||
|
if (this.values.length == vector2.getValues().length){
|
||||||
|
for (int i=0; i<this.values.length; i++){
|
||||||
|
if ((this.getSingleValue(i) - vector2.getSingleValue(i)) < 0){
|
||||||
|
throw new NegativeValueException(this.getSingleValue(i) + " - " + vector2.getSingleValue(i) + " ist negativ");
|
||||||
|
}
|
||||||
|
result.setSingleValue((this.getSingleValue(i) - vector2.getSingleValue(i)), i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double dotProduct(PositiveVector vector2) {
|
||||||
|
double result = 0;
|
||||||
|
|
||||||
|
if (this.length() == vector2.length()) {
|
||||||
|
for (int i = 0; i < this.length(); i++) {
|
||||||
|
result += this.getSingleValue(i) * vector2.getSingleValue(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args){
|
||||||
|
PositiveVector vector1 = new PositiveVector(3);
|
||||||
|
PositiveVector vector2 = new PositiveVector(3);
|
||||||
|
|
||||||
|
vector1.setValues(new double[] {1,0,5});
|
||||||
|
vector2.setValues(new double[] {0,0,1});
|
||||||
|
|
||||||
|
PositiveVector vector3 = vector1.vectorSubtraction(vector2);
|
||||||
|
vector3.print();
|
||||||
|
|
||||||
|
System.out.println(vector1.dotProduct(vector2));
|
||||||
|
}
|
||||||
|
}
|
7
src/part10/placeholder.java
Normal file
7
src/part10/placeholder.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package part10;
|
||||||
|
|
||||||
|
public class placeholder {
|
||||||
|
public static void main(String[] args){
|
||||||
|
System.out.println("Sup nerd!");
|
||||||
|
}
|
||||||
|
}
|
@@ -9,7 +9,7 @@ public class aufg3 {
|
|||||||
// =====================
|
// =====================
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
int MAXIMUM = 10_000_000; // takes about 13 seconds(!!)
|
int MAXIMUM = 100_000_000; // takes about 4 seconds(!!)
|
||||||
int MAX_PER_ROW = 20;
|
int MAX_PER_ROW = 20;
|
||||||
|
|
||||||
boolean[] primes = new boolean[MAXIMUM];
|
boolean[] primes = new boolean[MAXIMUM];
|
||||||
@@ -17,7 +17,7 @@ public class aufg3 {
|
|||||||
Arrays.fill(primes, true);
|
Arrays.fill(primes, true);
|
||||||
|
|
||||||
for(int i=2; i < MAXIMUM * 0.5; i++){
|
for(int i=2; i < MAXIMUM * 0.5; i++){
|
||||||
for(int j=2; j < ((MAXIMUM*1.0)/i); j++){
|
for(int j=i; j < ((MAXIMUM*1.0)/i); j++){
|
||||||
|
|
||||||
int index = i * j;
|
int index = i * j;
|
||||||
|
|
||||||
|
@@ -21,12 +21,12 @@ public class aufg1 {
|
|||||||
result = 0;
|
result = 0;
|
||||||
} else if (number < 2) {
|
} else if (number < 2) {
|
||||||
result = 1;
|
result = 1;
|
||||||
}else if (number == 10) { // Optimization1
|
// }else if (number == 10) { // Optimization1
|
||||||
result = 89;
|
// result = 89;
|
||||||
}else if (number == 20) { // Optimization2
|
// }else if (number == 20) { // Optimization2
|
||||||
result = 10946;
|
// result = 10946;
|
||||||
}else if (number == 30) { // Optimization3
|
// }else if (number == 30) { // Optimization3
|
||||||
result = 1346269;
|
// result = 1346269;
|
||||||
}else {
|
}else {
|
||||||
result = fibonacciRec(number-1) + fibonacciRec(number-2);
|
result = fibonacciRec(number-1) + fibonacciRec(number-2);
|
||||||
}
|
}
|
||||||
@@ -34,11 +34,27 @@ public class aufg1 {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static int fiboTailRec(int number){
|
||||||
|
return helper(++number, new int[] {0,1});
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int helper(int number, int[] result){
|
||||||
|
if (number < 0){
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (number < 2){
|
||||||
|
return result[number];
|
||||||
|
}
|
||||||
|
|
||||||
|
return helper(number-1, new int[] {result[1], result[0] + result[1]});
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] agrs){
|
public static void main(String[] agrs){
|
||||||
// Fib(47) causes an Integer Overflow
|
// Fib(47) causes an Integer Overflow
|
||||||
for (int i=0; i<46; i++) {
|
for (int i=0; i<46; i++) {
|
||||||
System.out.println(fibonacciIter(i));
|
System.out.println("i " + i + ": " + fibonacciIter(i));
|
||||||
System.out.println(fibonacciRec(i));
|
System.out.println("i " + i + ": " + fibonacciRec(i));
|
||||||
|
System.out.println("i " + i + ": " + fiboTailRec(i));
|
||||||
System.out.println("==========");
|
System.out.println("==========");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
13
src/part5/aufg2/Demo.java
Normal file
13
src/part5/aufg2/Demo.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package part5.aufg2;
|
||||||
|
|
||||||
|
public class Demo {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Queenproblem Q = new Queenproblem();
|
||||||
|
Q.solve();
|
||||||
|
System.out.println("====================================\n");
|
||||||
|
Q.setField(new int[]{0,0,0,0,0,0,0,0});
|
||||||
|
Q.solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
146
src/part5/aufg2/Queenproblem.java
Normal file
146
src/part5/aufg2/Queenproblem.java
Normal file
@@ -0,0 +1,146 @@
|
|||||||
|
package part5.aufg2;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
public class Queenproblem {
|
||||||
|
// 0 means empty
|
||||||
|
// 1-8 is the y position of the Queen
|
||||||
|
int[] field;
|
||||||
|
|
||||||
|
public Queenproblem(){
|
||||||
|
this.field = new int[8];
|
||||||
|
makeBoardEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setField(int[] field) {
|
||||||
|
this.field = field;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void makeBoardEmpty(){
|
||||||
|
this.setField(new int[] {0,0,0,0,0,0,0,0});
|
||||||
|
}
|
||||||
|
|
||||||
|
private int[][] make2D(){
|
||||||
|
int[][] field = new int[8][8];
|
||||||
|
|
||||||
|
for (int[] row : field ){
|
||||||
|
Arrays.fill(row, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i=0; i<8; i++) {
|
||||||
|
try {
|
||||||
|
int yPosition = this.field[i];
|
||||||
|
field[yPosition-1][i] = 1; // -1 to handle 0 as empty and 8 as lowest possible with actual index 7
|
||||||
|
}catch (IndexOutOfBoundsException e){/* System.out.println("Ignored Index Error while making it 2D") */}
|
||||||
|
}
|
||||||
|
|
||||||
|
return field;
|
||||||
|
}
|
||||||
|
public void print(){
|
||||||
|
int[][] field = make2D();
|
||||||
|
|
||||||
|
for (int[] row:field) {
|
||||||
|
System.out.println(Arrays.toString(row));
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkVertical(int col){
|
||||||
|
return this.field[col - 1] == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkHorizontal(int row){
|
||||||
|
for (int colFilled : this.field) {
|
||||||
|
if (colFilled == row){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureLegalCords(int row, int col){
|
||||||
|
if (col<1 || col>8){
|
||||||
|
throw new RuntimeException("Invalid Col " + col);
|
||||||
|
}
|
||||||
|
if (row<1 || row>8){
|
||||||
|
throw new RuntimeException("Invalid Row " + row);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public boolean checkFallingDiagonal(int row, int col){
|
||||||
|
ensureLegalCords(row, col);
|
||||||
|
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
try {
|
||||||
|
if (this.field[col-1+i] != 0) {
|
||||||
|
if (this.field[col - 1 + i] == row + i) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (IndexOutOfBoundsException e){/* Skip Iteration with IndexError */}
|
||||||
|
|
||||||
|
// cannot be in the same try block.
|
||||||
|
// Exceptions in the + direction stop execution of - prematurely
|
||||||
|
try {
|
||||||
|
if (this.field[col-1-i] != 0) {
|
||||||
|
if (this.field[col - 1 - i] == row - i) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (IndexOutOfBoundsException e){/* Skip Iteration with IndexError*/ }
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkRisingDiagonal(int row, int col){
|
||||||
|
ensureLegalCords(row, col);
|
||||||
|
|
||||||
|
for (int i = 0; i < 8; i++) {
|
||||||
|
try {
|
||||||
|
if (this.field[col-1+i] != 0) {
|
||||||
|
if (this.field[col-1+i] == row-i) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (IndexOutOfBoundsException e){}
|
||||||
|
try {
|
||||||
|
if (this.field[col-1-i] != 0) {
|
||||||
|
if (this.field[col-1-i] == row+i) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}catch (IndexOutOfBoundsException e){}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
public boolean isPossible(int row, int col){
|
||||||
|
if(!checkHorizontal(row)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!checkVertical(col)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!checkFallingDiagonal(row, col)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!checkRisingDiagonal(row, col)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void solve(){
|
||||||
|
for (int col = 1; col < 9; col++) {
|
||||||
|
if (this.field[col-1] == 0) {
|
||||||
|
for (int row = 1; row < 9; row++) {
|
||||||
|
if (isPossible(row,col)) {
|
||||||
|
this.field[col-1] = row;
|
||||||
|
this.solve();
|
||||||
|
this.field[col-1] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.print();
|
||||||
|
}
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
package part5;
|
package part5.aufg2;
|
||||||
|
|
||||||
// I know this is not recursive
|
// I know this is not recursive
|
||||||
// Shut up
|
// Shut up
|
@@ -10,7 +10,7 @@ public class aufg3 {
|
|||||||
public static void recursive2(int depth){
|
public static void recursive2(int depth){
|
||||||
int[] array = new int[100];
|
int[] array = new int[100];
|
||||||
System.out.println(depth);
|
System.out.println(depth);
|
||||||
recursive(depth+1);
|
recursive2(depth+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void recursive3(int depth){
|
public static void recursive3(int depth){
|
||||||
|
@@ -1,5 +1,10 @@
|
|||||||
package part8.aufg3;
|
package part8.aufg3;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* No Exception Handling in this version.
|
||||||
|
* Variante with 'some' is in "part10/aufg1"
|
||||||
|
*/
|
||||||
|
|
||||||
public class matrix {
|
public class matrix {
|
||||||
private int numberOfRows;
|
private int numberOfRows;
|
||||||
private int numberOfCols;
|
private int numberOfCols;
|
||||||
@@ -47,10 +52,6 @@ public class matrix {
|
|||||||
this.setValue(row,i,values[i]);
|
this.setValue(row,i,values[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
//
|
|
||||||
// Amount of given Values is not compatible Exception
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int[] getCol(int col){
|
public int[] getCol(int col){
|
||||||
@@ -69,10 +70,6 @@ public class matrix {
|
|||||||
this.setValue(i, col, values[i]);
|
this.setValue(i, col, values[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
//
|
|
||||||
// Amount of given Values is not compatible Exception
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void scalarMultiply(int factor){
|
void scalarMultiply(int factor){
|
||||||
@@ -122,10 +119,7 @@ public class matrix {
|
|||||||
result += vector1[i] * vector2[i];
|
result += vector1[i] * vector2[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// Vectors not compatible Exception
|
|
||||||
// TODO
|
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
55
src/part9/Fibonacci.java
Normal file
55
src/part9/Fibonacci.java
Normal 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
12
src/part9/aufg3
Normal 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.
|
Reference in New Issue
Block a user