Singleton

master
Eline 2022-12-30 03:08:38 +01:00
parent 27519b8f79
commit fc67d7b309
3 changed files with 18 additions and 12 deletions

View File

@ -61,7 +61,7 @@ public class C3PO implements Robot {
for (int i = 1; i < n; ++i) { for (int i = 1; i < n; ++i) {
int key = zahlen[i]; int key = zahlen[i];
int j = i - 1; int j = i - 1;
while (j >= 0 && zahlen[j] > key) { while (j >= 0 && zahlen[j] < key) {
zahlen[j + 1] = zahlen[j]; zahlen[j + 1] = zahlen[j];
j = j - 1; j = j - 1;
} }

View File

@ -1,21 +1,23 @@
import tpe.exceptions.roboter.Robot; import tpe.exceptions.roboter.Robot;
import tpe.exceptions.roboter.exceptions.RobotException; import tpe.exceptions.roboter.exceptions.RobotException;
import tpe.exceptions.roboter.exceptions.RobotIllegalStateException;
import java.util.Arrays; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class Nexus6 implements Robot { public class Nexus6 implements Robot {
private int id = 19281982;
private String name = "pris";
private boolean isPowerOn = false;
private static Nexus6 instance = new Nexus6(); private static Nexus6 instance = new Nexus6();
private int id;
private String name;
private boolean isPowerOn;
private Nexus6() { private Nexus6() {
} }
public static Nexus6 getInstance() { public static Nexus6 getInstance() {
if (instance == null) if (instance == null)
instance = new Nexus6(); instance = new Nexus6();
@ -25,12 +27,12 @@ public class Nexus6 implements Robot {
@Override @Override
public int getId() { public int getId() {
return 19281982; return id;
} }
@Override @Override
public String getName() { public String getName() {
return "pris"; return name;
} }
@Override @Override
@ -42,6 +44,7 @@ public class Nexus6 implements Robot {
} }
} }
@Override @Override
public boolean isPowerOn() { public boolean isPowerOn() {
return isPowerOn; return isPowerOn;
@ -53,7 +56,7 @@ public class Nexus6 implements Robot {
} }
@Override @Override
public String speak(int[] zahlen) throws RobotException { public String speak(int[] zahlen) /*throws RobotIllegalStateException */{
var sortiert = think(zahlen); var sortiert = think(zahlen);
@ -68,7 +71,7 @@ public class Nexus6 implements Robot {
return ergebnis; return ergebnis;
} }
@Override @Override
public int[] think(int[] zahlen) throws RobotException { public int[] think(int[] zahlen) /*throws RobotIllegalStateException*/ {
int n = zahlen.length; int n = zahlen.length;
for (int i = 1; i < n; ++i) { for (int i = 1; i < n; ++i) {
int key = zahlen[i]; int key = zahlen[i];
@ -81,4 +84,7 @@ public class Nexus6 implements Robot {
} }
return zahlen; return zahlen;
} }
} }

View File

@ -61,7 +61,7 @@ public class R2D2 implements Robot {
for (int i = 1; i < n; ++i) { for (int i = 1; i < n; ++i) {
int key = zahlen[i]; int key = zahlen[i];
int j = i - 1; int j = i - 1;
while (j >= 0 && zahlen[j] < key) { while (j >= 0 && zahlen[j] > key) {
zahlen[j + 1] = zahlen[j]; zahlen[j + 1] = zahlen[j];
j = j - 1; j = j - 1;
} }