From 83f4f99f0460c13c79af933e7c3ec2436c3695a0 Mon Sep 17 00:00:00 2001 From: roman Date: Tue, 20 Dec 2022 14:24:11 +0100 Subject: [PATCH] added speak method + tests --- .../informatik/name/domain/starwars/C3PO.java | 9 +++-- .../informatik/name/domain/starwars/R2D2.java | 5 ++- .../name/domain/starwars/StarWarsRobot.java | 22 ++++++++++-- tests/R2D2SpeakTest.java | 36 +++++++++++++++++++ tests/R2D2Test.java | 12 ------- tests/R2D2ThinkTest.java | 4 +++ 6 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 tests/R2D2SpeakTest.java delete mode 100644 tests/R2D2Test.java create mode 100644 tests/R2D2ThinkTest.java diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java index 3e31b53..2889848 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/C3PO.java @@ -3,9 +3,8 @@ package de.hsmannheim.informatik.name.domain.starwars; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; public class C3PO extends StarWarsRobot { - @Override - public String speak(int[] zahlen) throws RobotException { - return null; + public C3PO() { + super(';'); } @Override @@ -13,8 +12,8 @@ public class C3PO extends StarWarsRobot { int[] arrToSort = zahlen.clone(); for (int i = 1; i < arrToSort.length; i++) { for (int j = i; j > 0; j--) { - if (arrToSort[j-1] > arrToSort[j]) { - swap(arrToSort, j, j-1); + if (arrToSort[j - 1] > arrToSort[j]) { + swap(arrToSort, j, j - 1); } } } diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java index bc12cc4..ac84cd6 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/R2D2.java @@ -3,9 +3,8 @@ package de.hsmannheim.informatik.name.domain.starwars; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; public class R2D2 extends StarWarsRobot { - @Override - public String speak(int[] zahlen) throws RobotException { - return null; + public R2D2() { + super(','); } @Override diff --git a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java index 959ba2f..bedb7c3 100644 --- a/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java +++ b/src/de/hsmannheim/informatik/name/domain/starwars/StarWarsRobot.java @@ -1,11 +1,17 @@ package de.hsmannheim.informatik.name.domain.starwars; -import de.hsmannheim.informatik.name.domain.robot_interfaces.Robot; import de.hsmannheim.informatik.name.domain.exceptions.RobotException; +import de.hsmannheim.informatik.name.domain.robot_interfaces.Robot; public abstract class StarWarsRobot implements Robot { + private final char SEPARATOR; + + protected StarWarsRobot(char separator) { + SEPARATOR = separator; + } + @Override public int getId() { return 0; @@ -32,9 +38,19 @@ public abstract class StarWarsRobot implements Robot { } @Override - public abstract String speak(int[] zahlen) throws RobotException; + public String speak(int[] zahlen) throws RobotException { + StringBuilder sb = new StringBuilder(); + //Loops through every element of zahlen[] + for (int i = 0; i < zahlen.length; i++) { + sb.append(zahlen[i]); + //if current number is not the last number add a separator + if (i != zahlen.length - 1) { + sb.append(SEPARATOR).append(" "); + } + } + return sb.toString(); + } - @Override public abstract int[] think(int[] zahlen) throws RobotException; protected static void swap(int[] arr, int a, int b) { diff --git a/tests/R2D2SpeakTest.java b/tests/R2D2SpeakTest.java new file mode 100644 index 0000000..f7a2586 --- /dev/null +++ b/tests/R2D2SpeakTest.java @@ -0,0 +1,36 @@ +import de.hsmannheim.informatik.name.domain.exceptions.RobotException; +import de.hsmannheim.informatik.name.domain.starwars.R2D2; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +class R2D2SpeakTest { + + private final R2D2 r2d2 = new R2D2(); + + @Test + void speakTestStandard() throws RobotException { + assertEquals("1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12", r2d2.speak(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12})); + } + + @Test + void speakTestEmpty() throws RobotException { + assertEquals("", r2d2.speak(new int[]{})); + } + + @Test + void speakTestZeros() throws RobotException { + assertEquals("0, 0, 0, 0", r2d2.speak(new int[4])); + } + + @Test + void speakTestOneElement() throws RobotException { + assertEquals("1", r2d2.speak(new int[]{1})); + } + + @Test + void speakTestUnitElements() throws RobotException { + assertEquals("-1, 0, 1", r2d2.speak(new int[]{-1, 0, 1})); + } +} \ No newline at end of file diff --git a/tests/R2D2Test.java b/tests/R2D2Test.java deleted file mode 100644 index fa6e7ba..0000000 --- a/tests/R2D2Test.java +++ /dev/null @@ -1,12 +0,0 @@ -import static org.junit.jupiter.api.Assertions.*; - -class R2D2Test { - - @org.junit.jupiter.api.Test - void speak() { - } - - @org.junit.jupiter.api.Test - void think() { - } -} \ No newline at end of file diff --git a/tests/R2D2ThinkTest.java b/tests/R2D2ThinkTest.java new file mode 100644 index 0000000..bc99362 --- /dev/null +++ b/tests/R2D2ThinkTest.java @@ -0,0 +1,4 @@ +public class R2D2ThinkTest { + + +}