java Queue

Goal

By this program assignment, the students will experience the way of constructing a data structure by using a well designed Java class.

Requirements

Design a Java class, named QueueDS, implementing the java.util.Queue interface in J2SE by using the java.util.Vector class in J2SE.1. Design a Java class, named QueueDS, implementing the java.util.Queue interface in J2SE by using the java.util.Vector class in J2SE.

Update:

大大的add 和 element的回傳質不符服,能請教大大問題嗎??

感恩!!!

Update 2:

小弟是在空格後才出現

error: unreachable statement標示的

請大大詳解 感恩~~~

1 Answer

Rating
  • 8 years ago
    Favorite Answer

    Implementing java.util.Queue also requires implementing the super interfaces java.util.Collection and java.util.Iterable to beat the compiler. Is empty implementation possible for those super interfaces?

    2011-11-16 12:31:46 補充:

    請參考我的做法

    importjava.util.*; publicclass QueueDS<E> implements Queue<E> { private Vector<E> v; int capacity; public QueueDS(int capacity) { v = new Vector<E>(capacity, 0); this.capacity = capacity; } publicboolean add(E e) { if (v.size() >= capacity) throw newIllegalStateException(); return v.add(e); } publicboolean offer(E e) { if (v.size() >= capacity) return false; return v.add(e); } publicE remove() { if (v.size() == 0) throw new NoSuchElementException(); return v.remove(v.size() - 1); } publicE poll() { if (v.size() == 0) return null; return v.remove(v.size() - 1); } publicE element() { if (v.size() == 0) throw newNoSuchElementException(); return v.get(v.size() - 1); } publicE peek() { if (v.size() == 0) return null; return v.get(v.size() - 1); } // empty implementation ofjava.util.Collection public boolean addAll(Collection c) { returnfalse; } public void clear() {} public boolean contains(Object o) { returnfalse; } public boolean containsAll(Collection c) {return false; } public boolean equals(Object o) { returnfalse; } public int hashCode() { return 0; } public boolean isEmpty() { return false; } public Iterator<E> iterator() { returnnull; } public boolean remove(Object o) { return false;} public boolean removeAll(Collection c) {return false; } public boolean retainAll(Collection c) {return false; } public int size() { return 0; } public Object[] toArray() { return null; } public <T>T[] toArray(T[] a) { returnnull; } }

    2011-11-17 10:54:57 補充:

    add() 和 element() 中少了一個空格

    請將 throw newIllegalStateException() 更正為 throw new IllegalStateException()

    2011-11-21 10:31:33 補充:

    我用copy/paste的方式重新建立一個新的類別,再解決空格編排錯誤問題後,javac編譯無誤,麻煩將你遇到的問題說明地再詳細一點。

Still have questions? Get your answers by asking now.