Skip to content

Commit 163c215

Browse files
committed
add some really intelligent sorting algorithms;
1 parent cb2f813 commit 163c215

File tree

9 files changed

+325
-104
lines changed

9 files changed

+325
-104
lines changed

src/de/dhbwka/exercise/classes/Nimmspiel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private void makeMove(int pile, int num) throws OperationNotSupportedException {
6363
*
6464
* @return the name of the winning player
6565
*/
66-
public String play() throws OperationNotSupportedException {
66+
private String play() throws OperationNotSupportedException {
6767
do {
6868
nextPlayer();
6969

src/de/dhbwka/exercise/classes/Polynomial.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,31 @@ public class Polynomial {
1818
private final double B;
1919
private final double C;
2020

21+
/**
22+
* initialize zero polynomial
23+
*/
2124
public Polynomial() {
2225
this(0, 0, 0);
2326
}
2427

28+
/**
29+
* Initialize polynomial with double values
30+
*
31+
* @param a the a
32+
* @param b the b
33+
* @param c the c
34+
*/
2535
public Polynomial(double a, double b, double c) {
2636
this.A = a;
2737
this.B = b;
2838
this.C = c;
2939
}
3040

41+
/**
42+
* Parse a polynomial string
43+
*
44+
* @param polynomial the polynomial string
45+
*/
3146
public Polynomial(String polynomial) {
3247
List<Double> as = new ArrayList<>();
3348
List<Double> bs = new ArrayList<>();
@@ -78,7 +93,12 @@ public Polynomial multiply(double d) {
7893
return new Polynomial(A * d, B * d, C * d);
7994
}
8095

81-
public double[] roots () {
96+
/**
97+
* calculate roots of the polynomial
98+
*
99+
* @return the roots as array of length 2
100+
*/
101+
public double[] roots() {
82102
double[] roots = new double[2];
83103
if (A == 0) {
84104
if (B == 0) {
@@ -89,8 +109,8 @@ public double[] roots () {
89109
} else {
90110
final double D = (B * B) - (4 * A * C);
91111
if (D >= 0) {
92-
roots[0] = (-B + Math.sqrt(D))/(2*A);
93-
roots[1] = (-B - Math.sqrt(D))/(2*A);
112+
roots[0] = (-B + Math.sqrt(D)) / (2 * A);
113+
roots[1] = (-B - Math.sqrt(D)) / (2 * A);
94114
} else {
95115
return null;
96116
}
@@ -108,7 +128,7 @@ private String doubleToString(double d) {
108128
}
109129

110130
public static void main(String[] args) {
111-
Polynomial p1 = new Polynomial("2.0x^2 +0.0x +0.0");
131+
Polynomial p1 = new Polynomial("2.0x^2 + 0.0x + 0.0");
112132
System.out.println("p1: " + p1);
113133

114134
Polynomial p2 = new Polynomial("0.0x^2 -4.0x +1.0");

src/de/dhbwka/exercise/strings/CrossTotal.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
public class CrossTotal {
99
public static void main(String[] args) {
10-
System.out.printf("Crosstotal is %d", ScannerUtility.getString("Please enter number: ").chars().map(x -> x -= 48).sum());
10+
final char[] ALLOWED = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
11+
final String USER_INPUT = ScannerUtility.getString("Please enter number: ", ALLOWED);
12+
System.out.printf("Crosstotal is %d", USER_INPUT.chars().map(Character::getNumericValue).sum());
1113
}
1214
}

src/de/dhbwka/exercise/strings/Palindrome.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
public class Palindrome {
99
public static void main(String[] args) {
10-
String s = ScannerUtility.getString("Please enter string: ");
11-
String s2 = new StringBuilder(s).reverse().toString();
12-
System.out.printf("Reversed: %s%n%s is%s a palindrome", s2, s, s.toLowerCase().equals(s2.toLowerCase()) ? "" : "n't");
10+
final String INPUT = ScannerUtility.getString("Please enter string: ");
11+
final String REVERSED = new StringBuffer(INPUT).reverse().toString();
12+
System.out.printf("Reversed: '%s'%n'%s' is%s a palindrome", REVERSED, INPUT, INPUT.equalsIgnoreCase(REVERSED) ? "" : "n't");
1313
}
1414
}

src/de/dhbwka/exercise/strings/RomanNumber.java

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,51 @@
11
package de.dhbwka.exercise.strings;
22

3-
import java.util.HashMap;
4-
53
/**
64
* @author Leonhard Gahr
75
*/
86
public class RomanNumber {
9-
static HashMap<Character, Integer> ref = new HashMap<>();
10-
11-
static {
12-
ref.put('I', 1);
13-
ref.put('V', 5);
14-
ref.put('X', 10);
15-
ref.put('L', 50);
16-
ref.put('C', 100);
17-
ref.put('D', 500);
18-
ref.put('M', 1000);
19-
}
20-
21-
public static int convertRoman(String roman) {
22-
int number = 0;
23-
int prev = 0;
24-
25-
for (int i = roman.length() - 1; i >= 0; i--) {
26-
int temp = ref.get(roman.charAt(i));
27-
28-
if (temp < prev) {
29-
number -= temp;
30-
} else {
31-
number += temp;
32-
}
33-
prev = temp;
7+
/**
8+
* get the numeric value for a single roman number
9+
*
10+
* @param LETTER the roman LETTER
11+
* @return the integer value
12+
*/
13+
private static int decode(final int LETTER) {
14+
switch (LETTER) {
15+
case 'M':
16+
return 1000;
17+
case 'D':
18+
return 500;
19+
case 'C':
20+
return 100;
21+
case 'L':
22+
return 50;
23+
case 'X':
24+
return 10;
25+
case 'V':
26+
return 5;
27+
case 'I':
28+
return 1;
29+
default:
30+
return 0;
3431
}
32+
}
3533

36-
return number;
34+
/**
35+
* Convert roman number string to an integer
36+
*
37+
* @param ROMAN the roman number
38+
* @return the calculated integer
39+
*/
40+
private static int convertRoman(final String ROMAN) {
41+
final int[] prev = {0};
42+
43+
// reverse string, iterate over the chars, map to the actual integer values and return the sum
44+
return new StringBuilder(ROMAN).reverse().chars().map(RomanNumber::decode).map(n -> {
45+
int tmp = n < prev[0] ? -n : n;
46+
prev[0] = n;
47+
return tmp;
48+
}).sum();
3749
}
3850

3951
public static void main(String[] args) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package de.dhbwka.exercise.strings;
2+
3+
import de.dhbwka.exercise.utility.Sort;
4+
5+
import java.util.Arrays;
6+
7+
/**
8+
* @author Leonhard Gahr
9+
*/
10+
public class StartParameterProcessor {
11+
StartParameterProcessor(String... strings) {
12+
Sort.bogoBogoSort(strings);
13+
14+
for (int i = 0; i < strings.length; i++) {
15+
strings[i] = strings[i].replaceAll("ü", "ue")
16+
.replaceAll("ä", "ae")
17+
.replaceAll("ö", "oe")
18+
.replaceAll("ß", "ss");
19+
strings[i] = strings[i].replaceAll("Ü(?=[a-z ])", "Ue")
20+
.replaceAll("Ö(?=[a-z ])", "Oe")
21+
.replaceAll("Ä(?=[a-z ])", "Ae")
22+
.replaceAll("ẞ(?=[a-z ])", "Ss");
23+
strings[i] = strings[i].replaceAll("Ä", "AE")
24+
.replaceAll("Ö", "OE")
25+
.replaceAll("Ü", "UE")
26+
.replaceAll("ẞ", "SS");
27+
}
28+
System.out.println(Arrays.toString(strings));
29+
}
30+
31+
public static void main(String[] args) {
32+
new StartParameterProcessor(args);
33+
}
34+
}

src/de/dhbwka/exercise/utility/ScannerUtility.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.util.Scanner;
77

88
/**
9+
* Helper class for scanner operations
10+
*
911
* @author Leonhard Gahr
1012
*/
1113
public class ScannerUtility {
@@ -23,20 +25,31 @@ public static int[] getIntArray(String message, int N) {
2325
return values;
2426
}
2527

28+
/**
29+
* Get a string from the user with a specific message and only the allowed characters
30+
*
31+
* @param message the message to display before the input
32+
* @param allowed the allowed characters
33+
* @return the user's input string
34+
*/
35+
public static String getString(String message, char[] allowed) {
36+
return getString(message, allowed, -1);
37+
}
38+
2639
public static String getString(String message, char[] allowed, int len) {
2740
while (true) {
2841
Scanner scanner = new Scanner(System.in);
2942
System.out.print(message);
3043

3144
String result = scanner.nextLine().toLowerCase();
3245
if (result.chars().allMatch(c -> new String(allowed).indexOf(c) != -1)) {
33-
if (result.length() != len) {
46+
if (len != -1 && result.length() != len) {
3447
System.out.printf("Illegal input. Must be exactly %d characters.%n", len);
3548
continue;
3649
}
3750
return result;
3851
} else {
39-
System.out.printf("Illegal input. Mut be one of (%s)%n", String.join(", ", CharBuffer.wrap(allowed).chars().mapToObj(intValue -> String.valueOf((char) intValue)).toArray(String[]::new)));
52+
System.out.printf("Illegal input. Allowed are (%s)%n", String.join(", ", CharBuffer.wrap(allowed).chars().mapToObj(intValue -> String.valueOf((char) intValue)).toArray(String[]::new)));
4053
}
4154
}
4255
}

0 commit comments

Comments
 (0)