Klausur-2003-02-04
Aus Tudwiki
Lösung von Tudor, ohne Gewähr, siehe dazu Diskussion auf [1]
import java.util.*; public class OrderedSetImpl extends ArrayList implements OrderedSet{ public OrderedSetImpl(){ super(); } public OrderedSetImpl(int initialCapacity){ super(initialCapacity); } public boolean add(Object obj){ if (obj==null){ throw new NullPointerException(); } int previousIndex = indexOf(obj); if (previousIndex != -1) return false; else { super.add(obj); return true; } } public boolean addAll(Collection collection){ throw new UnsupportedOperationException(); } public boolean addAll(int i, Collection collection){ throw new UnsupportedOperationException(); } public Object set(int i, Object obj){ if (obj == null){ throw new NullPointerException(); } if (super.contains(obj)){ return super.get(i); } else { Object altI = super.get(i); super.set(i,obj); return altI; } } public void add(int i, Object obj){ if (obj == null){ throw new NullPointerException(); } if (super.contains(obj)){ } else { super.set(i,obj); } } }
import java.util.*; public class QueueImpl implements Queue { private List myObjects; public QueueImpl(boolean withDuplicates) { if (withDuplicates) { myObjects = new LinkedList(); } else { myObjects = new OrderedSetImpl(); } } public boolean add(Object o) { return myObjects.add(o); } public Object remove(){ return myObjects.remove(0); } public Object front(){ return myObjects.get(0); } public boolean isEmpty(){ return myObjects.isEmpty(); } public int size(){ return myObjects.size(); } public String toString(){ return myObjects.toString(); } }
zur letzten Frage, was bei der main-Methode rauskommt:
Erstmal nix, weil die Variable myQueue nicht als QueueImpl deklariert ist. Wenn man das dann tut ist das Ergebnis:
[Kevin, Frank]