Pr_robot_factory/domain/R2D2.java

70 lines
1.8 KiB
Java

package domain;
import robot.exceptions.RobotException;
import robot.exceptions.RobotIllegalStateException;
public class R2D2 extends RobotBasics {
/**
* Constructor
@@ -8,7 +12,35 @@ public class R2D2 extends RobotBasics {
*/
public R2D2(int id, String name){
super(id, name);
}
@Override
public int[] think(int[] zahlen) throws RobotException {
if(isPowerOn()){
return sorting(zahlen);
}else{
throw new RobotIllegalStateException(getName() + " is turned off!");
}
}
public int[] sorting(int[] arr) {
//Selectionsort
int small;
for (int i = 0; i <arr.length - 1; i++) {
small = i;
for (int j = i + 1; j < arr.length; j++) {
//if there is a smaller number on this position
if (arr[j] < arr[small]) {
small = j;
//swap values
int temp = arr[i];
arr[i] = arr[small];
arr[small] = temp;
}
}
}
return arr;
}
public String ausgabe(int[] input){
String result = " ";
if(input.length != 0) {
result = " " + input[0];
for(int i = 1; i < input.length; i++){
result += "; " + input[i];
}
return result;
}
return null;
}
/**
* Die Methode nimmt ein Array aus int entgegen und wandelt diese in einen String um.
* @param input Zahlen, die ausgegeben werden sollen.
* @return String oder null
* @throws RobotException
*/
@Override
public String speak(int[] input) throws RobotException {
if(isPowerOn()){
return ausgabe(input);
}else{
throw new RobotIllegalStateException(getName() + " is turned off!");
}
}
}