2022-12-08 14:58:44 +01:00
|
|
|
package domain;
|
|
|
|
|
2022-12-14 10:18:36 +01:00
|
|
|
|
2023-01-06 22:55:33 +01:00
|
|
|
import utility.robot_exceptions.ExceptionStorage;
|
|
|
|
import utility.robot_exceptions.RobotException;
|
|
|
|
import utility.robot_exceptions.robotExceptions;
|
2023-01-03 01:18:57 +01:00
|
|
|
|
|
|
|
public class R2D2 extends Robot {
|
2022-12-08 23:34:47 +01:00
|
|
|
/**
|
2022-12-20 12:03:41 +01:00
|
|
|
* @param id> int
|
|
|
|
* @param name> String
|
2022-12-08 23:34:47 +01:00
|
|
|
*/
|
2022-12-08 23:13:22 +01:00
|
|
|
public R2D2(int id, String name){
|
2023-01-09 23:32:30 +01:00
|
|
|
super(id, name, RobotType.R2D2);
|
2022-12-08 23:13:22 +01:00
|
|
|
}
|
2022-12-08 23:34:47 +01:00
|
|
|
|
2022-12-25 19:57:20 +01:00
|
|
|
/**
|
2023-01-06 22:55:33 +01:00
|
|
|
* @see utility.interfaces.RobotInstructions
|
2023-01-07 18:35:00 +01:00
|
|
|
* Sorting then returning the input arr with selection sort
|
2022-12-25 19:57:20 +01:00
|
|
|
*/
|
2022-12-21 16:44:49 +01:00
|
|
|
public int[] think(int[] input) throws RobotException {
|
2022-12-14 10:18:36 +01:00
|
|
|
if(isPowerOn()){
|
2022-12-21 16:44:49 +01:00
|
|
|
return selectionSort(input);
|
2022-12-14 09:54:18 +01:00
|
|
|
}else{
|
2022-12-25 19:57:20 +01:00
|
|
|
RobotException robotexception = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
|
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
|
|
throw robotexception;
|
2022-12-14 09:54:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 22:55:33 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 11:08:49 +01:00
|
|
|
/**
|
2023-01-06 22:55:33 +01:00
|
|
|
* @see utility.interfaces.RobotInstructions
|
2023-01-07 18:35:00 +01:00
|
|
|
* returns the sorted list as String with the delimiter ','
|
2022-12-14 11:08:49 +01:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String speak(int[] input) throws RobotException {
|
2022-12-20 12:03:41 +01:00
|
|
|
if (isPowerOn()) {
|
2023-01-07 18:35:00 +01:00
|
|
|
return output(input, ",");
|
2022-12-20 12:03:41 +01:00
|
|
|
} else {
|
2022-12-25 19:57:20 +01:00
|
|
|
RobotException robotexception = new RobotException(robotExceptions.ILLEGALSTATE, getName());
|
|
|
|
this.exceptions = new ExceptionStorage(robotexception);
|
|
|
|
throw robotexception;
|
2022-12-14 11:08:49 +01:00
|
|
|
}
|
|
|
|
}
|
2022-12-14 10:18:36 +01:00
|
|
|
}
|