package tpe.exceptions.roboter; import tpe.exceptions.roboter.exceptions.RobotException; import tpe.exceptions.roboter.exceptions.RobotMagicValueException; public class R2D2 implements RobotControl, RobotInstructions { private static int counter = 0; private String name; private boolean powerSwitch; private int id; private RobotMagicValueException robotMagicValueException; StringBuilder sb = new StringBuilder(); public R2D2(String name, boolean powerSwitch) { super(); id = 0+counter; this.name = name; this.powerSwitch = powerSwitch; } @Override public int getId() { return id; } @Override public String getName() { return name; } @Override public void triggerPowerSwitch() { if(powerSwitch=false) powerSwitch=true; else powerSwitch=false; } @Override public boolean isPowerOn() { return powerSwitch; } @Override public RobotException getLastException() { // TODO Auto-generated method stub return null; } @Override public String speak(int[] zahlen) { if(robotMagicValueException.invalidNumber(zahlen)==true) return "42 ist keine gültige Eingabe"; else { for (int i = 0; i < zahlen.length; i++) { sb.append(zahlen[i]); if (i < zahlen.length - 1) { sb.append(", "); } } String output = sb.toString(); return output; } } @Override public int[] think(int[] zahlen) { // Iterate through zahlen from left to right for (int i = 0; i < zahlen.length - 1; i++) { // Set the index of the current smallest element to i int minIndex = i; // Search the smallest element in zahlen for (int j = i + 1; j < zahlen.length; j++) { if (zahlen[j] < zahlen[minIndex]) { minIndex = j; } } // Switch the smallest with the current element int temp = zahlen[i]; zahlen[i] = zahlen[minIndex]; zahlen[minIndex] = temp; } return zahlen; } }