Skip to content

Commit 2fddc67

Browse files
committed
update tasks;
1 parent 21552e4 commit 2fddc67

File tree

14 files changed

+813
-13
lines changed

14 files changed

+813
-13
lines changed
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
package de.dhbwka.exercise.arrays;
22

3+
import de.dhbwka.exercise.utility.ScannerUtility;
4+
35
import java.util.Arrays;
46

57
/**
68
* @author Leonhard Gahr
79
*/
810
public class Fibonacci {
911
public static void main(String[] args) {
10-
for (int num : getFibonacci()) {
11-
System.out.println(num);
12-
}
12+
Arrays.stream(getFibonacci()).forEach(System.out::println);
1313
}
1414

1515
private static int[] getFibonacci() {
16-
return getFibonacci(new int[] {0, 1}, 20);
16+
return getFibonacci(new int[]{1, 1}, ScannerUtility.getInt("Please enter depth: "));
1717
}
1818

1919
private static int[] getFibonacci(int[] nums, final int LIMIT) {
20-
final int N = nums.length;
21-
if (N >= LIMIT) {
22-
return nums;
23-
}
24-
int[] newNums = append(nums, nums[N - 2] + nums[N - 1]);
20+
if (nums.length >= LIMIT) return nums;
21+
int[] newNums = append(nums, nums[nums.length - 2] + nums[nums.length - 1]);
2522
return getFibonacci(newNums, LIMIT);
2623
}
2724

2825
private static int[] append(int[] arr, int element) {
29-
final int N = arr.length;
30-
arr = Arrays.copyOf(arr, N + 1);
31-
arr[N] = element;
32-
return arr;
26+
int[] arr2 = Arrays.copyOf(arr, arr.length + 1);
27+
arr2[arr.length] = element;
28+
return arr2;
3329
}
3430
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package de.dhbwka.exercise.classes;
2+
3+
import de.dhbwka.exercise.utility.Sort;
4+
import lombok.Getter;
5+
6+
import java.util.Arrays;
7+
import java.util.concurrent.ThreadLocalRandom;
8+
import java.util.stream.IntStream;
9+
10+
/**
11+
* @author Leonhard Gahr
12+
*/
13+
14+
@Getter
15+
public class Complex implements Comparable {
16+
private final double REAL;
17+
private final double IMAG;
18+
19+
private final double MAGNITUDE;
20+
21+
public Complex(double real, double imag) {
22+
this.REAL = real;
23+
this.IMAG = imag;
24+
25+
this.MAGNITUDE = Math.hypot(real, imag);
26+
}
27+
28+
public Complex() {
29+
this(0, 0);
30+
}
31+
32+
public Complex add(Complex complex) {
33+
return new Complex(REAL + complex.REAL, IMAG + complex.IMAG);
34+
}
35+
36+
public Complex subtract(Complex complex) {
37+
return new Complex(REAL - complex.REAL, IMAG - complex.IMAG);
38+
}
39+
40+
public Complex mult(Complex complex) {
41+
return new Complex(REAL * complex.REAL - IMAG * complex.IMAG, REAL * complex.IMAG + IMAG * complex.REAL);
42+
}
43+
44+
public Complex div(Complex complex) {
45+
double newReal = (REAL * complex.REAL + IMAG * complex.IMAG) / (Math.pow(complex.REAL, 2) + Math.pow(complex.IMAG, 2));
46+
double newImag = ((IMAG * complex.REAL) - (REAL * complex.IMAG)) / (Math.pow(complex.REAL, 2) + Math.pow(complex.IMAG, 2));
47+
return new Complex(newReal, newImag);
48+
}
49+
50+
public boolean isLess(Complex complex) {
51+
return MAGNITUDE < complex.MAGNITUDE;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return String.format("%.3f + %.3fi", REAL, IMAG);
57+
}
58+
59+
@Override
60+
public int compareTo(Object o) {
61+
return Double.compare(MAGNITUDE, ((Complex) o).getMAGNITUDE());
62+
}
63+
64+
public static void main(String[] args) {
65+
Complex c1 = new Complex(1, 2);
66+
Complex c2 = new Complex(2, 1);
67+
68+
System.out.println("c1: " + c1);
69+
System.out.println("c2: " + c2);
70+
71+
System.out.println("c1 + c2: " + c1.add(c2));
72+
System.out.println("c1 - c2: " + c1.subtract(c2));
73+
System.out.println("c1 * c2: " + c1.mult(c2));
74+
System.out.println("c1 / c2: " + c1.div(c2));
75+
System.out.println("c1 < c2: " + c1.isLess(c2));
76+
77+
78+
System.out.printf("%n%n");
79+
80+
Complex[] comps = new Complex[10];
81+
ThreadLocalRandom random = ThreadLocalRandom.current();
82+
IntStream.range(0, 10).forEach(i -> comps[i] = new Complex(random.nextDouble(0, 20), random.nextDouble(0, 20)));
83+
84+
85+
System.out.println("Unordered:");
86+
Arrays.stream(comps).forEach(v -> System.out.printf("%s magnitude: %.3f %n", v.toString(), v.getMAGNITUDE()));
87+
88+
Sort.bogoSort(comps);
89+
90+
System.out.println("Ordered:");
91+
Arrays.stream(comps).forEach(v -> System.out.printf("%s magnitude: %.3f %n", v.toString(), v.getMAGNITUDE()));
92+
93+
}
94+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package de.dhbwka.exercise.classes;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author Leonhard Gahr
7+
*/
8+
public class Horner {
9+
private final double[] FACTORS;
10+
11+
public Horner() {
12+
this(0);
13+
}
14+
15+
public Horner(double... factors) {
16+
FACTORS = factors;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
StringBuilder s = new StringBuilder();
22+
for (int i = 0; i < FACTORS.length; i++) {
23+
s.append(FACTORS[i] < 0 ? " - " : " + ").append(Math.abs(FACTORS[i])).append("x^").append(FACTORS.length - i - 1);
24+
}
25+
if (FACTORS[FACTORS.length - 1] >= 0) {
26+
s.delete(0, 3);
27+
}
28+
return s.toString().trim();
29+
}
30+
31+
public double getValue(double x) {
32+
return getValue(x, FACTORS);
33+
}
34+
35+
private double getValue(double x, double[] factors) {
36+
return factors.length == 1 ? factors[0] : factors[factors.length - 1] + x * getValue(x, Arrays.copyOf(factors, factors.length - 1));
37+
}
38+
39+
40+
public static void main(String[] args) {
41+
System.out.println(new Horner(0.5, -3, 2, 4, 3, -10, 8, 4.5, 3, -2, 1));
42+
System.out.println(new Horner(0.5, -3, 2, 4, 3, -10, 8, 4.5, 3, -2, 1).getValue(1.5));
43+
}
44+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package de.dhbwka.exercise.classes;
2+
3+
import de.dhbwka.exercise.utility.ScannerUtility;
4+
import de.dhbwka.exercise.utility.Sort;
5+
6+
import java.util.Arrays;
7+
import java.util.Random;
8+
import java.util.stream.IntStream;
9+
10+
/**
11+
* @author Leonhard Gahr
12+
*/
13+
public class Lotto {
14+
private final int M;
15+
private final int N;
16+
17+
private int[] tip;
18+
private int[] drawn;
19+
20+
public Lotto(int m, int n) {
21+
M = m;
22+
N = n;
23+
}
24+
25+
public void tip() {
26+
tip = new int[M];
27+
// anonymous object for lambda modification
28+
var reference = new Object() {
29+
int[] allowed = IntStream.range(M, N + 1).toArray();
30+
};
31+
IntStream.range(0, M).forEach(i -> {
32+
tip[i] = ScannerUtility.getInt(String.format("Tip #%d: ", i + 1), reference.allowed);
33+
reference.allowed = Arrays.stream(reference.allowed).filter(x -> x != tip[i]).toArray();
34+
});
35+
36+
Sort.bogoSort(tip);
37+
}
38+
39+
private void tip(int[] tip) {
40+
Sort.bogoSort(tip);
41+
this.tip = tip;
42+
}
43+
44+
/**
45+
* Draw random numbers with no duplicates at the size of this.M
46+
*/
47+
public void draw() {
48+
drawn = new Random().ints(M, N + 1).distinct().limit(M).toArray();
49+
Sort.bogoSort(drawn);
50+
}
51+
52+
/**
53+
* Determine the amount correctly guessed numbers in n log n time
54+
*
55+
* @return the number of corrects (max this.M)
56+
*/
57+
public int rights() {
58+
int rights = 0;
59+
int i = 0;
60+
for (int t : tip) {
61+
for (; i < drawn.length; i++) {
62+
if (t == drawn[i]) {
63+
rights++;
64+
} else if (t < drawn[i]) {
65+
break;
66+
}
67+
}
68+
}
69+
return rights;
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return tip == null ? "" : ("Tip: " + String.join(" ", Arrays.stream(tip).mapToObj(String::valueOf).toArray(String[]::new))) +
75+
(drawn == null ? "" : ("\nDrawn: " + String.join(" ", Arrays.stream(drawn).mapToObj(String::valueOf).toArray(String[]::new))));
76+
}
77+
78+
public static void main(String[] args) {
79+
Lotto lotto = new Lotto(6, 49);
80+
lotto.tip();
81+
System.out.println(lotto);
82+
lotto.draw();
83+
System.out.println(lotto);
84+
System.out.println(lotto.rights());
85+
}
86+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package de.dhbwka.exercise.classes;
2+
3+
import de.dhbwka.exercise.utility.ScannerUtility;
4+
5+
import java.util.Random;
6+
import java.util.stream.Collectors;
7+
8+
/**
9+
* @author Leonhard Gahr
10+
*/
11+
public class MasterMind {
12+
/**
13+
* Play the mastermind game
14+
*
15+
* @param tries the maximum amount of tries allowed
16+
* @return whether the player won or not
17+
*/
18+
public static boolean play(int tries) {
19+
String toGuess = randomString();
20+
StringBuilder hints = new StringBuilder();
21+
char[] allowed = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
22+
int n = 0;
23+
String input;
24+
while (n < tries && !(input = ScannerUtility.getString("Enter hier: ", allowed, 5)).equals(toGuess)) {
25+
hints.append(input).append(" ").append(String.join(" ", getHint(toGuess, input))).append("\n");
26+
System.out.println(hints);
27+
n++;
28+
}
29+
30+
if (n == tries) {
31+
System.out.printf("You're to stupid for this game and lost after %d tries", n);
32+
return false;
33+
} else {
34+
System.out.printf("You won after %d tries", n);
35+
return true;
36+
}
37+
}
38+
39+
/**
40+
* Generate a random string with the length of 5 between the letters 'a' amd 'h' (inclusive)
41+
*
42+
* @return the generated string
43+
*/
44+
private static String randomString() {
45+
return new Random().ints(5, 97, 105).mapToObj(x -> Character.toString((char) x)).collect(Collectors.joining());
46+
}
47+
48+
/**
49+
* Generate the hints for the game. the first value is the number of correct letters at the correct indices,
50+
* the second the correct letters at wrong indices, excluding the first ones
51+
*
52+
* @param s1 the string to guess
53+
* @param s2 the guessed string
54+
* @return the values as strings
55+
*/
56+
private static String[] getHint(String s1, String s2) {
57+
char[] c1 = s1.toCharArray();
58+
char[] c2 = s2.toCharArray();
59+
60+
int[] result = new int[2];
61+
62+
for (int i = 0; i < c1.length; i++) {
63+
if (c1[i] == c2[i]) {
64+
c1[i] = '\0';
65+
c2[i] = '\0';
66+
result[0]++;
67+
}
68+
}
69+
70+
for (int i = 0; i < c1.length; i ++){
71+
if(c1[i] == '\0') continue;
72+
for (int y = 0; y < c2.length; y++) {
73+
if (c2[y]=='\0') continue;
74+
if (c1[i] == c2[y]) {
75+
result[1] ++;
76+
c1[i] = '\0';
77+
c2[y] = '\0';
78+
}
79+
}
80+
}
81+
82+
83+
return new String[]{String.valueOf(result[0]), String.valueOf(result[1])};
84+
}
85+
86+
public static boolean play() {
87+
return play(20);
88+
}
89+
90+
public static void main(String[] args) {
91+
MasterMind.play();
92+
}
93+
}

0 commit comments

Comments
 (0)