Praktikum 2: bis top alles grün
This commit is contained in:
parent
344484735f
commit
7d0008e5e6
|
@ -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<Character> list = new ArrayList<>();
|
||||
private ArrayList<Character> 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<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
|
||||
public boolean equals(Object o) {
|
||||
if(o instanceof CharCollection) {
|
||||
|
@ -38,17 +83,5 @@ public class CharCollection {
|
|||
}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(']', ')');
|
||||
}
|
||||
}
|
|
@ -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));
|
||||
|
|
Loading…
Reference in New Issue