diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2014-11-26 14:57:09 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2014-11-26 14:57:09 +0100 |
| commit | 77bde510679581b31bf66f6ffc142468b1664671 (patch) | |
| tree | 851c45f48805d3faa828d31fbffbf9aeea7d0f05 /src/de/fhswf/in/inf/java1/aufgabe8 | |
| parent | 699e0b41e966dc92bc90c49bdb32f2f71fd05309 (diff) | |
| download | Java1-77bde510679581b31bf66f6ffc142468b1664671.tar.gz Java1-77bde510679581b31bf66f6ffc142468b1664671.zip | |
Assignment No.8 after correction
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe8')
3 files changed, 29 insertions, 24 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe8/FloydAndBentley.java b/src/de/fhswf/in/inf/java1/aufgabe8/FloydAndBentley.java index 54dd863..5416fe5 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe8/FloydAndBentley.java +++ b/src/de/fhswf/in/inf/java1/aufgabe8/FloydAndBentley.java @@ -75,11 +75,11 @@ public final class FloydAndBentley return; } - T val = s.get(rnd.nextInt(s.size() - (k - ret.size()))); + T val = s.get(rnd.nextInt(s.size() - (k - ret.size() - 1))); if (ret.contains(val)) { - val = s.get(s.size() - (k - ret.size())); + val = s.get(s.size() - (k - ret.size() - 1)); } ret.add(val); @@ -115,11 +115,11 @@ public final class FloydAndBentley for (int i = 0; i < k; i++) { - T val = s.get(rnd.nextInt(s.size() - (k - ret.size()))); + T val = s.get(rnd.nextInt(s.size() - (k - ret.size() - 1))); if (ret.contains(val)) { - val = s.get(s.size() - (k - ret.size())); + val = s.get(s.size() - (k - ret.size() - 1)); } ret.add(val); diff --git a/src/de/fhswf/in/inf/java1/aufgabe8/Lottery6OutOf49.java b/src/de/fhswf/in/inf/java1/aufgabe8/Lottery6OutOf49.java index c09893e..fbe150a 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe8/Lottery6OutOf49.java +++ b/src/de/fhswf/in/inf/java1/aufgabe8/Lottery6OutOf49.java @@ -16,9 +16,11 @@ import java.util.List; public class Lottery6OutOf49 { - private ArrayList<Integer> lotteryNumbers = new ArrayList<>(49); + private static final int MAX_NUMBER = 49; - private ArrayList<Integer> lastDraw = new ArrayList<>(6); + private ArrayList<Integer> lotteryNumbers; + + private List<Integer> lastDraw; private Integer superzahl; @@ -28,13 +30,12 @@ public class Lottery6OutOf49 */ public Lottery6OutOf49() { - for (int i = 1; i <= 49; i++) + lotteryNumbers = new ArrayList<Integer>(MAX_NUMBER); + + for (int i = 1; i <= MAX_NUMBER; i++) { lotteryNumbers.add(i); } - - nextDraw(); - } /** @@ -43,17 +44,9 @@ public class Lottery6OutOf49 */ public void nextDraw() { - List<Integer> tmp = FloydAndBentley.sample2(lotteryNumbers, 7); - - superzahl = tmp.get(tmp.size() - 1); - - lastDraw.clear(); - - for (int i = 0; i < 6; i++) - { - lastDraw.add(tmp.get(i)); - } + lastDraw = FloydAndBentley.sample(lotteryNumbers, 7); + superzahl = lastDraw.remove(lastDraw.size() - 1); lastDraw.sort(null); } @@ -64,6 +57,10 @@ public class Lottery6OutOf49 */ public List<Integer> getLastDraw() { + if (lastDraw == null) + { + throw new IllegalStateException("First needs a sraw."); + } return Collections.unmodifiableList(lastDraw); } @@ -74,6 +71,10 @@ public class Lottery6OutOf49 */ public Integer getSuperzahl() { + if (superzahl == null) + { + throw new IllegalStateException("First needs a sraw."); + } return new Integer(superzahl); } diff --git a/src/de/fhswf/in/inf/java1/aufgabe8/LottoZiehungMain.java b/src/de/fhswf/in/inf/java1/aufgabe8/LottoZiehungMain.java index 9c9cffe..f4cb322 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe8/LottoZiehungMain.java +++ b/src/de/fhswf/in/inf/java1/aufgabe8/LottoZiehungMain.java @@ -3,6 +3,7 @@ */ package de.fhswf.in.inf.java1.aufgabe8; +import java.util.ArrayList; import java.util.List; /** @@ -29,13 +30,16 @@ public final class LottoZiehungMain * Command line arguments. */ public static void main(String[] args) - { - Lottery6OutOf49 spiel = new Lottery6OutOf49(); + { + List<Integer> l = new ArrayList<Integer>(); + l.add(1); + System.out.println(FloydAndBentley.sample(l, 1).size()); + System.out.println(FloydAndBentley.sample2(l, 1).size()); + Lottery6OutOf49 spiel = new Lottery6OutOf49(); + spiel.nextDraw(); List<Integer> ziehung = spiel.getLastDraw(); - Integer sonderzahl = spiel.getSuperzahl(); - System.out.println(ziehung + " SonderZahl: " + sonderzahl); } } |
