Praktikum 2: bis top alles grün

This commit is contained in:
Johannes Theiner 2018-04-06 15:53:31 +02:00
parent 344484735f
commit 7d0008e5e6
2 changed files with 52 additions and 19 deletions

View File

@ -3,14 +3,17 @@ package de.joethei.hs.java2.praktikum.praktikum2;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import java.util.logging.Logger;
public class CharCollection { public class CharCollection {
private Logger logger = Logger.getLogger(this.getClass().getName());
ArrayList<Character> list = new ArrayList<>(); private ArrayList<Character> list = new ArrayList<>();
public CharCollection(char... cc) { public CharCollection(char... cc) {
for(char c:cc) this.list.add(c); for(char c:cc) this.list.add(c);
} }
public CharCollection(String s) { public CharCollection(String s) {
for(char c : s.toCharArray()) { for(char c : s.toCharArray()) {
this.list.add(c); this.list.add(c);
@ -30,6 +33,48 @@ public class CharCollection {
return rueckgabe; return rueckgabe;
} }
public int different() {
Set<Character> set = new HashSet<>(list);
return set.size();
}
/*
public char top() {
if(list.size() == 0) return 0;
ArrayList<Character> 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 @Override
public boolean equals(Object o) { public boolean equals(Object o) {
if(o instanceof CharCollection) { if(o instanceof CharCollection) {
@ -38,17 +83,5 @@ public class CharCollection {
}return false; }return false;
} }
public int different() {
Set<Character> set = new HashSet<>(list);
return set.size();
}
/*public char top() {
if(list.size() == 0) return 0;
}*/
public String toString() {
return list.toString().replace('[', '(').replace(']', ')');
}
} }

View File

@ -27,18 +27,18 @@ public class CharCollectionTest {
assertEquals(4, new CharCollection("Hallo").different()); assertEquals(4, new CharCollection("Hallo").different());
} }
/*
@Test @Test
public void top() { public void top() {
assertEquals('l', new CharCollection("Hallo").top()); assertEquals('l', new CharCollection("Hallo").top());
} }
*/
@Test @Test
public void toStringTest() { public void toStringTest() {
assertEquals("(H, a, l, l, o, W, e, l, t)", new CharCollection("HalloWelt").toString()); assertEquals("(H, a, l, l, o, W, e, l, t)", new CharCollection("HalloWelt").toString());
} }
/*
/*
@Test @Test
public void moreThan() { public void moreThan() {
assertEquals(new CharCollection("ll"), new CharCollection("Hallo").moreThan(2)); assertEquals(new CharCollection("ll"), new CharCollection("Hallo").moreThan(2));