added speak method + tests

main
roman 2022-12-20 14:24:11 +01:00
parent aa77b2fcc6
commit 83f4f99f04
6 changed files with 65 additions and 23 deletions

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -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}));
}
}

View File

@ -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() {
}
}

View File

@ -0,0 +1,4 @@
public class R2D2ThinkTest {
}