This commit is contained in:
Johannes Schmelz 2024-06-15 23:30:02 +02:00
parent ac1cc19a88
commit 0c95c28bc4
14 changed files with 333 additions and 11 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,76 @@
package test.uebung.uebung09.collection;
import java.util.Iterator;
import uebung09.collection.Set;
import uebung09.collection.SetFactory;
import java.util.NoSuchElementException;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class SetTest {
static String a = "test";
static String b = "test2";
static String c = "test3";
public static void main(String[] args) {
}
@Test
public void EmptySetTest(){
Set<String> test = SetFactory.create();
Iterator<String> i = test.iterator();
assertFalse(i.hasNext());
assertThrows(NoSuchElementException.class, i::next);
assertFalse(test.contains(a));
assertEquals(0,test.size());
assertTrue(test.isEmpty());
}
@Test
public void SingeltonSetTest(){
Set<String> test = SetFactory.create(a);
Iterator<String> i = test.iterator();
assertTrue(i.hasNext());
assertTrue(a == i.next());
assertFalse(i.hasNext());
assertThrows(NoSuchElementException.class, i::next);
assertTrue(test.contains(a));
assertEquals(1,test.size());
assertFalse(test.isEmpty());
}
@Test
public void BigSetTest() {
Set<String> test = SetFactory.create(a,b,c);
Iterator<String> i = test.iterator();
assertTrue(i.hasNext());
assertTrue(a == i.next());
assertTrue(i.hasNext());
assertTrue(a == i.next());
assertTrue(i.hasNext());
assertTrue(a == i.next());
assertFalse(i.hasNext());
assertThrows(NoSuchElementException.class, i::next);
assertTrue(test.contains(a));
assertEquals(1,test.size());
assertFalse(test.isEmpty());
}
}

View File

@ -0,0 +1,94 @@
package uebung09.collection;
import java.util.Iterator;
class BigSet<T> implements Set<T>{
private T item;
private Set<T> rest;
public BigSet(T item, Set<T> rest){
//TODO
this.item = item;
this.rest = rest;
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
private boolean returned = false;
private Iterator<T> restI = rest.iterator();
@Override
public boolean hasNext() {
return restI.hasNext();
}
@Override
public T next() {
if (!returned) {
returned = true;
return item;
} else {
return restI.next();
}
}
};
}
@Override
public int size() {
int count = 1;
count += rest.size();
return count;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object el) {
if(el == item) {
return true;
} else {
return rest.contains(el);
}
}
@Override
public Set<T> union(Set<T> other) {
if (other == EmptySet.getInstance()) {
return this;
} else if (other instanceof SingeltonSet<T> && !contains(other.iterator().next())) {
return new BigSet<T>(other.iterator().next(), this);
} else {
return new BigSet<T>(item, rest.union(other));
}
}
@Override
public Set<T> add(T element) {
if (contains(element)) {
return this;
} else {
return new BigSet<T>(element, this);
}
}
@Override
public Set<T> intersection(Set<T> other) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'intersection'");
}
@Override
public boolean subsetOf(Set<?> other) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'subsetOf'");
}
}

View File

@ -0,0 +1,71 @@
package uebung09.collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
class EmptySet<T> implements Set<T> {
private static EmptySet<?> INSTANCE;
private EmptySet() {}
@SuppressWarnings("unchecked")
public static <T> EmptySet<T> getInstance() {
if (INSTANCE == null) {
INSTANCE = new EmptySet<>();
}
return (EmptySet<T>) INSTANCE;
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
@Override
public boolean hasNext() {
return false;
}
@Override
public T next() {
throw new NoSuchElementException();
}
};
}
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return true;
}
@Override
public boolean contains(Object el) {
return false;
}
@Override
public Set<T> union(Set<T> other) {
return other;
}
@Override
public Set<T> add(T element) {
return new SingeltonSet<T>(element);
}
@Override
public Set<T> intersection(Set<T> other) {
return other;
}
@Override
public boolean subsetOf(Set<?> other) {
return true;
}
}

View File

@ -38,10 +38,10 @@ public interface Set<E> extends Iterable<E> {
/**
* returns the set resulting from adding the specified element to this set.
*
* @param element an element (must not be null)
* @param elem an element (must not be null)
* @return the set resulting from adding the specified element to this set.
*/
Set<E> add(E element);
Set<E> add(E elem);
/**
* Returns the intersection of this set and the specified set.

View File

@ -10,9 +10,8 @@ public class SetFactory {
* @param <T> the element type of the returned set.
* @return the empty set.
*/
static <T> Set<T> create() {
// TODO implement
return null;
public static <T> Set<T> create() {
return EmptySet.getInstance();
}
/**
@ -22,9 +21,8 @@ public class SetFactory {
* @param <T> the element type of the returned set.
* @return the singleton set containing the specified element.
*/
static <T> Set<T> create(T element) {
// TODO implement
return null;
public static <T> Set<T> create(T element) {
return new SingeltonSet<T>(element);
}
/**
@ -34,8 +32,14 @@ public class SetFactory {
* @param <T> the element type of the returned set.
* @return a set containing the specified elements.
*/
static <T> Set<T> create(T... elems) {
// TODO implement
return null;
public static <T> Set<T> create(T... elems) {
// BigSet<T> rtn = new BigSet<T>(elems[1], new SingeltonSet<>(elems[1]));
Set<T> set = EmptySet.getInstance();
for(T elem : elems) {
set = set.add(elem);
}
return set;
}
}

View File

@ -0,0 +1,77 @@
package uebung09.collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
class SingeltonSet<T> implements Set<T> {
private T item;
public SingeltonSet(T item) {
this.item = item;
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
boolean returned = false;
@Override
public boolean hasNext() {
return !returned;
}
@Override
public T next() {
if (!returned) {
returned = true;
return item;
} else {
throw new NoSuchElementException();
}
}
};
}
@Override
public int size() {
return 1;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object el) {
return el == item;
}
@Override
public Set<T> union(Set<T> other) {
//TODO
return null;
}
@Override
public Set<T> add(T element) {
if (element == item) {
return this;
} else {
return new BigSet<T>(item, new SingeltonSet<T>(element));
}
}
@Override
public Set<T> intersection(Set<T> other) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'intersection'");
}
@Override
public boolean subsetOf(Set<?> other) {
throw new UnsupportedOperationException();
}
}