Courses‎ > ‎APCS - Term 1‎ > ‎Konstantinovich‎ > ‎

2019-03-21 MyDeque

posted Mar 21, 2019, 5:53 AM by Konstantinovich Samuel   [ updated Mar 21, 2019, 11:37 AM ]
Due Monday, you will have an extension to the lab on Monday. I will ultimately test both the Deque and the Extension together.

Add: (push / en-queue)
Get: return but NOT remove the element. (peek)
Remove: return AND remove the element. (pop / de-queue)

Size,Add, remove, and get should be O(1)
toString O(n) - format:  {a b c d }  / {}  /   {VALUE_VALUE2_VALUE3_}  (space after each value)

public class MyDeque<E>{
  private E[] data;
  private int size, start, end;

  public MyDeque(){  }
  public MyDeque(int initialCapacity){  }
  public int size(){ }
  public String toString(){ }
  public void addFirst(E element){ }
  public void addLast(E element){ }
  public E removeFirst(){ }
  public E removeLast(){ }
  public E getFirst(){ }
  public E getLast(){ }

Note Exceptions:
remove/get  (both first and last) will throw:
NoSuchElementException - if this deque is empty
Add(both first and last) will throw:
NullPointerException - if the specified element is null (this deque does not permit null elements)
To create a generic array you need to create an array of Object, and then cast it to E. This causes a warning, and you can use the SuppressWarnings command to prevent it. This is an appropriate use of the command because we need to instantiate an array of objects and cast it. You would not use this in most other cases.

Suppress the warning in the constructor:

  public MyDeque(){
    data = (E[])new Object[10];

OR suppress the warning on the creation of the array, but you cannot directly assign to the instance variable if you do this. (This would prevent the accidental suppresion of additional warnings)

  public MyDeque(){
    E[] d = (E[])new Object[10];
    data = d;