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 017d7ba..736b301 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 @@ -3,18 +3,21 @@ package de.joethei.hs.java2.praktikum.praktikum2; import java.util.ArrayList; import java.util.HashSet; import java.util.Set; +import java.util.logging.Logger; public class CharCollection { + private Logger logger = Logger.getLogger(this.getClass().getName()); - ArrayList list = new ArrayList<>(); + private ArrayList list = new ArrayList<>(); public CharCollection(char... cc) { for(char c:cc) this.list.add(c); } + public CharCollection(String s) { - for(char c : s.toCharArray()) { - this.list.add(c); - } + for(char c : s.toCharArray()) { + this.list.add(c); + } } @@ -30,6 +33,48 @@ public class CharCollection { return rueckgabe; } + public int different() { + Set set = new HashSet<>(list); + return set.size(); + } + + /* + public char top() { + if(list.size() == 0) return 0; + ArrayList test = new ArrayList<>(list); + Collections.sort(test); + int actual=0, most=0; + char top = test.get(0); + for(char c : test) { + if (top == c) { + actual++; + if (actual >= most) most = actual; top = c; + } else { + actual = 1; + } + } + return top; + } + */ + public char top() { + if(list.size() == 0) return 0; + int max = 0; + char character ='_'; + for(char c : list) { + int count = count(c); + if(count >= max) { + character = c; + max = count; + } + } + return character; + } + + @Override + public String toString() { + return list.toString().replace('[', '(').replace(']', ')'); + } + @Override public boolean equals(Object o) { if(o instanceof CharCollection) { @@ -38,17 +83,5 @@ public class CharCollection { }return false; } - public int different() { - Set set = new HashSet<>(list); - return set.size(); - } - /*public char top() { - if(list.size() == 0) return 0; - - }*/ - - public String toString() { - return list.toString().replace('[', '(').replace(']', ')'); - } } \ 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 c6e15f1..8b88fde 100644 --- a/src/test/java/de/joethei/hs/java2/tests/CharCollectionTest.java +++ b/src/test/java/de/joethei/hs/java2/tests/CharCollectionTest.java @@ -27,18 +27,18 @@ public class CharCollectionTest { assertEquals(4, new CharCollection("Hallo").different()); } - /* + @Test public void top() { assertEquals('l', new CharCollection("Hallo").top()); } - */ + @Test public void toStringTest() { 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));