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.R2D2;
import robot.exceptions.RobotException;
public class Main {
@ -12,17 +11,8 @@ public class Main {
//Herbert.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");
//just some testing

View File

@ -1,36 +1,57 @@
package domain;
import robot.exceptions.RobotException;
import robot.exceptions.RobotIllegalStateException;
import robot.interfaces.Sorting;
public class R2D2 extends RobotBasics {
/**
* Constructor
* @param id> int
* @param name> String
@@ -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() == true){
return selectionsSort.sorting(zahlen);
if(isPowerOn()){
return sorting(zahlen);
}else{
throw new RobotIllegalStateException(getName() + " is turned off.");
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;
}
/*
//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;
*/
//Selectionsort...
Sorting selectionsSort = (int[] input) -> {
System.out.println("ich sortiere mit selection-Sort");
return input;
};
}
}