R2D2 Sortieralgorithmus angepasst

master
azadehobenland 2023-01-05 18:48:22 +01:00
parent 42d4f44e02
commit 6415b936c7
3 changed files with 22 additions and 33 deletions

View File

@ -19,10 +19,11 @@ public class Nexus6 implements Robot {
public static Nexus6 getInstance() { public static Nexus6 getInstance() {
if (instance == null) if (instance == null) {
instance = new Nexus6(); instance = new Nexus6();
return instance; }
} return instance;
}
@Override @Override
@ -37,11 +38,7 @@ public class Nexus6 implements Robot {
@Override @Override
public void triggerPowerSwitch() { public void triggerPowerSwitch() {
if(isPowerOn==false){
isPowerOn=true;
}else{
isPowerOn=false;
}
} }
@ -56,33 +53,15 @@ public class Nexus6 implements Robot {
} }
@Override @Override
public String speak(int[] zahlen) /*throws RobotIllegalStateException */{ public String speak(int[] zahlen) throws RobotIllegalStateException {
throw new RobotIllegalStateException(this);
var sortiert = think(zahlen);
return arrayFormatieren(sortiert);
} }
private String arrayFormatieren(int[] zahlen){
var ergebnis = Arrays.stream(zahlen)
.boxed()
.map(String::valueOf)
.collect(Collectors.joining(","));
return ergebnis;
}
@Override @Override
public int[] think(int[] zahlen) /*throws RobotIllegalStateException*/ { public int[] think(int[] zahlen) throws RobotIllegalStateException {
int n = zahlen.length; throw new RobotIllegalStateException(this);
for (int i = 1; i < n; ++i) {
int key = zahlen[i];
int j = i - 1;
while (j >= 0 && zahlen[j] < key) {
zahlen[j + 1] = zahlen[j];
j = j - 1;
}
zahlen[j + 1] = key;
}
return zahlen;
} }

View File

@ -1,4 +1,7 @@
package tpe.exceptions.roboter.exceptions; package tpe.exceptions.roboter.exceptions;
public class RobotException extends Exception{ public class RobotException extends Exception{
public RobotException(String errormessage) {
super(errormessage);
}
} }

View File

@ -1,4 +1,11 @@
package tpe.exceptions.roboter.exceptions; package tpe.exceptions.roboter.exceptions;
public class RobotIllegalStateException { import tpe.exceptions.roboter.Robot;
public class RobotIllegalStateException extends RobotException {
public RobotIllegalStateException(Robot roboter) {
super("Fehler von : " + roboter.getName());
}
} }