implements zu Robot geändert nach Anforderungen

main
Lukas Berens 2023-01-07 14:10:45 +01:00
parent 6807b436c5
commit dbb7419f1e
2 changed files with 12 additions and 11 deletions

View File

@ -2,7 +2,7 @@ package tpe.exceptions.roboter;
import tpe.exceptions.roboter.exceptions.RobotException; import tpe.exceptions.roboter.exceptions.RobotException;
public class C3PO implements RobotControl, RobotInstructions{ public class C3PO implements Robot{
@Override @Override
public String speak(int[] zahlen) { public String speak(int[] zahlen) {

View File

@ -2,13 +2,12 @@ package tpe.exceptions.roboter;
import java.util.*; import java.util.*;
import tpe.exceptions.roboter.exceptions.RobotException; import tpe.exceptions.roboter.exceptions.RobotException;
import tpe.exceptions.roboter.exceptions.RobotMagicValueException;
public class R2D2 implements RobotControl, RobotInstructions { public class R2D2 implements Robot {
RobotException robotexception;
private String name; private String name;
private boolean powerSwitch; private boolean powerSwitch;
private int id; private int id;
private RobotMagicValueException robotMagicValueException;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
public R2D2(String name, boolean powerSwitch) { public R2D2(String name, boolean powerSwitch) {
@ -52,10 +51,8 @@ public class R2D2 implements RobotControl, RobotInstructions {
} }
@Override @Override
public String speak(int[] zahlen) { public String speak(int[] zahlen) throws RobotException {
if(robotMagicValueException.invalidNumber(zahlen)==true)
return "42 ist keine gültige Eingabe";
else {
for (int i = 0; i < zahlen.length; i++) { for (int i = 0; i < zahlen.length; i++) {
sb.append(zahlen[i]); sb.append(zahlen[i]);
if (i < zahlen.length - 1) { if (i < zahlen.length - 1) {
@ -65,11 +62,14 @@ public class R2D2 implements RobotControl, RobotInstructions {
String output = sb.toString(); String output = sb.toString();
return output; return output;
}
}
}
@Override @Override
public int[] think(int[] zahlen) { public int[] think(int[] zahlen) throws RobotException {
for(int i = 0; i < zahlen.length; i++) {
if(zahlen[i]==42)
throw new RobotException.MagicValueException();
}
// Iterate through zahlen from left to right // Iterate through zahlen from left to right
for (int i = 0; i < zahlen.length - 1; i++) { for (int i = 0; i < zahlen.length - 1; i++) {
// Set the index of the current smallest element to i // Set the index of the current smallest element to i
@ -86,6 +86,7 @@ public class R2D2 implements RobotControl, RobotInstructions {
zahlen[minIndex] = temp; zahlen[minIndex] = temp;
} }
return zahlen; return zahlen;
} }
} }