From efd9dba44544c6c86b126ad5855f1065ad91bfee Mon Sep 17 00:00:00 2001 From: Eline Date: Thu, 29 Dec 2022 11:30:12 +0100 Subject: [PATCH 1/4] 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; + } +} From 684854b525b54b8ce357f3e4383b188f3320dcef Mon Sep 17 00:00:00 2001 From: Eline Date: Thu, 29 Dec 2022 12:13:12 +0100 Subject: [PATCH 2/4] Nexus6 Singleton Versuch --- src/Nexus6.java | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 src/Nexus6.java diff --git a/src/Nexus6.java b/src/Nexus6.java new file mode 100644 index 0000000..853eb10 --- /dev/null +++ b/src/Nexus6.java @@ -0,0 +1,4 @@ + +public class Nexus6 { + +} From 27519b8f7946f9474a33de54326764953ef8783f Mon Sep 17 00:00:00 2001 From: Eline Date: Fri, 30 Dec 2022 03:08:10 +0100 Subject: [PATCH 3/4] Singleton --- src/Nexus6.java | 82 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/src/Nexus6.java b/src/Nexus6.java index 853eb10..ff6ef73 100644 --- a/src/Nexus6.java +++ b/src/Nexus6.java @@ -1,4 +1,84 @@ +import tpe.exceptions.roboter.Robot; +import tpe.exceptions.roboter.exceptions.RobotException; -public class Nexus6 { +import java.util.Arrays; +import java.util.stream.Collectors; +public class Nexus6 implements Robot { + + private static Nexus6 instance = new Nexus6(); + + private int id; + private String name; + private boolean isPowerOn; + + private Nexus6() { + + } + + public static Nexus6 getInstance() { + if (instance == null) + instance = new Nexus6(); + return instance; + } + + + @Override + public int getId() { + return 19281982; + } + + @Override + public String getName() { + return "pris"; + } + + @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; + } } From fc67d7b309de8a5ce9d2cb47b9f8401bb8eb29f5 Mon Sep 17 00:00:00 2001 From: Eline Date: Fri, 30 Dec 2022 03:08:38 +0100 Subject: [PATCH 4/4] Singleton --- src/C3PO.java | 2 +- src/Nexus6.java | 26 ++++++++++++++++---------- src/R2D2.java | 2 +- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/C3PO.java b/src/C3PO.java index 08d4f75..38fe1ae 100644 --- a/src/C3PO.java +++ b/src/C3PO.java @@ -61,7 +61,7 @@ public class C3PO implements Robot { for (int i = 1; i < n; ++i) { int key = zahlen[i]; int j = i - 1; - while (j >= 0 && zahlen[j] > key) { + while (j >= 0 && zahlen[j] < key) { zahlen[j + 1] = zahlen[j]; j = j - 1; } diff --git a/src/Nexus6.java b/src/Nexus6.java index ff6ef73..1338ab1 100644 --- a/src/Nexus6.java +++ b/src/Nexus6.java @@ -1,21 +1,23 @@ import tpe.exceptions.roboter.Robot; import tpe.exceptions.roboter.exceptions.RobotException; +import tpe.exceptions.roboter.exceptions.RobotIllegalStateException; import java.util.Arrays; import java.util.stream.Collectors; public class Nexus6 implements Robot { - + + private int id = 19281982; + private String name = "pris"; + private boolean isPowerOn = false; + + private static Nexus6 instance = new Nexus6(); - private int id; - private String name; - private boolean isPowerOn; - private Nexus6() { - } + public static Nexus6 getInstance() { if (instance == null) instance = new Nexus6(); @@ -25,12 +27,12 @@ public class Nexus6 implements Robot { @Override public int getId() { - return 19281982; + return id; } @Override public String getName() { - return "pris"; + return name; } @Override @@ -41,6 +43,7 @@ public class Nexus6 implements Robot { isPowerOn=false; } } + @Override public boolean isPowerOn() { @@ -53,7 +56,7 @@ public class Nexus6 implements Robot { } @Override - public String speak(int[] zahlen) throws RobotException { + public String speak(int[] zahlen) /*throws RobotIllegalStateException */{ var sortiert = think(zahlen); @@ -68,7 +71,7 @@ public class Nexus6 implements Robot { return ergebnis; } @Override - public int[] think(int[] zahlen) throws RobotException { + public int[] think(int[] zahlen) /*throws RobotIllegalStateException*/ { int n = zahlen.length; for (int i = 1; i < n; ++i) { int key = zahlen[i]; @@ -81,4 +84,7 @@ public class Nexus6 implements Robot { } return zahlen; } + + + } diff --git a/src/R2D2.java b/src/R2D2.java index d71c5d9..502f6fa 100644 --- a/src/R2D2.java +++ b/src/R2D2.java @@ -61,7 +61,7 @@ public class R2D2 implements Robot { for (int i = 1; i < n; ++i) { int key = zahlen[i]; int j = i - 1; - while (j >= 0 && zahlen[j] < key) { + while (j >= 0 && zahlen[j] > key) { zahlen[j + 1] = zahlen[j]; j = j - 1; }