From a313547060ff280e14ae9745b99c096b25ae6d08 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Thu, 12 Apr 2018 14:39:26 +0200 Subject: [PATCH] Praktikum 2: neue Test, erster Versuch an moreThan --- .../praktikum/praktikum1/GrosseZahl.java | 90 +++++++++---------- .../praktikum/praktikum2/CharCollection.java | 26 ++++-- .../hs/java2/tests/CharCollectionTest.java | 11 ++- 3 files changed, 73 insertions(+), 54 deletions(-) diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum1/GrosseZahl.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum1/GrosseZahl.java index fbfa344..595e92f 100644 --- a/src/main/java/de/joethei/hs/java2/praktikum/praktikum1/GrosseZahl.java +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum1/GrosseZahl.java @@ -1,8 +1,10 @@ package de.joethei.hs.java2.praktikum.praktikum1; +import java.util.logging.Logger; + public class GrosseZahl { - private int Zahl[]; + private int zahl[]; public GrosseZahl(String d) { Boolean korrekteEingabe=true; @@ -12,9 +14,9 @@ public class GrosseZahl { } } if(korrekteEingabe) { - Zahl = new int[d.length()]; + zahl = new int[d.length()]; for(int w=0; w b.Zahl.length) return false; - //if(this.equals(b)) return false; - for(int i=0; i b.Zahl[i]) return false; - if(this.Zahl[i] < b.Zahl[i]) return true; + if(this.zahl.length < b.zahl.length) return true; + if(this.zahl.length > b.zahl.length) return false; + for(int i = 0; i b.zahl[i]) return false; + if(this.zahl[i] < b.zahl[i]) return true; } return false; } @@ -55,14 +47,14 @@ public class GrosseZahl { rueckgabe = new GrosseZahl(this.toString()); operand = new GrosseZahl(b.toString()); } - for(int i=(rueckgabe.Zahl.length-1), w=(operand.Zahl.length-1); i>=0; i--, w--) { - rueckgabe.Zahl[i] += uebertrag; + for(int i = (rueckgabe.zahl.length-1), w = (operand.zahl.length-1); i>=0; i--, w--) { + rueckgabe.zahl[i] += uebertrag; uebertrag = 0; if(w>=0) { - rueckgabe.Zahl[i] += operand.Zahl[w]; + rueckgabe.zahl[i] += operand.zahl[w]; } - if(rueckgabe.Zahl[i] > 9) { - rueckgabe.Zahl[i] -= 10; + if(rueckgabe.zahl[i] > 9) { + rueckgabe.zahl[i] -= 10; uebertrag = 1; } if(i==0 && uebertrag==1) { @@ -83,21 +75,21 @@ public class GrosseZahl { } GrosseZahl rueckgabe = new GrosseZahl(this.toString()); GrosseZahl operand = new GrosseZahl(b.toString()); - for(int i=(rueckgabe.Zahl.length-1), w=(operand.Zahl.length-1); i>=0; i--, w--) { + for(int i = (rueckgabe.zahl.length-1), w = (operand.zahl.length-1); i>=0; i--, w--) { if(w>=0) { - rueckgabe.Zahl[i] -= operand.Zahl[w]; + rueckgabe.zahl[i] -= operand.zahl[w]; } - rueckgabe.Zahl[i] -= uebertrag; + rueckgabe.zahl[i] -= uebertrag; uebertrag = 0; - if(rueckgabe.Zahl[i] < 0) { - rueckgabe.Zahl[i] += 10; + if(rueckgabe.zahl[i] < 0) { + rueckgabe.zahl[i] += 10; uebertrag += 1; } } - while(rueckgabe.Zahl[0] == 0) { + while(rueckgabe.zahl[0] == 0) { x = new StringBuilder(); - for(int i=1; i b.Zahl.length) return false; - for(int i=0; i= 0; i--) { - if(tmp.Zahl[i] != this.Zahl[i]) return false; + if(tmp.zahl.length == this.zahl.length) { + for(int i = this.zahl.length - 1; i >= 0; i--) { + if(tmp.zahl[i] != this.zahl[i]) return false; } return true; }else return false; diff --git a/src/main/java/de/joethei/hs/java2/praktikum/praktikum2/CharCollection.java b/src/main/java/de/joethei/hs/java2/praktikum/praktikum2/CharCollection.java index 736b301..d770d4f 100644 --- a/src/main/java/de/joethei/hs/java2/praktikum/praktikum2/CharCollection.java +++ b/src/main/java/de/joethei/hs/java2/praktikum/praktikum2/CharCollection.java @@ -1,25 +1,30 @@ package de.joethei.hs.java2.praktikum.praktikum2; +import com.sun.istack.internal.NotNull; + import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.logging.Logger; -public class CharCollection { - private Logger logger = Logger.getLogger(this.getClass().getName()); +public class CharCollection implements Cloneable{ - private ArrayList list = new ArrayList<>(); + private List list = new ArrayList<>(); - public CharCollection(char... cc) { + public CharCollection(@NotNull char... cc) { for(char c:cc) this.list.add(c); } - public CharCollection(String s) { + public CharCollection(@NotNull String s) { for(char c : s.toCharArray()) { this.list.add(c); } } + private CharCollection(List list) { + this.list = list; + } public int size() { return this.list.size(); @@ -70,6 +75,14 @@ public class CharCollection { return character; } + public CharCollection moreThan(int n) { + CharCollection result = new CharCollection(list); + for (int i = 0; i < result.size(); i++) { + if(result.count(result.list.get(i)) < n) result.list.remove(i); + } + return result; + } + @Override public String toString() { return list.toString().replace('[', '(').replace(']', ')'); @@ -79,9 +92,8 @@ public class CharCollection { public boolean equals(Object o) { if(o instanceof CharCollection) { CharCollection c = (CharCollection) o; - return this.list.equals(c.list); + return this.list.containsAll(c.list); }return false; } - } \ No newline at end of file diff --git a/src/test/java/de/joethei/hs/java2/tests/CharCollectionTest.java b/src/test/java/de/joethei/hs/java2/tests/CharCollectionTest.java index 8b88fde..33ec209 100644 --- a/src/test/java/de/joethei/hs/java2/tests/CharCollectionTest.java +++ b/src/test/java/de/joethei/hs/java2/tests/CharCollectionTest.java @@ -20,6 +20,7 @@ public class CharCollectionTest { @Test public void count() { assertEquals(2, new CharCollection("Hallo").count('l')); + assertEquals(1, new CharCollection("Hallo").count('a')); } @Test @@ -38,12 +39,18 @@ public class CharCollectionTest { assertEquals("(H, a, l, l, o, W, e, l, t)", new CharCollection("HalloWelt").toString()); } - /* + @Test public void moreThan() { - assertEquals(new CharCollection("ll"), new CharCollection("Hallo").moreThan(2)); + assertEquals(new CharCollection("lll"), new CharCollection("Hallol").moreThan(2)); } + @Test + public void equals() { + assertEquals(new CharCollection("llo"), new CharCollection("lol")); + } + + /* @Test public void except() { assertEquals(new CharCollection("Hao"), new CharCollection("Hallo").except("l"));