Up to H09 inclusive
This commit is contained in:
parent
8a317c2e73
commit
0b43040d7a
57 changed files with 912 additions and 187 deletions
7
.idea/codeStyles/Project.xml
generated
Normal file
7
.idea/codeStyles/Project.xml
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<code_scheme name="Project" version="173">
|
||||||
|
<ScalaCodeStyleSettings>
|
||||||
|
<option name="MULTILINE_STRING_CLOSING_QUOTES_ON_NEW_LINE" value="true" />
|
||||||
|
</ScalaCodeStyleSettings>
|
||||||
|
</code_scheme>
|
||||||
|
</component>
|
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
generated
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||||
|
</state>
|
||||||
|
</component>
|
0
.idea/misc.xml → .idea/sbt.xml
generated
0
.idea/misc.xml → .idea/sbt.xml
generated
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
71
.idea/workspace.xml
generated
71
.idea/workspace.xml
generated
|
@ -3,11 +3,70 @@
|
||||||
<component name="AutoImportSettings">
|
<component name="AutoImportSettings">
|
||||||
<option name="autoReloadType" value="SELECTIVE" />
|
<option name="autoReloadType" value="SELECTIVE" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent">{
|
<component name="ChangeListManager">
|
||||||
"keyToString": {
|
<list default="true" id="b8100c61-8e50-490a-9a7c-6730a500f60c" name="Changes" comment="" />
|
||||||
"project.structure.last.edited": "Project",
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
"project.structure.proportion": "0.0",
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
"project.structure.side.proportion": "0.0"
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
|
<option name="LAST_RESOLUTION" value="IGNORE" />
|
||||||
|
</component>
|
||||||
|
<component name="Git.Settings">
|
||||||
|
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectCodeStyleSettingsMigration">
|
||||||
|
<option name="version" value="2" />
|
||||||
|
</component>
|
||||||
|
<component name="ProjectColorInfo"><![CDATA[{
|
||||||
|
"associatedIndex": 7
|
||||||
|
}]]></component>
|
||||||
|
<component name="ProjectId" id="2uPCCpmfd5kfOW2JWEb0jQXA4Qs" />
|
||||||
|
<component name="ProjectViewState">
|
||||||
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
|
<option name="showLibraryContents" value="true" />
|
||||||
|
</component>
|
||||||
|
<component name="PropertiesComponent"><![CDATA[{
|
||||||
|
"keyToString": {
|
||||||
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
|
"RunOnceActivity.git.unshallow": "true",
|
||||||
|
"git-widget-placeholder": "master",
|
||||||
|
"last_opened_file_path": "/home/osh/Desktop/FOP-2425-Marathon/H06",
|
||||||
|
"nodejs_package_manager_path": "npm",
|
||||||
|
"project.structure.last.edited": "Project",
|
||||||
|
"project.structure.proportion": "0.0",
|
||||||
|
"project.structure.side.proportion": "0.0",
|
||||||
|
"vue.rearranger.settings.migration": "true"
|
||||||
}
|
}
|
||||||
}</component>
|
}]]></component>
|
||||||
|
<component name="RunManager">
|
||||||
|
<configuration default="true" type="JetRunConfigurationType">
|
||||||
|
<module name="FOP-2425-Marathon" />
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
<configuration default="true" type="KotlinStandaloneScriptRunConfigurationType">
|
||||||
|
<module name="FOP-2425-Marathon" />
|
||||||
|
<option name="filePath" />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
|
<component name="SharedIndexes">
|
||||||
|
<attachedChunks>
|
||||||
|
<set>
|
||||||
|
<option value="bundled-jdk-9823dce3aa75-a94e463ab2e7-intellij.indexing.shared.core-IU-243.25659.39" />
|
||||||
|
</set>
|
||||||
|
</attachedChunks>
|
||||||
|
</component>
|
||||||
|
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
|
||||||
|
<component name="TaskManager">
|
||||||
|
<task active="true" id="Default" summary="Default task">
|
||||||
|
<changelist id="b8100c61-8e50-490a-9a7c-6730a500f60c" name="Changes" comment="" />
|
||||||
|
<created>1742140697920</created>
|
||||||
|
<option name="number" value="Default" />
|
||||||
|
<option name="presentableId" value="Default" />
|
||||||
|
<updated>1742140697920</updated>
|
||||||
|
<workItem from="1742140700363" duration="14000" />
|
||||||
|
</task>
|
||||||
|
<servers />
|
||||||
|
</component>
|
||||||
</project>
|
</project>
|
|
@ -1,5 +1,9 @@
|
||||||
package h06;
|
package h06;
|
||||||
|
|
||||||
|
import h06.problems.Fractals;
|
||||||
|
import h06.ui.FractalDrawer;
|
||||||
|
import h06.ui.FractalVisualizer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main entry point in executing the program.
|
* Main entry point in executing the program.
|
||||||
*/
|
*/
|
||||||
|
@ -10,6 +14,7 @@ public class Main {
|
||||||
* @param args program arguments, currently ignored
|
* @param args program arguments, currently ignored
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
var inst = Fractals.kochSnowflake(3);
|
||||||
|
new FractalVisualizer(inst, 60).setVisible(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,12 +41,12 @@ public class Fibonacci {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static int fibonacciRecursiveDifferent(int n) {
|
public static int fibonacciRecursiveDifferent(int n) {
|
||||||
return crash(); // TODO: H1.1 - remove if implemented
|
return doTheRecursion(0, 1, n);
|
||||||
}
|
}
|
||||||
|
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
private static int doTheRecursion(int a, int b, int n) {
|
private static int doTheRecursion(int a, int b, int n) {
|
||||||
return crash(); // TODO: H1.1 - remove if implemented
|
return n <= 0 ? a : doTheRecursion(b, a+b, n-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,6 +57,15 @@ public class Fibonacci {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static int fibonacciIterative(int n) {
|
public static int fibonacciIterative(int n) {
|
||||||
return crash(); // TODO: H1.2 - remove if implemented
|
var a = 0;
|
||||||
|
var b = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
var t = a+b;
|
||||||
|
a = b;
|
||||||
|
b = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
return a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,14 @@ import h06.ui.DrawInstruction;
|
||||||
import org.tudalgo.algoutils.student.annotation.DoNotTouch;
|
import org.tudalgo.algoutils.student.annotation.DoNotTouch;
|
||||||
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import static org.tudalgo.algoutils.student.Student.crash;
|
import static org.tudalgo.algoutils.student.Student.crash;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,7 +37,7 @@ public class Fractals {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static int pow(int a, int b) {
|
public static int pow(int a, int b) {
|
||||||
return crash(); // TODO: H3.1 - remove if implemented
|
return (int) Math.pow(a, b);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,7 +50,9 @@ public class Fractals {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static DrawInstruction[] concatenate(DrawInstruction[] arr1, DrawInstruction[] arr2) {
|
public static DrawInstruction[] concatenate(DrawInstruction[] arr1, DrawInstruction[] arr2) {
|
||||||
return crash(); // TODO: H3.2 - remove if implemented
|
do {
|
||||||
|
return Stream.of(arr1, arr2).flatMap(Stream::of).toArray(DrawInstruction[]::new); // TODO: H3.2 - remove if implemented
|
||||||
|
} while(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,7 +66,29 @@ public class Fractals {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static DrawInstruction[] replaceAtIndex(DrawInstruction[] arr, int idx, DrawInstruction elem) {
|
public static DrawInstruction[] replaceAtIndex(DrawInstruction[] arr, int idx, DrawInstruction elem) {
|
||||||
return crash(); // TODO: H3.3 - remove if implemented
|
do {
|
||||||
|
List<DrawInstruction> x = new ArrayList<>(List.of(arr));
|
||||||
|
x.set(idx, elem);
|
||||||
|
return x.toArray(DrawInstruction[]::new);
|
||||||
|
} while (true);
|
||||||
|
}
|
||||||
|
|
||||||
|
static String fracToString(DrawInstruction[] frac) {
|
||||||
|
return Stream.of(frac).map(String::valueOf).collect(Collectors.joining());
|
||||||
|
}
|
||||||
|
|
||||||
|
static DrawInstruction[] stringToFrac(String string) {
|
||||||
|
return string.chars()
|
||||||
|
.mapToObj(c -> (char) c)
|
||||||
|
.map(String::valueOf)
|
||||||
|
.map(x -> switch (x) {
|
||||||
|
case "D": yield DrawInstruction.DRAW_LINE;
|
||||||
|
case "R": yield DrawInstruction.TURN_RIGHT;
|
||||||
|
case "L": yield DrawInstruction.TURN_LEFT;
|
||||||
|
default:
|
||||||
|
throw new IllegalStateException("Unexpected value: " + x);
|
||||||
|
})
|
||||||
|
.toArray(DrawInstruction[]::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,7 +99,25 @@ public class Fractals {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static DrawInstruction[] dragonCurve(int n) {
|
public static DrawInstruction[] dragonCurve(int n) {
|
||||||
return crash(); // TODO: H3.4 - remove if implemented
|
return stringToFrac(dragonCurveString(n));
|
||||||
|
}
|
||||||
|
public static String dragonCurveString(int n) {
|
||||||
|
if (n < 0) return "D";
|
||||||
|
|
||||||
|
return switch (n) {
|
||||||
|
case 0: yield "D";
|
||||||
|
case 1: yield "DRD";
|
||||||
|
default: yield ((dragonCurveString(n-1) + "R") + stringReplace(dragonCurveString(n-1), pow(2, n-1)-1, "L"));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
static String stringReplace(String a, int n, String b) {
|
||||||
|
return a.substring(0, n) + b + a.substring(n+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static <A> List<A> replaceAll(List<A> list, A a, List<A> newThings) {
|
||||||
|
return list.stream().flatMap(x -> a.equals(x) ? newThings.stream() : Stream.of(x)).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,6 +128,17 @@ public class Fractals {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static DrawInstruction[] kochSnowflake(int n) {
|
public static DrawInstruction[] kochSnowflake(int n) {
|
||||||
return crash(); // TODO: H3.5 - remove if implemented
|
return stringToFrac(kockSnowflakeString(n));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String kockSnowflakeString(int n) {
|
||||||
|
if (n < 0) return "DRRDRRD";
|
||||||
|
|
||||||
|
return switch (n) {
|
||||||
|
case 0: yield "DRRDRRD";
|
||||||
|
default: yield kockSnowflakeString(n-1).replaceAll("D", "DLDRRDLD");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package h06.problems;
|
||||||
|
|
||||||
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
|
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
import static org.tudalgo.algoutils.student.Student.crash;
|
import static org.tudalgo.algoutils.student.Student.crash;
|
||||||
|
|
||||||
public class LinearSearch {
|
public class LinearSearch {
|
||||||
|
@ -15,7 +17,11 @@ public class LinearSearch {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static int linearSearchRecursive(int[] arr, int target) {
|
public static int linearSearchRecursive(int[] arr, int target) {
|
||||||
return crash(); // TODO: H2.1 - remove if implemented
|
linearSearchRecursiveHelper(arr, target, 0);
|
||||||
|
return IntStream.range(0, arr.length)
|
||||||
|
.filter(i -> arr[i] == target)
|
||||||
|
.findFirst()
|
||||||
|
.orElse(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -28,7 +34,7 @@ public class LinearSearch {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static int linearSearchRecursiveHelper(int[] arr, int target, int index) {
|
public static int linearSearchRecursiveHelper(int[] arr, int target, int index) {
|
||||||
return crash(); // TODO: H2.1 - remove if implemented
|
return index >= arr.length ? -1 : (arr[index] == target ? index : linearSearchRecursiveHelper(arr, target, index+1)); // TODO: H2.1 - remove if implemented
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -40,6 +46,16 @@ public class LinearSearch {
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
public static int linearSearchIterative(int[] arr, int target) {
|
public static int linearSearchIterative(int[] arr, int target) {
|
||||||
return crash(); // TODO: H2.2 - remove if implemented
|
for (int i = 0; i < arr.length; i++) {
|
||||||
|
scope : {
|
||||||
|
if (arr[i] == target) {
|
||||||
|
break scope;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
H07/src/main/java/h07/ArithmeticExpression.java
Normal file
6
H07/src/main/java/h07/ArithmeticExpression.java
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package h07;
|
||||||
|
|
||||||
|
public interface ArithmeticExpression {
|
||||||
|
|
||||||
|
NumberExpression evaluate(NumberExpression lhs, NumberExpression rhs);
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package h07;
|
||||||
|
|
||||||
|
import h07.peano.PeanoNumberExpression;
|
||||||
|
|
||||||
|
public interface ConvertNumberToPeanoExpression {
|
||||||
|
|
||||||
|
PeanoNumberExpression convert(NumberExpression e);
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package h07;
|
||||||
|
|
||||||
|
import h07.peano.PeanoNumberExpression;
|
||||||
|
import h07.peano.Successor;
|
||||||
|
import h07.peano.Zero;
|
||||||
|
|
||||||
|
public class ConvertNumberToPeanoExpressionImpl implements ConvertNumberToPeanoExpression {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PeanoNumberExpression convert(NumberExpression e) {
|
||||||
|
var x = e.evaluate();
|
||||||
|
if (x == 0) {
|
||||||
|
return () -> {
|
||||||
|
return new Zero();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return () -> {
|
||||||
|
return new Successor(convert(() -> x-1).evaluate());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package h07;
|
||||||
|
|
||||||
|
import h07.peano.PeanoNumberExpression;
|
||||||
|
|
||||||
|
public interface ConvertPeanoToNumberExpression {
|
||||||
|
|
||||||
|
NumberExpression convert(PeanoNumberExpression e);
|
||||||
|
}
|
|
@ -0,0 +1,22 @@
|
||||||
|
package h07;
|
||||||
|
|
||||||
|
import h07.peano.PeanoNumberExpression;
|
||||||
|
import h07.peano.Successor;
|
||||||
|
|
||||||
|
public class ConvertPeanoToNumberExpressionImpl implements ConvertPeanoToNumberExpression {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberExpression convert(PeanoNumberExpression e) {
|
||||||
|
var x = e.evaluate();
|
||||||
|
|
||||||
|
if (x instanceof Successor s) {
|
||||||
|
return () -> {
|
||||||
|
return 1 + convert(() -> s.predecessor).evaluate();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return () -> {
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,9 @@
|
||||||
package h07;
|
package h07;
|
||||||
|
|
||||||
|
import h07.peano.*;
|
||||||
import org.tudalgo.algoutils.student.annotation.DoNotTouch;
|
import org.tudalgo.algoutils.student.annotation.DoNotTouch;
|
||||||
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
|
|
||||||
import h07.peano.NaturalNumber;
|
|
||||||
import h07.peano.Successor;
|
|
||||||
import h07.peano.Zero;
|
|
||||||
|
|
||||||
import static org.tudalgo.algoutils.student.Student.crash;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main entry point in executing the program.
|
* Main entry point in executing the program.
|
||||||
*/
|
*/
|
||||||
|
@ -39,16 +34,16 @@ public class Main {
|
||||||
@DoNotTouch
|
@DoNotTouch
|
||||||
private static void numberExpressionMultiplicationTableTests() {
|
private static void numberExpressionMultiplicationTableTests() {
|
||||||
// TODO: H2.2 - uncomment to test
|
// TODO: H2.2 - uncomment to test
|
||||||
// int lowerBound = 1;
|
int lowerBound = 1;
|
||||||
// int upperBound = 10;
|
int upperBound = 10;
|
||||||
// NumberExpression[] multiplicationTable = NumberExpressionFactory.multiplicationTable(lowerBound, upperBound);
|
NumberExpression[] multiplicationTable = NumberExpressionFactory.multiplicationTable(lowerBound, upperBound);
|
||||||
//
|
|
||||||
// for (int i = lowerBound; i <= upperBound; i++) {
|
for (int i = lowerBound; i <= upperBound; i++) {
|
||||||
// for (int j = lowerBound; j <= upperBound; j++) {
|
for (int j = lowerBound; j <= upperBound; j++) {
|
||||||
// System.out.printf("| %4s ", multiplicationTable[(i - lowerBound) * (upperBound - lowerBound + 1) + (j - lowerBound)].evaluate());
|
System.out.printf("| %4s ", multiplicationTable[(i - lowerBound) * (upperBound - lowerBound + 1) + (j - lowerBound)].evaluate());
|
||||||
// }
|
}
|
||||||
// System.out.println("|");
|
System.out.println("|");
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final NaturalNumber THREE = new Successor(new Successor(new Successor(new Zero())));
|
private static final NaturalNumber THREE = new Successor(new Successor(new Successor(new Zero())));
|
||||||
|
@ -56,11 +51,36 @@ public class Main {
|
||||||
|
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
private static void peanoNumberExpressionTests() {
|
private static void peanoNumberExpressionTests() {
|
||||||
crash(); // TODO: H3.3 - remove if implemented
|
var ten = new PeanoAddExpression().evaluate(() -> {
|
||||||
|
return THREE;
|
||||||
|
}, () -> {
|
||||||
|
return SEVEN;
|
||||||
|
}).evaluate();
|
||||||
|
System.out.println(ten);
|
||||||
|
|
||||||
|
var _21 = new PeanoMultiplyExpression().evaluate(() -> {
|
||||||
|
return THREE;
|
||||||
|
}, () -> {
|
||||||
|
return SEVEN;
|
||||||
|
}).evaluate();
|
||||||
|
System.out.println(_21);
|
||||||
}
|
}
|
||||||
|
|
||||||
@StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
private static void filterFoldMapTests() {
|
private static void filterFoldMapTests() {
|
||||||
crash(); // TODO: H4.6 - remove if implemented
|
int lowerBound = 1;
|
||||||
|
int upperBound = 10;
|
||||||
|
var timesTable = NumberExpressionFactory.multiplicationTable(lowerBound, upperBound);
|
||||||
|
|
||||||
|
var filtered = NumberExpressionFactory.filter(timesTable, x -> {
|
||||||
|
return x % 3 == 0;
|
||||||
|
});
|
||||||
|
var peano = PeanoNumberExpressionFactory.fromNumberExpressions(filtered);
|
||||||
|
var sum = PeanoNumberExpressionFactory.fold(peano, () -> {
|
||||||
|
return new Zero();
|
||||||
|
}, (e1, e2) -> {
|
||||||
|
return new PeanoAddExpression().evaluate(e1, e2);
|
||||||
|
});
|
||||||
|
System.out.println(new ConvertPeanoToNumberExpressionImpl().convert(sum).evaluate());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
6
H07/src/main/java/h07/NumberExpression.java
Normal file
6
H07/src/main/java/h07/NumberExpression.java
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package h07;
|
||||||
|
|
||||||
|
public interface NumberExpression {
|
||||||
|
|
||||||
|
int evaluate();
|
||||||
|
}
|
|
@ -1,12 +1,11 @@
|
||||||
package h07;
|
package h07;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.function.IntPredicate;
|
import java.util.function.IntPredicate;
|
||||||
|
|
||||||
import org.tudalgo.algoutils.student.annotation.DoNotTouch;
|
import org.tudalgo.algoutils.student.annotation.DoNotTouch;
|
||||||
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
|
|
||||||
import static org.tudalgo.algoutils.student.Student.crash;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A factory class for creating number expressions.
|
* A factory class for creating number expressions.
|
||||||
*/
|
*/
|
||||||
|
@ -18,10 +17,14 @@ public class NumberExpressionFactory {
|
||||||
* @return An array of number expressions representing the result of the
|
* @return An array of number expressions representing the result of the
|
||||||
* multiplication table of the given numbers.
|
* multiplication table of the given numbers.
|
||||||
*/
|
*/
|
||||||
// @StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
// public static NumberExpression[] multiplicationTable(NumberExpression[] numbers) {
|
public static NumberExpression[] multiplicationTable(NumberExpression[] numbers) {
|
||||||
// return crash(); // TODO: H2.1 - remove if implemented
|
return Arrays.stream(numbers)
|
||||||
// }
|
.flatMap(e1 -> Arrays.stream(numbers).<NumberExpression>map(e2 -> {
|
||||||
|
return () -> e1.evaluate() * e2.evaluate();
|
||||||
|
}))
|
||||||
|
.toArray(NumberExpression[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO: H2.2 - uncomment for testing
|
// TODO: H2.2 - uncomment for testing
|
||||||
|
@ -33,20 +36,20 @@ public class NumberExpressionFactory {
|
||||||
* @return An array of number expressions representing the result of the
|
* @return An array of number expressions representing the result of the
|
||||||
* multiplication table of the numbers from lowerBound to upperBound.
|
* multiplication table of the numbers from lowerBound to upperBound.
|
||||||
*/
|
*/
|
||||||
// @DoNotTouch
|
@DoNotTouch
|
||||||
// public static NumberExpression[] multiplicationTable(int lowerBound, int upperBound) {
|
public static NumberExpression[] multiplicationTable(int lowerBound, int upperBound) {
|
||||||
// int numberOfNumbers = upperBound - lowerBound + 1;
|
int numberOfNumbers = upperBound - lowerBound + 1;
|
||||||
// NumberExpression[] baseNumbers = new NumberExpression[numberOfNumbers];
|
NumberExpression[] baseNumbers = new NumberExpression[numberOfNumbers];
|
||||||
//
|
|
||||||
// for (int i = lowerBound; i <= upperBound; i++) {
|
for (int i = lowerBound; i <= upperBound; i++) {
|
||||||
// // Copy to local variable to make it effectively final, so it can be used in
|
// Copy to local variable to make it effectively final, so it can be used in
|
||||||
// // lambda
|
// lambda
|
||||||
// int finalI = i;
|
int finalI = i;
|
||||||
// baseNumbers[i - lowerBound] = () -> finalI;
|
baseNumbers[i - lowerBound] = () -> finalI;
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// return multiplicationTable(baseNumbers);
|
return multiplicationTable(baseNumbers);
|
||||||
// }
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters the given array of number expressions based on the given predicate.
|
* Filters the given array of number expressions based on the given predicate.
|
||||||
|
@ -58,8 +61,10 @@ public class NumberExpressionFactory {
|
||||||
* @param predicate the predicate to filter the number expressions
|
* @param predicate the predicate to filter the number expressions
|
||||||
* @return An array of number expressions that satisfy the predicate.
|
* @return An array of number expressions that satisfy the predicate.
|
||||||
*/
|
*/
|
||||||
// @StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
// public static NumberExpression[] filter(NumberExpression[] numbers, IntPredicate predicate) {
|
public static NumberExpression[] filter(NumberExpression[] numbers, IntPredicate predicate) {
|
||||||
// return crash(); // TODO: H4.4 - remove if implemented
|
return Arrays.stream(numbers)
|
||||||
// }
|
.filter(e -> predicate.test(e.evaluate()))
|
||||||
|
.toArray(NumberExpression[]::new);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
20
H07/src/main/java/h07/peano/PeanoAddExpression.java
Normal file
20
H07/src/main/java/h07/peano/PeanoAddExpression.java
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
package h07.peano;
|
||||||
|
|
||||||
|
public class PeanoAddExpression implements PeanoArithmeticExpression {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PeanoNumberExpression evaluate(PeanoNumberExpression a, PeanoNumberExpression b) {
|
||||||
|
if (b.evaluate() instanceof Successor s) {
|
||||||
|
return () -> {
|
||||||
|
return new Successor(evaluate(() -> {
|
||||||
|
return a.evaluate();
|
||||||
|
}, () -> {
|
||||||
|
return s.predecessor;
|
||||||
|
}).evaluate());
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return () -> {
|
||||||
|
return a.evaluate();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
package h07.peano;
|
||||||
|
|
||||||
|
public interface PeanoArithmeticExpression {
|
||||||
|
|
||||||
|
PeanoNumberExpression evaluate(PeanoNumberExpression a, PeanoNumberExpression b);
|
||||||
|
}
|
12
H07/src/main/java/h07/peano/PeanoMultiplyExpression.java
Normal file
12
H07/src/main/java/h07/peano/PeanoMultiplyExpression.java
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
package h07.peano;
|
||||||
|
|
||||||
|
public class PeanoMultiplyExpression implements PeanoArithmeticExpression {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PeanoNumberExpression evaluate(PeanoNumberExpression a, PeanoNumberExpression b) {
|
||||||
|
if (b.evaluate() instanceof Successor s) {
|
||||||
|
return () -> new PeanoAddExpression().evaluate(a, evaluate(a, () -> s.predecessor)).evaluate();
|
||||||
|
}
|
||||||
|
return Zero::new;
|
||||||
|
}
|
||||||
|
}
|
6
H07/src/main/java/h07/peano/PeanoNumberExpression.java
Normal file
6
H07/src/main/java/h07/peano/PeanoNumberExpression.java
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
package h07.peano;
|
||||||
|
|
||||||
|
public interface PeanoNumberExpression {
|
||||||
|
|
||||||
|
NaturalNumber evaluate();
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
package h07.peano;
|
package h07.peano;
|
||||||
|
|
||||||
|
|
||||||
|
import h07.ConvertNumberToPeanoExpressionImpl;
|
||||||
|
import h07.NumberExpression;
|
||||||
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
|
|
||||||
import static org.tudalgo.algoutils.student.Student.crash;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a factory for Peano number expressions.
|
* Represents a factory for Peano number expressions.
|
||||||
|
@ -15,10 +17,14 @@ public class PeanoNumberExpressionFactory {
|
||||||
* @param numberExpressions the number expressions to convert
|
* @param numberExpressions the number expressions to convert
|
||||||
* @return the converted Peano number expressions
|
* @return the converted Peano number expressions
|
||||||
*/
|
*/
|
||||||
// @StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
// public static PeanoNumberExpression[] fromNumberExpressions(NumberExpression[] numberExpressions) {
|
public static PeanoNumberExpression[] fromNumberExpressions(NumberExpression[] numberExpressions) {
|
||||||
// return crash(); // TODO: H4.3 - remove if implemented
|
return Arrays.stream(numberExpressions)
|
||||||
// }
|
.map(e -> {
|
||||||
|
return new ConvertNumberToPeanoExpressionImpl().convert(e);
|
||||||
|
})
|
||||||
|
.toArray(PeanoNumberExpression[]::new);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Folds an array of Peano number expressions into a single Peano number expression.
|
* Folds an array of Peano number expressions into a single Peano number expression.
|
||||||
|
@ -28,8 +34,11 @@ public class PeanoNumberExpressionFactory {
|
||||||
* @param operation the operation to apply
|
* @param operation the operation to apply
|
||||||
* @return the folded Peano number expression
|
* @return the folded Peano number expression
|
||||||
*/
|
*/
|
||||||
// @StudentImplementationRequired
|
@StudentImplementationRequired
|
||||||
// public static PeanoNumberExpression fold(PeanoNumberExpression[] peanoNumberExpressions, PeanoNumberExpression initial, PeanoArithmeticExpression operation) {
|
public static PeanoNumberExpression fold(PeanoNumberExpression[] peanoNumberExpressions, PeanoNumberExpression initial, PeanoArithmeticExpression operation) {
|
||||||
// return crash(); // TODO: H4.5 - remove if implemented
|
return Arrays.stream(peanoNumberExpressions)
|
||||||
// }
|
.reduce(initial, (e1, e2) -> {
|
||||||
|
return operation.evaluate(e1, e2);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package h08;
|
package h08;
|
||||||
|
|
||||||
|
|
||||||
|
import h08.Exceptions.FlightNotFoundException;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -56,7 +58,35 @@ public class Airport {
|
||||||
*/
|
*/
|
||||||
public void addFlight(Flight flight, boolean isDeparting) {
|
public void addFlight(Flight flight, boolean isDeparting) {
|
||||||
//TODO H8.4.1
|
//TODO H8.4.1
|
||||||
org.tudalgo.algoutils.student.Student.crash("H8.4.1 - Remove if implemented");
|
if (isDeparting) {
|
||||||
|
addDeparture(flight);
|
||||||
|
} else {
|
||||||
|
addArrival(flight);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addDeparture(Flight flight) {
|
||||||
|
if (!flight.getDeparture().equals(airportCode)) {
|
||||||
|
throw new IllegalArgumentException("Flight's departure airport code does not match this airport's code");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (departingFlights.length >= departingSize) {
|
||||||
|
departingFlights = Arrays.copyOf(departingFlights, departingSize*2);
|
||||||
|
}
|
||||||
|
|
||||||
|
departingFlights[departingSize++] = flight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addArrival(Flight flight) {
|
||||||
|
if (!flight.getDestination().equals(airportCode)) {
|
||||||
|
throw new IllegalArgumentException("Flight's arrival airport code does not match this airport's code");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arrivingFlights.length >= arrivingSize) {
|
||||||
|
arrivingFlights = Arrays.copyOf(arrivingFlights, arrivingSize*2);
|
||||||
|
}
|
||||||
|
|
||||||
|
arrivingFlights[arrivingSize++] = flight;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,9 +96,54 @@ public class Airport {
|
||||||
* @param isDeparting if true, removes from departing flights, otherwise from arriving flights
|
* @param isDeparting if true, removes from departing flights, otherwise from arriving flights
|
||||||
* @throws FlightNotFoundException if the flight is not found
|
* @throws FlightNotFoundException if the flight is not found
|
||||||
*/
|
*/
|
||||||
public void removeFlight(String flightNumber, boolean isDeparting) {
|
public void removeFlight(String flightNumber, boolean isDeparting) throws FlightNotFoundException {
|
||||||
//TODO H8.4.2
|
//TODO H8.4.2
|
||||||
org.tudalgo.algoutils.student.Student.crash("H8.4.2 - Remove if implemented");
|
if (isDeparting) {
|
||||||
|
removeDeparture(flightNumber);
|
||||||
|
} else {
|
||||||
|
removeArrival(flightNumber);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeArrival(String flightNumber) throws FlightNotFoundException {
|
||||||
|
if (removeFlight(arrivingSize, arrivingFlights, flightNumber)) {
|
||||||
|
arrivingSize--;
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
throw new FlightNotFoundException("Arriving flight not found: " + flightNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeDeparture(String flightNumber) throws FlightNotFoundException {
|
||||||
|
if (removeFlight(departingSize, departingFlights, flightNumber)) {
|
||||||
|
departingSize--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new FlightNotFoundException("Departing flight not found: " + flightNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean removeFlight(int size, Flight[] flights, String flightNumber) {
|
||||||
|
int index = 0;
|
||||||
|
boolean found = false;
|
||||||
|
|
||||||
|
for (; index < size; index++) {
|
||||||
|
if (flights[index].getFlightNumber().equals(flightNumber)) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(index);
|
||||||
|
|
||||||
|
if (found) {
|
||||||
|
for (int i = index; i < size - 1; i++) {
|
||||||
|
flights[i] = flights[i + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
flights[size-1] = null;
|
||||||
|
}
|
||||||
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,11 +154,34 @@ public class Airport {
|
||||||
* @return the flight with the specified flight number
|
* @return the flight with the specified flight number
|
||||||
* @throws FlightNotFoundException if the flight is not found
|
* @throws FlightNotFoundException if the flight is not found
|
||||||
*/
|
*/
|
||||||
public Flight getFlight(String flightNumber, boolean isDeparting) {
|
public Flight getFlight(String flightNumber, boolean isDeparting) throws FlightNotFoundException {
|
||||||
//TODO H8.4.3
|
//TODO H8.4.3
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H8.4.3 - Remove if implemented");
|
if (isDeparting) {
|
||||||
|
return getDeparture(flightNumber);
|
||||||
|
} else {
|
||||||
|
return getArrival(flightNumber);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Flight getArrival(String flightNumber) throws FlightNotFoundException {
|
||||||
|
for (int i = 0; i < arrivingSize; i++) {
|
||||||
|
if (arrivingFlights[i].getFlightNumber().equals(flightNumber)) {
|
||||||
|
return arrivingFlights[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new FlightNotFoundException("Arriving flight not found: " + flightNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Flight getDeparture(String flightNumber) throws FlightNotFoundException {
|
||||||
|
for (int i = 0; i < departingSize; i++) {
|
||||||
|
if (departingFlights[i].getFlightNumber().equals(flightNumber)) {
|
||||||
|
return departingFlights[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new FlightNotFoundException("Departing flight not found: " + flightNumber);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the departing flights of the airport.
|
* Returns the departing flights of the airport.
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package h08;
|
package h08;
|
||||||
|
|
||||||
|
import h08.Exceptions.BookingAlreadyCancelledException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a flight booking. A booking allows the reservation of a flight as long as managing its identification and its relevant information.
|
* Represents a flight booking. A booking allows the reservation of a flight as long as managing its identification and its relevant information.
|
||||||
*/
|
*/
|
||||||
|
@ -80,9 +82,13 @@ public class Booking {
|
||||||
*
|
*
|
||||||
* @throws BookingAlreadyCancelledException if the booking is already cancelled
|
* @throws BookingAlreadyCancelledException if the booking is already cancelled
|
||||||
*/
|
*/
|
||||||
public void cancelBooking() {
|
public void cancelBooking() throws BookingAlreadyCancelledException {
|
||||||
//TODO H8.4.4
|
//TODO H8.4.4
|
||||||
org.tudalgo.algoutils.student.Student.crash("H8.4.4 - Remove if implemented");
|
if (isCancelled) {
|
||||||
|
throw new BookingAlreadyCancelledException("Booking is already cancelled: " + bookingId);
|
||||||
|
}
|
||||||
|
|
||||||
|
isCancelled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
package h08;
|
package h08;
|
||||||
|
|
||||||
|
import h08.Exceptions.*;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a booking management. A booking management oversees booking operations, ensuring validity and handling duplicates.
|
* Represents a booking management. A booking management oversees booking operations, ensuring validity and handling duplicates.
|
||||||
*/
|
*/
|
||||||
|
@ -39,9 +43,26 @@ public class BookingManagement {
|
||||||
* @param flightNumber the flight number of the booking
|
* @param flightNumber the flight number of the booking
|
||||||
* @param passengerId the passenger ID of the booking
|
* @param passengerId the passenger ID of the booking
|
||||||
*/
|
*/
|
||||||
public void createBooking(String bookingId, String flightNumber, String passengerId) {
|
public void createBooking(String bookingId, String flightNumber, String passengerId) throws InvalidBookingException {
|
||||||
//TODO H8.5.5
|
//TODO H8.5.5
|
||||||
org.tudalgo.algoutils.student.Student.crash("H8.5.5 - Remove if implemented");
|
try {
|
||||||
|
validateAndCheckBooking(bookingId, flightNumber, passengerId);
|
||||||
|
|
||||||
|
var flight = flightManagement.getFlight(flightNumber);
|
||||||
|
flight.bookSeat();
|
||||||
|
|
||||||
|
if (bookings.length <= size) {
|
||||||
|
bookings = Arrays.copyOf(bookings, size*2);
|
||||||
|
}
|
||||||
|
|
||||||
|
bookings[size++] = new Booking(bookingId, flightNumber, passengerId);
|
||||||
|
|
||||||
|
} catch (NoSeatsAvailableException ignored) {
|
||||||
|
} catch (DuplicateBookingException e) {
|
||||||
|
System.out.println("Booking already exists: " + bookingId);
|
||||||
|
} catch (InvalidBookingException e) {
|
||||||
|
System.out.println("Invalid booking details: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,9 +74,17 @@ public class BookingManagement {
|
||||||
* @throws InvalidBookingException if the booking details are invalid
|
* @throws InvalidBookingException if the booking details are invalid
|
||||||
* @throws DuplicateBookingException if the booking ID is already in use
|
* @throws DuplicateBookingException if the booking ID is already in use
|
||||||
*/
|
*/
|
||||||
private void validateAndCheckBooking(String bookingId, String flightNumber, String passengerId){
|
private void validateAndCheckBooking(String bookingId, String flightNumber, String passengerId) throws InvalidBookingException {
|
||||||
//TODO H8.5.2
|
//TODO H8.5.2
|
||||||
org.tudalgo.algoutils.student.Student.crash("H8.5.2 - Remove if implemented");
|
if (bookingId == null || flightNumber == null || passengerId == null || bookingId.isEmpty() || flightNumber.isEmpty() || passengerId.isEmpty()) {
|
||||||
|
throw new InvalidBookingException("Invalid booking details provided.");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
searchBooking(bookingId);
|
||||||
|
throw new DuplicateBookingException("A booking with this ID already exists.");
|
||||||
|
} catch (BookingNotFoundException ignored) {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -65,9 +94,15 @@ public class BookingManagement {
|
||||||
* @return the booking with the specified booking ID
|
* @return the booking with the specified booking ID
|
||||||
* @throws BookingNotFoundException if the booking ist not found
|
* @throws BookingNotFoundException if the booking ist not found
|
||||||
*/
|
*/
|
||||||
private Booking searchBooking(String bookingId){
|
private Booking searchBooking(String bookingId) throws BookingNotFoundException {
|
||||||
//TODO H8.5.3
|
//TODO H8.5.3
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H8.5.3 - Remove if implemented");
|
for (int i = 0; i < size; i++) {
|
||||||
|
if (bookings[i].getBookingId().equals(bookingId)) {
|
||||||
|
return bookings[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new BookingNotFoundException("Booking not found: " + bookingId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,7 +113,12 @@ public class BookingManagement {
|
||||||
*/
|
*/
|
||||||
public Booking getBooking(String bookingId) {
|
public Booking getBooking(String bookingId) {
|
||||||
//TODO H8.5.3
|
//TODO H8.5.3
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H8.5.3 - Remove if implemented");
|
try {
|
||||||
|
return searchBooking(bookingId);
|
||||||
|
} catch (BookingNotFoundException e) {
|
||||||
|
System.out.println("Error retrieving booking: " + e.getMessage());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -88,6 +128,13 @@ public class BookingManagement {
|
||||||
*/
|
*/
|
||||||
public void cancelBooking(String bookingId) {
|
public void cancelBooking(String bookingId) {
|
||||||
//TODO H8.5.4
|
//TODO H8.5.4
|
||||||
org.tudalgo.algoutils.student.Student.crash("H8.5.4 - Remove if implemented");
|
try {
|
||||||
|
searchBooking(bookingId).cancelBooking();
|
||||||
|
System.out.println("Booking cancelled successfully: " + bookingId);
|
||||||
|
} catch (BookingAlreadyCancelledException e) {
|
||||||
|
System.out.println("Already cancelled booking: " + bookingId);
|
||||||
|
} catch (BookingNotFoundException e) {
|
||||||
|
System.out.println("Error cancelling booking: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
package h08.Exceptions;
|
||||||
|
|
||||||
|
public class BookingAlreadyCancelledException extends FlightNotFoundException {
|
||||||
|
|
||||||
|
public BookingAlreadyCancelledException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package h08.Exceptions;
|
||||||
|
|
||||||
|
public class BookingManagementException extends Exception {
|
||||||
|
|
||||||
|
public BookingManagementException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package h08.Exceptions;
|
||||||
|
|
||||||
|
public class BookingNotFoundException extends BookingManagementException {
|
||||||
|
|
||||||
|
|
||||||
|
public BookingNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package h08.Exceptions;
|
||||||
|
|
||||||
|
public class DuplicateBookingException extends InvalidBookingException {
|
||||||
|
|
||||||
|
|
||||||
|
public DuplicateBookingException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package h08.Exceptions;
|
||||||
|
|
||||||
|
public class FlightManagementException extends Exception {
|
||||||
|
|
||||||
|
public FlightManagementException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package h08.Exceptions;
|
||||||
|
|
||||||
|
public class FlightNotFoundException extends FlightManagementException {
|
||||||
|
|
||||||
|
|
||||||
|
public FlightNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package h08.Exceptions;
|
||||||
|
|
||||||
|
public class InvalidBookingException extends BookingManagementException {
|
||||||
|
|
||||||
|
|
||||||
|
public InvalidBookingException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package h08.Exceptions;
|
||||||
|
|
||||||
|
public class NoSeatsAvailableException extends Exception {
|
||||||
|
|
||||||
|
public NoSeatsAvailableException(String flightNumber) {
|
||||||
|
super("No seats available for flight: " + flightNumber);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,11 @@
|
||||||
package h08;
|
package h08;
|
||||||
|
|
||||||
|
import h08.Exceptions.NoSeatsAvailableException;
|
||||||
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a flight. A flight offers information such as the flight details and seat management.
|
* Represents a flight. A flight offers information such as the flight details and seat management.
|
||||||
|
@ -50,10 +53,17 @@ public class Flight {
|
||||||
*/
|
*/
|
||||||
public Flight(String flightNumber, String departure, String destination, LocalDateTime departureTime, int initialSeats) {
|
public Flight(String flightNumber, String departure, String destination, LocalDateTime departureTime, int initialSeats) {
|
||||||
//TODO H8.2.1
|
//TODO H8.2.1
|
||||||
|
assert null != flightNumber;
|
||||||
|
assert null != departure;
|
||||||
|
assert null != destination;
|
||||||
|
assert null != departureTime;
|
||||||
|
assert initialSeats >= 0;
|
||||||
|
validateFlightNumber(flightNumber);
|
||||||
|
|
||||||
this.flightNumber = flightNumber;
|
this.flightNumber = flightNumber;
|
||||||
this.departure = departure;
|
this.departure = Objects.requireNonNull(departure);
|
||||||
this.destination = destination;
|
this.destination = Objects.requireNonNull(destination);
|
||||||
this.departureTime = departureTime;
|
this.departureTime = Objects.requireNonNull(departureTime);
|
||||||
this.initialSeats = initialSeats;
|
this.initialSeats = initialSeats;
|
||||||
this.availableSeats = initialSeats;
|
this.availableSeats = initialSeats;
|
||||||
|
|
||||||
|
@ -67,7 +77,7 @@ public class Flight {
|
||||||
@StudentImplementationRequired("H8.2.1")
|
@StudentImplementationRequired("H8.2.1")
|
||||||
public void validateFlightNumber(String flightNumber) {
|
public void validateFlightNumber(String flightNumber) {
|
||||||
//TODO H8.2.1
|
//TODO H8.2.1
|
||||||
org.tudalgo.algoutils.student.Student.crash("H8.2.1 - Remove if implemented");
|
assert flightNumber.matches("^[A-Z]{2}\\d{3,4}$");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -116,9 +126,13 @@ public class Flight {
|
||||||
}
|
}
|
||||||
|
|
||||||
@StudentImplementationRequired("H8.2.2")
|
@StudentImplementationRequired("H8.2.2")
|
||||||
public void bookSeat() {
|
public void bookSeat() throws NoSeatsAvailableException {
|
||||||
//TODO H8.2.2
|
//TODO H8.2.2
|
||||||
org.tudalgo.algoutils.student.Student.crash("H8.2.2 - Remove if implemented");
|
if (availableSeats == 0) {
|
||||||
|
throw new NoSeatsAvailableException(flightNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
availableSeats--;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void cancelSeat() {
|
public void cancelSeat() {
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package h08;
|
package h08;
|
||||||
|
|
||||||
|
import h08.Exceptions.FlightNotFoundException;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -46,9 +48,28 @@ public class FlightManagement {
|
||||||
* @param flight the flight to be added or removed
|
* @param flight the flight to be added or removed
|
||||||
* @param isAddOperation if true, the flight will be added; if false, the flight will be removed
|
* @param isAddOperation if true, the flight will be added; if false, the flight will be removed
|
||||||
*/
|
*/
|
||||||
public void manageFlight(String airportCode, Flight flight, boolean isAddOperation) {
|
public void manageFlight(String airportCode, Flight flight, boolean isAddOperation) throws Exception {
|
||||||
//TODO H8.5.2
|
//TODO H8.5.2
|
||||||
org.tudalgo.algoutils.student.Student.crash("H8.5.2 - Remove if implemented");
|
var airport = searchAirport(airportCode);
|
||||||
|
airport.getAirportCode();
|
||||||
|
|
||||||
|
if (isAddOperation) {
|
||||||
|
try {
|
||||||
|
airport.addFlight(flight, true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
airport.addFlight(flight, false);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
airport.removeDeparture(flight.getFlightNumber());
|
||||||
|
} catch (Exception e) {
|
||||||
|
try {
|
||||||
|
airport.removeArrival(flight.getFlightNumber());
|
||||||
|
} catch (FlightNotFoundException ficken) {
|
||||||
|
System.out.println("Error removing flight: " + ficken.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -87,9 +108,15 @@ public class FlightManagement {
|
||||||
* @return an airport by airport code
|
* @return an airport by airport code
|
||||||
* @throws Exception if the airport ist not found
|
* @throws Exception if the airport ist not found
|
||||||
*/
|
*/
|
||||||
private Airport searchAirport(String airportCode) {
|
private Airport searchAirport(String airportCode) throws Exception {
|
||||||
//TODO H8.5.1
|
//TODO H8.5.1
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H8.5.1 - Remove if implemented");
|
for (int i = 0; i < size; i++) {
|
||||||
|
if (airports[i].getAirportCode().equals(airportCode)) {
|
||||||
|
return airports[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception("Airport not found: " + airportCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -101,6 +128,15 @@ public class FlightManagement {
|
||||||
*/
|
*/
|
||||||
private Flight searchFlight(Airport airport, String flightNumber) {
|
private Flight searchFlight(Airport airport, String flightNumber) {
|
||||||
//TODO H8.5.1
|
//TODO H8.5.1
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H8.5.1 - Remove if implemented");
|
try {
|
||||||
|
return airport.getFlight(flightNumber, true);
|
||||||
|
} catch (FlightNotFoundException e) {
|
||||||
|
try {
|
||||||
|
return airport.getFlight(flightNumber, false);
|
||||||
|
}
|
||||||
|
catch (FlightNotFoundException euerScheißErnstEinElf) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -55,7 +55,7 @@ public class Passenger {
|
||||||
@StudentImplementationRequired("H8.1")
|
@StudentImplementationRequired("H8.1")
|
||||||
private String generatePassengerID(String firstName, String lastName, LocalDate dateOfBirth) {
|
private String generatePassengerID(String firstName, String lastName, LocalDate dateOfBirth) {
|
||||||
//TODO H8.1
|
//TODO H8.1
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H8.1 - Remove if implemented");
|
return "" + firstName.charAt(0) + lastName.charAt(0) + dateOfBirth.hashCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -13,12 +13,9 @@ submission {
|
||||||
// Setzen Sie im folgenden Bereich Ihre TU-ID (NICHT Ihre Matrikelnummer!), Ihren Nachnamen und Ihren Vornamen
|
// Setzen Sie im folgenden Bereich Ihre TU-ID (NICHT Ihre Matrikelnummer!), Ihren Nachnamen und Ihren Vornamen
|
||||||
// in Anführungszeichen (z.B. "ab12cdef" für Ihre TU-ID) ein!
|
// in Anführungszeichen (z.B. "ab12cdef" für Ihre TU-ID) ein!
|
||||||
// BEISPIEL:
|
// BEISPIEL:
|
||||||
// studentId = "ab12cdef"
|
studentId = "ab12cdef"
|
||||||
// firstName = "sol_first"
|
firstName = "sol_first"
|
||||||
// lastName = "sol_last"
|
lastName = "sol_last"
|
||||||
studentId = ""
|
|
||||||
firstName = ""
|
|
||||||
lastName = ""
|
|
||||||
|
|
||||||
// Optionally require own tests for mainBuildSubmission task. Default is false
|
// Optionally require own tests for mainBuildSubmission task. Default is false
|
||||||
requireTests = false
|
requireTests = false
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package h09;
|
package h09;
|
||||||
|
|
||||||
|
import h09.abilities.Swims;
|
||||||
import h09.animals.Animal;
|
import h09.animals.Animal;
|
||||||
import org.tudalgo.algoutils.student.annotation.DoNotTouch;
|
import org.tudalgo.algoutils.student.annotation.DoNotTouch;
|
||||||
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
|
|
||||||
|
import javax.security.auth.login.AccountNotFoundException;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
@ -12,13 +14,13 @@ import java.util.function.Supplier;
|
||||||
* An object of a class implementing {@link Enclosure} has the ability to contain and manage a stack of {@link Animal}s.
|
* An object of a class implementing {@link Enclosure} has the ability to contain and manage a stack of {@link Animal}s.
|
||||||
*/
|
*/
|
||||||
// TODO: H9.2.1
|
// TODO: H9.2.1
|
||||||
public interface Enclosure<TODO_REPLACE> {
|
public interface Enclosure<A extends Animal> {
|
||||||
/**
|
/**
|
||||||
* @return the stack of animals which is used manage the contained {@link Animal}s
|
* @return the stack of animals which is used manage the contained {@link Animal}s
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.2.1")
|
@StudentImplementationRequired("H9.2.1")
|
||||||
// TODO: H9.2.1
|
// TODO: H9.2.1
|
||||||
StackOfObjects getStack();
|
StackOfObjects<A> getStack();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Feeds all contained animals.
|
* Feeds all contained animals.
|
||||||
|
@ -47,7 +49,7 @@ public interface Enclosure<TODO_REPLACE> {
|
||||||
* @param func operation to be applied to each {@link Animal} in the enclosure
|
* @param func operation to be applied to each {@link Animal} in the enclosure
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.3.1") // TODO: H9.3.1
|
@StudentImplementationRequired("H9.3.1") // TODO: H9.3.1
|
||||||
default void forEach(Consumer func) {
|
default void forEach(Consumer<? super A> func) {
|
||||||
for (int i = 0; i < this.getStack().size(); i++)
|
for (int i = 0; i < this.getStack().size(); i++)
|
||||||
func.accept(this.getStack().get(i));
|
func.accept(this.getStack().get(i));
|
||||||
}
|
}
|
||||||
|
@ -60,9 +62,9 @@ public interface Enclosure<TODO_REPLACE> {
|
||||||
* @param filter operation to test to each {@link Animal} in the enclosure
|
* @param filter operation to test to each {@link Animal} in the enclosure
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.3.2") // TODO: H9.3.2
|
@StudentImplementationRequired("H9.3.2") // TODO: H9.3.2
|
||||||
default void filterObj(Predicate filter) {
|
default void filterObj(Predicate<? super A> filter) {
|
||||||
for (int i = 0; i < this.getStack().size(); i++) {
|
for (int i = 0; i < this.getStack().size(); i++) {
|
||||||
Object a = this.getStack().get(i);
|
A a = this.getStack().get(i);
|
||||||
if (!filter.test(a)) {
|
if (!filter.test(a)) {
|
||||||
this.getStack().remove(a);
|
this.getStack().remove(a);
|
||||||
i--;
|
i--;
|
||||||
|
@ -82,9 +84,15 @@ public interface Enclosure<TODO_REPLACE> {
|
||||||
* satisfied the predicate
|
* satisfied the predicate
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.3.3")
|
@StudentImplementationRequired("H9.3.3")
|
||||||
default Enclosure filterFunc(Supplier<Enclosure> supp, Predicate<Object> filter) {
|
default <E extends Enclosure<A>> E filterFunc(Supplier<? extends E> supp, Predicate<? super A> filter) {
|
||||||
// TODO: H9.3.3
|
// TODO: H9.3.3
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H9.3.3 - Remove if implemented");
|
E enclosure = supp.get();
|
||||||
|
forEach(a -> {
|
||||||
|
if (filter.test(a)) {
|
||||||
|
enclosure.getStack().push(a);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return enclosure;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -97,13 +105,17 @@ public interface Enclosure<TODO_REPLACE> {
|
||||||
* {@link Predicate} which returns true if a swimming {@link Animal} swims at a low elevation.
|
* {@link Predicate} which returns true if a swimming {@link Animal} swims at a low elevation.
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.4.1") // TODO: H9.4.1
|
@StudentImplementationRequired("H9.4.1") // TODO: H9.4.1
|
||||||
Predicate SWIMS_AT_LOW_ELEVATION = null;
|
Predicate<Swims> SWIMS_AT_LOW_ELEVATION = a ->
|
||||||
|
a.getElevation() < Swims.HIGH_ELEVATION;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Consumer} which lets the consumed {@link Animal} eat and sleep.
|
* {@link Consumer} which lets the consumed {@link Animal} eat and sleep.
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.4.2") // TODO: H9.4.2
|
@StudentImplementationRequired("H9.4.2") // TODO: H9.4.2
|
||||||
Consumer FEED_AND_SLEEP = null;
|
Consumer<Animal> FEED_AND_SLEEP = a -> {
|
||||||
|
a.eat();
|
||||||
|
a.sleep();
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a {@link Consumer} which lets the consumed swimming {@link Animal} eat and swim down.
|
* Returns a {@link Consumer} which lets the consumed swimming {@link Animal} eat and swim down.
|
||||||
|
@ -112,8 +124,11 @@ public interface Enclosure<TODO_REPLACE> {
|
||||||
* @return a {@link Consumer} which lets the consumed swimming {@link Animal} eat and swim down
|
* @return a {@link Consumer} which lets the consumed swimming {@link Animal} eat and swim down
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.4.3")
|
@StudentImplementationRequired("H9.4.3")
|
||||||
static Consumer EAT_AND_SINK() {
|
static <T extends Animal & Swims> Consumer<T> EAT_AND_SINK() {
|
||||||
// TODO: H9.4.3
|
// TODO: H9.4.3
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H9.4.3 - Remove if implemented");
|
return a -> {
|
||||||
|
a.eat();
|
||||||
|
a.swimDown();
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,10 @@ import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
/**
|
/**
|
||||||
* An object of class {@link StackOfObjects} represents a data structure of type stack.
|
* An object of class {@link StackOfObjects} represents a data structure of type stack.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"ManualArrayCopy"})
|
@SuppressWarnings({"ManualArrayCopy", "unchecked"})
|
||||||
public class StackOfObjects<TODO_REPLACE> {
|
public class StackOfObjects<O> {
|
||||||
@StudentImplementationRequired("H9.1.1") // TODO: H9.1.1
|
@StudentImplementationRequired("H9.1.1") // TODO: H9.1.1
|
||||||
private Object[] objs = new Object[0];
|
private O[] objs = (O[]) new Object[0];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pushes the given object on this stack.
|
* Pushes the given object on this stack.
|
||||||
|
@ -17,8 +17,8 @@ public class StackOfObjects<TODO_REPLACE> {
|
||||||
* @param obj the object
|
* @param obj the object
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.1.2") // TODO: H9.1.2
|
@StudentImplementationRequired("H9.1.2") // TODO: H9.1.2
|
||||||
public void push(Object obj) {
|
public void push(O obj) {
|
||||||
Object[] newArray = new Object[objs.length + 1];
|
O[] newArray = (O[]) new Object[objs.length + 1];
|
||||||
for (int i = 0; i < objs.length; i++) newArray[i] = objs[i];
|
for (int i = 0; i < objs.length; i++) newArray[i] = objs[i];
|
||||||
newArray[objs.length] = obj;
|
newArray[objs.length] = obj;
|
||||||
objs = newArray;
|
objs = newArray;
|
||||||
|
@ -30,10 +30,10 @@ public class StackOfObjects<TODO_REPLACE> {
|
||||||
* @param obj the object
|
* @param obj the object
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.1.3") // TODO: H9.1.3
|
@StudentImplementationRequired("H9.1.3") // TODO: H9.1.3
|
||||||
public void remove(Object obj) {
|
public void remove(O obj) {
|
||||||
Object[] newArray = new Object[objs.length - 1];
|
O[] newArray = (O[]) new Object[objs.length - 1];
|
||||||
int n = 0;
|
int n = 0;
|
||||||
for (Object currObj : objs) {
|
for (O currObj : objs) {
|
||||||
if (currObj == obj) continue;
|
if (currObj == obj) continue;
|
||||||
newArray[n++] = currObj;
|
newArray[n++] = currObj;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +57,7 @@ public class StackOfObjects<TODO_REPLACE> {
|
||||||
* @return the object
|
* @return the object
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.1.4") // TODO: H9.1.4
|
@StudentImplementationRequired("H9.1.4") // TODO: H9.1.4
|
||||||
public Object get(int index) {
|
public O get(int index) {
|
||||||
return objs[index];
|
return objs[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,8 +67,8 @@ public class StackOfObjects<TODO_REPLACE> {
|
||||||
* @return the top object
|
* @return the top object
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.1.4") // TODO: H9.1.4
|
@StudentImplementationRequired("H9.1.4") // TODO: H9.1.4
|
||||||
public Object pop() {
|
public O pop() {
|
||||||
Object o = get(objs.length - 1);
|
O o = get(objs.length - 1);
|
||||||
remove(o);
|
remove(o);
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
@ -82,8 +82,8 @@ public class StackOfObjects<TODO_REPLACE> {
|
||||||
*/
|
*/
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
@StudentImplementationRequired("H9.1.5") // TODO: H9.1.5
|
@StudentImplementationRequired("H9.1.5") // TODO: H9.1.5
|
||||||
public static StackOfObjects of(Object... objs) {
|
public static <O> StackOfObjects<O> of(O... objs) {
|
||||||
StackOfObjects stack = new StackOfObjects();
|
var stack = new StackOfObjects<O>();
|
||||||
stack.objs = objs;
|
stack.objs = objs;
|
||||||
return stack;
|
return stack;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,25 +9,36 @@ import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
* {@link Animal}s which have the ability to {@link Swims}.
|
* {@link Animal}s which have the ability to {@link Swims}.
|
||||||
*/
|
*/
|
||||||
// TODO: H9.2.2
|
// TODO: H9.2.2
|
||||||
public class WaterEnclosure<TODO_REPLACE> {
|
public class WaterEnclosure<A extends Animal & Swims> implements Enclosure<A> {
|
||||||
/**
|
/**
|
||||||
* The stack of animals which is used manage the contained Animals.
|
* The stack of animals which is used manage the contained Animals.
|
||||||
*/
|
*/
|
||||||
@StudentImplementationRequired("H9.2.2") // TODO: H9.2.2
|
@StudentImplementationRequired("H9.2.2") // TODO: H9.2.2
|
||||||
final StackOfObjects animals = null;
|
final StackOfObjects<A> animals = new StackOfObjects<>();
|
||||||
|
|
||||||
@StudentImplementationRequired("H9.2.2")
|
@StudentImplementationRequired("H9.2.2")
|
||||||
// @Override
|
@Override
|
||||||
public StackOfObjects getStack() {
|
public StackOfObjects<A> getStack() {
|
||||||
// TODO: H9.2.2
|
// TODO: H9.2.2
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H9.2.2 - Remove if implemented");
|
return animals;
|
||||||
}
|
}
|
||||||
|
|
||||||
@StudentImplementationRequired("H9.2.2")
|
@StudentImplementationRequired("H9.2.2")
|
||||||
// @Override
|
@Override
|
||||||
public void feed() {
|
public void feed() {
|
||||||
// TODO: H9.2.2
|
// TODO: H9.2.2
|
||||||
org.tudalgo.algoutils.student.Student.crash("H9.2.2 - Remove if implemented");
|
forEach(a -> {
|
||||||
|
if (!a.isHungry()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (a.getElevation() < Swims.HIGH_ELEVATION) {
|
||||||
|
a.swimUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
a.eat();
|
||||||
|
a.swimDown();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -38,6 +49,10 @@ public class WaterEnclosure<TODO_REPLACE> {
|
||||||
@StudentImplementationRequired("H9.2.2")
|
@StudentImplementationRequired("H9.2.2")
|
||||||
public float getMeanElevation() {
|
public float getMeanElevation() {
|
||||||
// TODO: H9.2.2
|
// TODO: H9.2.2
|
||||||
return org.tudalgo.algoutils.student.Student.crash("H9.2.2 - Remove if implemented");
|
float sum = 0;
|
||||||
|
for (int i = 0; i < getStack().size(); i++) {
|
||||||
|
sum += getStack().get(i).getElevation();
|
||||||
|
}
|
||||||
|
return sum / getStack().size();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,15 @@
|
||||||
package h09;
|
package h09;
|
||||||
|
|
||||||
|
import h09.abilities.Swims;
|
||||||
|
import h09.animals.Animal;
|
||||||
|
import h09.animals.Fish;
|
||||||
|
import h09.animals.Lion;
|
||||||
|
import h09.animals.Penguin;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An example JUnit test class.
|
* An example JUnit test class.
|
||||||
*/
|
*/
|
||||||
|
@ -12,7 +19,25 @@ public class EnclosureTest {
|
||||||
@StudentImplementationRequired("H9.5.1")
|
@StudentImplementationRequired("H9.5.1")
|
||||||
public void testForEach() {
|
public void testForEach() {
|
||||||
// TODO: H9.5.1
|
// TODO: H9.5.1
|
||||||
org.tudalgo.algoutils.student.Student.crash("H9.5.1 - Remove if implemented");
|
Lion lia = new Lion("Lia", 1);
|
||||||
|
Lion lib = new Lion("Lib", 1);
|
||||||
|
Lion lic = new Lion("Lic", 1);
|
||||||
|
|
||||||
|
// Creating lion enclosure...
|
||||||
|
GroundEnclosure<Lion> lionEnclosure = new GroundEnclosure<>();
|
||||||
|
lionEnclosure.getStack().push(lia);
|
||||||
|
lionEnclosure.getStack().push(lib);
|
||||||
|
lionEnclosure.getStack().push(lic);
|
||||||
|
|
||||||
|
assertTrue(lia.isHungry());
|
||||||
|
assertTrue(lib.isHungry());
|
||||||
|
assertTrue(lic.isHungry());
|
||||||
|
|
||||||
|
lionEnclosure.forEach(Lion::eat);
|
||||||
|
|
||||||
|
assertFalse(lia.isHungry());
|
||||||
|
assertFalse(lib.isHungry());
|
||||||
|
assertFalse(lic.isHungry());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,6 +45,35 @@ public class EnclosureTest {
|
||||||
@StudentImplementationRequired("H9.5.2")
|
@StudentImplementationRequired("H9.5.2")
|
||||||
public void testFilter() {
|
public void testFilter() {
|
||||||
// TODO: H9.5.2
|
// TODO: H9.5.2
|
||||||
org.tudalgo.algoutils.student.Student.crash("H9.5.2 - Remove if implemented");
|
// crating fish
|
||||||
|
Penguin tux = new Penguin("tux", 1, -5);
|
||||||
|
tux.eat();
|
||||||
|
|
||||||
|
Penguin windoof = new Penguin("windoof", 1, -5);
|
||||||
|
|
||||||
|
WaterEnclosure<Penguin> enclosure = new WaterEnclosure<>();
|
||||||
|
enclosure.getStack().push(tux);
|
||||||
|
enclosure.getStack().push(windoof);
|
||||||
|
|
||||||
|
assertFalse(tux.isHungry());
|
||||||
|
assertTrue(windoof.isHungry());
|
||||||
|
|
||||||
|
enclosure
|
||||||
|
.filterFunc(WaterEnclosure::new, Animal::isHungry)
|
||||||
|
.filterFunc(WaterEnclosure::new, Enclosure.SWIMS_AT_LOW_ELEVATION)
|
||||||
|
.forEach(Penguin::swimUp);
|
||||||
|
|
||||||
|
assertTrue(tux.getElevation() < Swims.HIGH_ELEVATION);
|
||||||
|
assertTrue(windoof.getElevation() > Swims.HIGH_ELEVATION);
|
||||||
|
|
||||||
|
enclosure
|
||||||
|
.filterFunc(WaterEnclosure::new, Animal::isHungry)
|
||||||
|
.forEach(Enclosure.EAT_AND_SINK());
|
||||||
|
|
||||||
|
assertTrue(tux.getElevation() < Swims.HIGH_ELEVATION);
|
||||||
|
assertTrue(windoof.getElevation() < Swims.HIGH_ELEVATION);
|
||||||
|
|
||||||
|
assertFalse(tux.isHungry());
|
||||||
|
assertFalse(windoof.isHungry());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,13 +17,13 @@ public class ExampleZooTest {
|
||||||
fishEnclosure.getStack().push(new Fish("Fisch", 3));
|
fishEnclosure.getStack().push(new Fish("Fisch", 3));
|
||||||
|
|
||||||
// can be used after H9.3.1
|
// can be used after H9.3.1
|
||||||
// System.out.println("Get fish elevation...");
|
System.out.println("Get fish elevation...");
|
||||||
// fishEnclosure.forEach(fish -> System.out.println(fish.getName() + ": " + fish.getElevation()));
|
fishEnclosure.forEach(fish -> System.out.println(fish.getName() + ": " + fish.getElevation()));
|
||||||
//
|
|
||||||
// System.out.println("\nLet fish swim up...");
|
System.out.println("\nLet fish swim up...");
|
||||||
// fishEnclosure.forEach(Fish::swimUp);
|
fishEnclosure.forEach(Fish::swimUp);
|
||||||
// fishEnclosure.forEach(Fish::swimUp);
|
fishEnclosure.forEach(Fish::swimUp);
|
||||||
// fishEnclosure.forEach(Fish::swimUp);
|
fishEnclosure.forEach(Fish::swimUp);
|
||||||
|
|
||||||
System.out.println("\nFeed the fish...");
|
System.out.println("\nFeed the fish...");
|
||||||
fishEnclosure.feed();
|
fishEnclosure.feed();
|
||||||
|
@ -40,27 +40,27 @@ public class ExampleZooTest {
|
||||||
penguinGroundEnclosure.getStack().push(new Penguin("Penge", 30));
|
penguinGroundEnclosure.getStack().push(new Penguin("Penge", 30));
|
||||||
|
|
||||||
System.out.println("\nMigrating the Penguins to a WaterEnclosure...");
|
System.out.println("\nMigrating the Penguins to a WaterEnclosure...");
|
||||||
// WaterEnclosure<Penguin> penguinWaterEnclosure = new WaterEnclosure<>();
|
WaterEnclosure<Penguin> penguinWaterEnclosure = new WaterEnclosure<>();
|
||||||
// while (penguinGroundEnclosure.getStack().size() > 0) {
|
while (penguinGroundEnclosure.getStack().size() > 0) {
|
||||||
// Penguin penguin = penguinGroundEnclosure.getStack().pop();
|
Penguin penguin = penguinGroundEnclosure.getStack().pop();
|
||||||
// penguinWaterEnclosure.getStack().push(penguin);
|
penguinWaterEnclosure.getStack().push(penguin);
|
||||||
// }
|
}
|
||||||
|
|
||||||
|
|
||||||
// can be used after H9.3.2
|
// can be used after H9.3.2
|
||||||
// System.out.println("\nRemoving old penguins...");
|
System.out.println("\nRemoving old penguins...");
|
||||||
// penguinWaterEnclosure.filterObj(Enclosure.IS_OLD.negate());
|
penguinWaterEnclosure.filterObj(Enclosure.IS_OLD.negate());
|
||||||
// System.out.println(penguinWaterEnclosure.getStack().size() + " young penguin(s) left.");
|
System.out.println(penguinWaterEnclosure.getStack().size() + " young penguin(s) left.");
|
||||||
//
|
|
||||||
// System.out.println("\nSleep /age again");
|
System.out.println("\nSleep /age again");
|
||||||
// penguinWaterEnclosure.forEach(Animal::sleep);
|
penguinWaterEnclosure.forEach(Animal::sleep);
|
||||||
//
|
|
||||||
// // can be used after H9.3.3
|
// can be used after H9.3.3
|
||||||
// System.out.println("\nLooking for hungry and old Penguins...");
|
System.out.println("\nLooking for hungry and old Penguins...");
|
||||||
// System.out.println(penguinWaterEnclosure.countHungry() + " hungry penguins.");
|
System.out.println(penguinWaterEnclosure.countHungry() + " hungry penguins.");
|
||||||
// System.out.println(penguinWaterEnclosure.filterFunc(WaterEnclosure::new, Enclosure.IS_OLD).getStack().size() + " old penguins.");
|
System.out.println(penguinWaterEnclosure.filterFunc(WaterEnclosure::new, Enclosure.IS_OLD).getStack().size() + " old penguins.");
|
||||||
//
|
|
||||||
// System.out.println("\nFeed old penguins and let them sleep...");
|
System.out.println("\nFeed old penguins and let them sleep...");
|
||||||
// penguinWaterEnclosure.filterFunc(WaterEnclosure::new, Enclosure.IS_OLD).forEach(Enclosure.FEED_AND_SLEEP);
|
penguinWaterEnclosure.filterFunc(WaterEnclosure::new, Enclosure.IS_OLD).forEach(Enclosure.FEED_AND_SLEEP);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
[plugins]
|
[plugins]
|
||||||
algomate = { id = "org.tudalgo.algomate", version = "0.7.1" }
|
algomate = { id = "org.tudalgo.algomate", version = "0.7.0" }
|
||||||
style = { id = "org.sourcegrade.style", version = "3.0.0" }
|
style = { id = "org.sourcegrade.style", version = "3.0.0" }
|
||||||
|
|
|
@ -34,13 +34,4 @@ jagr {
|
||||||
rubricProviderName.set("h07.H07_RubricProvider")
|
rubricProviderName.set("h07.H07_RubricProvider")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
graders {
|
|
||||||
val graderPrivate by creating {
|
|
||||||
graderName.set("H07-Private")
|
|
||||||
rubricProviderName.set("h07.H07_RubricProvider")
|
|
||||||
configureDependencies {
|
|
||||||
implementation(libs.algoutils.tutor)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
[plugins]
|
[plugins]
|
||||||
algomate = { id = "org.tudalgo.algomate", version = "0.7.1" }
|
algomate = { id = "org.tudalgo.algomate", version = "0.7.0" }
|
||||||
style = { id = "org.sourcegrade.style", version = "3.0.0" }
|
style = { id = "org.sourcegrade.style", version = "3.0.0" }
|
||||||
|
|
|
@ -8,8 +8,4 @@ dependencyResolutionManagement {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
<<<<<<<< Updated upstream:solution/H08/settings.gradle.kts
|
|
||||||
rootProject.name = "H08-Root"
|
rootProject.name = "H08-Root"
|
||||||
========
|
|
||||||
rootProject.name = "H09-Student"
|
|
||||||
>>>>>>>> Stashed changes:H09/settings.gradle.kts
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H08/src/main
|
/home/osh/Desktop/FOP-2425-Marathon/H08/src/main
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H08/src/test
|
/home/osh/Desktop/FOP-2425-Marathon/H08/src/test
|
70
solution/H09/jagr.conf
Normal file
70
solution/H09/jagr.conf
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
# The locations of the following directories may be configured here
|
||||||
|
dir {
|
||||||
|
# Grader jar ingest directory
|
||||||
|
graders=graders
|
||||||
|
# Runtime dependencies for submissions
|
||||||
|
libs=libs
|
||||||
|
# Rubrics export directory
|
||||||
|
rubrics=rubrics
|
||||||
|
# Submissions ingest directory
|
||||||
|
submissions=submissions
|
||||||
|
# Submission export directory
|
||||||
|
submissions-export=submissions-export
|
||||||
|
}
|
||||||
|
executor {
|
||||||
|
#
|
||||||
|
# The maximum amount of concurrency to use for grading.
|
||||||
|
# For a given concurrency n, Jagr will ensure that a maximum of n threads or processes are used concurrently that actively run
|
||||||
|
# submission code.
|
||||||
|
concurrency=4
|
||||||
|
#
|
||||||
|
# The JVM arguments to use for grading. These arguments are passed to the JVM that runs the grading code.
|
||||||
|
# This only applies to the "process" mode, as the "thread" and "single" modes do not spawn a new JVM.
|
||||||
|
#
|
||||||
|
jvm-args=[]
|
||||||
|
#
|
||||||
|
# The executor mode to use. The following options are available:
|
||||||
|
# - "single" ::
|
||||||
|
# Runs every TestCycle consecutively in the main thread. This mode does not create any extra processes or threads for grading.
|
||||||
|
#
|
||||||
|
# - "thread" ::
|
||||||
|
# Creates a separate thread for every TestCycle. This mode greatly speeds up the grading process, especially with a large
|
||||||
|
# amount of submissions. The overhead of creating, managing and synchronizing threads is minimal compared to the performance
|
||||||
|
# benefits. However, this mode has the danger of creating "unkillable" threads (e.g. from certain kinds of infinite loops)
|
||||||
|
# which dramatically slow down the grading process through resource starvation of the host machine.
|
||||||
|
#
|
||||||
|
# The maximum number of concurrent threads used for grading is defined by the option "concurrency".
|
||||||
|
#
|
||||||
|
# - "process" ::
|
||||||
|
# Creates a separate process for every TestCycle. This mode has the most overhead, but is also the most defensive against
|
||||||
|
# "badly behaving" code. A certain amount of sandboxing can be achieved in this mode, which is not possible in the other modes
|
||||||
|
# such as "thread" or "single".
|
||||||
|
#
|
||||||
|
# The maximum number of concurrent child process used for grading is defined by the option "concurrency".
|
||||||
|
mode=process
|
||||||
|
#
|
||||||
|
# The grading thread's maximum permitted elapsed userTime in milliseconds since the last timeout before an
|
||||||
|
# AssertionFailedError is thrown. If a thread's userTime satisfies
|
||||||
|
# (userTime - lastTimeout) > individualTimeout,
|
||||||
|
# the current userTime is stored for comparison later, and an AssertionFailedError is thrown to be caught by JUnit.
|
||||||
|
timeout-individual=10000
|
||||||
|
#
|
||||||
|
# The grading thread's maximum permitted elapsed userTime in milliseconds (from thread start) before an
|
||||||
|
# AssertionFailedError is thrown. If a thread's userTime satisfies
|
||||||
|
# ((userTime - lastTimeout) > individualTimeout) && (userTime > totalTimeout),
|
||||||
|
# an AssertionFailedError is thrown to be caught by JUnit. Note that lastTimeout is not reset in this case, and all further
|
||||||
|
# invocations of checkTimeout() will result in an AssertionFailedError
|
||||||
|
timeout-total=150000
|
||||||
|
}
|
||||||
|
extras {
|
||||||
|
moodle-unpack {
|
||||||
|
assignment-id-regex=".*Abgabe[^0-9]*(?<assignmentId>[0-9]{1,2}).*[.]zip"
|
||||||
|
enabled=true
|
||||||
|
student-regex=".* - (?<studentId>([a-z]{2}[0-9]{2}[a-z]{4})|([a-z]+_[a-z]+))/submissions/.*[.]jar"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
transformers {
|
||||||
|
timeout {
|
||||||
|
enabled=true
|
||||||
|
}
|
||||||
|
}
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H09/src/main
|
/home/osh/Desktop/FOP-2425-Marathon/H09/src/main
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H09/src/test
|
/home/osh/Desktop/FOP-2425-Marathon/H09/src/test
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H10/src/main
|
/home/osh/Desktop/FOP-2425-Marathon/H10/src/main
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H10/src/test
|
/home/osh/Desktop/FOP-2425-Marathon/H10/src/test
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H11/src/main
|
/home/osh/Desktop/FOP-2425-Marathon/H11/src/main
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H11/src/test
|
/home/osh/Desktop/FOP-2425-Marathon/H11/src/test
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H12/src/main
|
/home/osh/Desktop/FOP-2425-Marathon/H12/src/main
|
|
@ -1 +1 @@
|
||||||
/home/osh/Desktop/FOP-2425-Marathon/FOP-2425-Marathon/H12/src/test
|
/home/osh/Desktop/FOP-2425-Marathon/H12/src/test
|
Loading…
Add table
Reference in a new issue