From 7d0008e5e60d130c43135cf07b156fdb18ba8410 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Fri, 6 Apr 2018 15:53:31 +0200 Subject: [PATCH] =?UTF-8?q?Praktikum=202:=20bis=20top=20alles=20gr=C3=BCn?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../praktikum/praktikum2/CharCollection.java | 65 ++++++++++++++----- .../hs/java2/tests/CharCollectionTest.java | 6 +- 2 files changed, 52 insertions(+), 19 deletions(-) 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));