From efd9dba44544c6c86b126ad5855f1065ad91bfee Mon Sep 17 00:00:00 2001 From: Eline Date: Thu, 29 Dec 2022 11:30:12 +0100 Subject: [PATCH] R2D2 --- .classpath | 6 +++++ .gitignore | 1 + .project | 17 ++++++++++++ src/R2D2.java | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 src/R2D2.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..0cbf9cd --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae3c172 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..bf10708 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Robot + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/R2D2.java b/src/R2D2.java new file mode 100644 index 0000000..d71c5d9 --- /dev/null +++ b/src/R2D2.java @@ -0,0 +1,72 @@ +import tpe.exceptions.roboter.Robot; +import tpe.exceptions.roboter.exceptions.RobotException; + +import java.util.Arrays; +import java.util.stream.Collectors; + +public class R2D2 implements Robot { + private int id; + private String name; + private boolean isPowerOn; + + + + @Override + public int getId() { + return id; + } + + @Override + public String getName() { + return name; + } + + @Override + public void triggerPowerSwitch() { + if(isPowerOn==false){ + isPowerOn=true; + }else{ + isPowerOn=false; + } + } + + @Override + public boolean isPowerOn() { + return isPowerOn; + } + + @Override + public RobotException getLastException() { + return null; + } + + @Override + public String speak(int[] zahlen) throws RobotException { + + var sortiert = think(zahlen); + + return arrayFormatieren(sortiert); + } + + private String arrayFormatieren(int[] zahlen){ + var ergebnis = Arrays.stream(zahlen) + .boxed() + .map(String::valueOf) + .collect(Collectors.joining(",")); + return ergebnis; + } + @Override + public int[] think(int[] zahlen) throws RobotException { + int n = zahlen.length; + for (int i = 1; i < n; ++i) { + int key = zahlen[i]; + int j = i - 1; + while (j >= 0 && zahlen[j] < key) { + zahlen[j + 1] = zahlen[j]; + j = j - 1; + } + zahlen[j + 1] = key; + } + return zahlen; + } +}