73 lines
2.1 KiB
Java
73 lines
2.1 KiB
Java
package domain;
|
|
|
|
|
|
import utility.robot_exceptions.ExceptionStorage;
|
|
import utility.robot_exceptions.RobotException;
|
|
import utility.robot_exceptions.robotExceptions;
|
|
|
|
public class R2D2 extends Robot {
|
|
/**
|
|
* @param id> int
|
|
* @param name> String
|
|
*/
|
|
public R2D2(int id, String name){
|
|
super(id, name, "R2D2");
|
|
}
|
|
|
|
/**
|
|
* @see utility.interfaces.RobotInstructions
|
|
* Sorting then returning the input arr with selection sort
|
|
*/
|
|
public int[] think(int[] input) throws RobotException {
|
|
if(isPowerOn()){
|
|
return selectionSort(input);
|
|
}else{
|
|
RobotException robotexception = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
throw robotexception;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Sorts any given array of integers with the selection Sort algorithm.
|
|
* @param input
|
|
* @return
|
|
* @throws RobotException
|
|
*/
|
|
public int[] selectionSort(int[] input) throws RobotException{
|
|
if(checkArray(input)){
|
|
int small;
|
|
for(int i = 0; i < input.length; i++){
|
|
small = i;
|
|
for(int j = i + 1; j < input.length; j++){
|
|
if(input[j] < input[small]){
|
|
small = j;
|
|
}
|
|
}
|
|
int temp = input[i];
|
|
input[i] = input[small];
|
|
input[small] = temp;
|
|
}
|
|
return input;
|
|
}else{
|
|
RobotException robotexception = new RobotException(robotExceptions.MAGICVALUE, getName());
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
throw robotexception;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @see utility.interfaces.RobotInstructions
|
|
* returns the sorted list as String with the delimiter ','
|
|
*/
|
|
@Override
|
|
public String speak(int[] input) throws RobotException {
|
|
if (isPowerOn()) {
|
|
return output(input, ",");
|
|
} else {
|
|
RobotException robotexception = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
throw robotexception;
|
|
}
|
|
}
|
|
} |