Pr_robot_factory/domain/R2D2.java

70 lines
1.8 KiB
Java
Raw Normal View History

2022-12-08 14:58:44 +01:00
package domain;
2022-12-14 10:18:36 +01:00
2022-12-14 09:54:18 +01:00
import robot.exceptions.RobotException;
import robot.exceptions.RobotIllegalStateException;
2022-12-09 00:30:35 +01:00
public class R2D2 extends RobotBasics {
/**
* Constructor
2022-12-14 10:18:36 +01:00
@@ -8,7 +12,35 @@ public class R2D2 extends RobotBasics {
*/
public R2D2(int id, String name){
super(id, name);
}
2022-12-14 09:54:18 +01:00
@Override
public int[] think(int[] zahlen) throws RobotException {
2022-12-14 10:18:36 +01:00
if(isPowerOn()){
return sorting(zahlen);
2022-12-14 09:54:18 +01:00
}else{
2022-12-14 10:18:36 +01:00
throw new RobotIllegalStateException(getName() + " is turned off!");
2022-12-14 09:54:18 +01:00
}
}
2022-12-14 10:18:36 +01:00
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;
}
2022-12-14 11:08:49 +01:00
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];
2022-12-14 10:18:36 +01:00
}
2022-12-14 11:08:49 +01:00
return result;
2022-12-14 10:18:36 +01:00
}
2022-12-14 11:08:49 +01:00
return null;
}
2022-12-14 09:54:18 +01:00
2022-12-14 11:08:49 +01:00
/**
* 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!");
}
}
2022-12-14 10:18:36 +01:00
}