updated R2D2

main
Philipp3107 2022-12-14 10:18:36 +01:00
parent c928bb028e
commit 2a68c15468
2 changed files with 38 additions and 27 deletions

View File

@ -1,5 +1,4 @@
import domain.C3PO; import domain.C3PO;
import domain.R2D2;
import robot.exceptions.RobotException; import robot.exceptions.RobotException;
public class Main { public class Main {
@ -12,17 +11,8 @@ public class Main {
//Herbert.triggerPowerSwitch(); //Herbert.triggerPowerSwitch();
//Gudrun.triggerPowerSwitch(); //Gudrun.triggerPowerSwitch();
try{
Herbert.speak(input);
}catch(RobotException re){
System.out.println(re);
}
try{
Gudrun.think(input2);
}catch(RobotException re){
System.out.println(re);
}
//System.out.println("Was neues ausgeben"); //System.out.println("Was neues ausgeben");
//just some testing //just some testing

View File

@ -1,36 +1,57 @@
package domain; package domain;
import robot.exceptions.RobotException; import robot.exceptions.RobotException;
import robot.exceptions.RobotIllegalStateException; import robot.exceptions.RobotIllegalStateException;
import robot.interfaces.Sorting;
public class R2D2 extends RobotBasics { public class R2D2 extends RobotBasics {
/** /**
* Constructor * Constructor
* @param id> int @@ -8,7 +12,35 @@ public class R2D2 extends RobotBasics {
* @param name> String
*/ */
public R2D2(int id, String name){ public R2D2(int id, String name){
super(id, name); super(id, name);
} }
@Override @Override
public int[] think(int[] zahlen) throws RobotException { public int[] think(int[] zahlen) throws RobotException {
if(isPowerOn() == true){ if(isPowerOn()){
return selectionsSort.sorting(zahlen); return sorting(zahlen);
}else{ }else{
throw new RobotIllegalStateException(getName() + " is turned off."); throw new RobotIllegalStateException(getName() + " is turned off!");
} }
} }
public int[] sorting(int[] arr) {
//Selectionsort... //Selectionsort
Sorting selectionsSort = (int[] input) -> { int small;
System.out.println("ich sortiere mit selection-Sort"); for (int i = 0; i <arr.length - 1; i++) {
small = i;
return input; 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;
}
/*
//Writing all values of the array into a String
String result = "";
if(arr.length != 0) {
result = "" + arr[0];
if (arr.length > 1) {
for (int i = 1; i < arr.length; i++) {
result += "," + arr[i];
}
}
}
return result;
*/
} }