Hack 1.a: Watch the college board video Classes and Objects Blog and Define the details of a Class: Access modifier, constructor, modifiers/setters, getters, etc.

Hack 1.b: Build example code in jupyter notebook with Linked List, Queues, and Stacks.

// example class
class ExampleLLQS {
    
    // Queue testing, inits queue 
    public void queueTesting() {
        Queue<Integer> q = new LinkedList<Integer>();

        System.out.println("Adding to Queue");

        for (int i = 1; i < 10; i = i + 2) {
            q.add(i);
            printQueue(q);
            System.out.println();
        }

        queueToStack(q);

    }

    // Prints queue regardless of type
    public void printQueue(Queue<?> q) {
        for (Object i : q) {
            System.out.print(i);
        }
    }

    public void queueToStack(Queue<?> q) {
        // create new stack with type T
        Stack<Object> tempStack = new Stack<>();

        System.out.println("");
        System.out.println("Queue to Stack");

        while ( !q.isEmpty() ) {
            // push queue element to stack and remove from queue
            tempStack.push(q.remove());
            printStack(tempStack);
            System.out.println();
        }
    }

    // Print stack
    public void printStack(Stack<?> s) {
        for (Object i : s) {
            System.out.print(i);
        }
    }


    public static void main(String[] args) {
        ExampleLLQS e = new ExampleLLQS();
        e.queueTesting();
    }
}
ExampleLLQS.main(null);
Adding to Queue
1
13
135
1357
13579

Queue to Stack
1
13
135
1357
13579

Code Hacks

Challenge #1, Add and Delete elements from Queue. Working with the code that is given, you will need to adjust Add and write Delete, to output from the Queue as follows.

import java.util.Iterator;

/**
 * Queue Iterator
 *
 * 1. "has a" current reference in Queue
 * 2. supports iterable required methods for next that returns a generic T Object
 */
/**
 *  Implementation of a Double Linked List;  forward and backward links point to adjacent Nodes.
 *
 */

 public class LinkedList<T>
 {
     private T data;
     private LinkedList<T> prevNode, nextNode;
 
     /**
      *  Constructs a new element
      *
      * @param  data, data of object
      * @param  node, previous node
      */
     public LinkedList(T data, LinkedList<T> node)
     {
         this.setData(data);
         this.setPrevNode(node);
         this.setNextNode(null);
     }
 
     /**
      *  Clone an object,
      *
      * @param  node  object to clone
      */
     public LinkedList(LinkedList<T> node)
     {
         this.setData(node.data);
         this.setPrevNode(node.prevNode);
         this.setNextNode(node.nextNode);
     }
 
     /**
      *  Setter for T data in DoubleLinkedNode object
      *
      * @param  data, update data of object
      */
     public void setData(T data)
     {
         this.data = data;
     }
 
     /**
      *  Returns T data for this element
      *
      * @return  data associated with object
      */
     public T getData()
     {
         return this.data;
     }
 
     /**
      *  Setter for prevNode in DoubleLinkedNode object
      *
      * @param node, prevNode to current Object
      */
     public void setPrevNode(LinkedList<T> node)
     {
         this.prevNode = node;
     }
 
     /**
      *  Setter for nextNode in DoubleLinkedNode object
      *
      * @param node, nextNode to current Object
      */
     public void setNextNode(LinkedList<T> node)
     {
         this.nextNode = node;
     }
 
 
     /**
      *  Returns reference to previous object in list
      *
      * @return  the previous object in the list
      */
     public LinkedList<T> getPrevious()
     {
         return this.prevNode;
     }
 
     /**
      *  Returns reference to next object in list
      *
      * @return  the next object in the list
      */
     public LinkedList<T> getNext()
     {
         return this.nextNode;
     }
 
 }

 class QueueIterator<T> implements Iterator<T> {
    LinkedList<T> current;  // current element in iteration

    // QueueIterator is pointed to the head of the list for iteration
    public QueueIterator(LinkedList<T> head) {
        current = head;
    }

    // hasNext informs if next element exists
    public boolean hasNext() {
        return current != null;
    }

    // next returns data object and advances to next position in queue
    public T next() {
        T data = current.getData();
        current = current.getNext();
        return data;
    }
}

/**
 * Queue: custom implementation
 * @author     John Mortensen
 *
 * 1. Uses custom LinkedList of Generic type T
 * 2. Implements Iterable
 * 3. "has a" LinkedList for head and tail
 */
public class Queue<T> implements Iterable<T> {
    LinkedList<T> head = null, tail = null;

    /**
     *  Add a new object at the end of the Queue,
     *
     * @param  data,  is the data to be inserted in the Queue.
     */
    public void add(T data) {
        // add new object to end of Queue
        LinkedList<T> tail = new LinkedList<>(data, null);

        if (this.head == null)  // initial condition
            this.head = this.tail = tail;
        else {  // nodes in queue
            this.tail.setNextNode(tail); // current tail points to new tail
            this.tail = tail;  // update tail
        }
    }

    /**
     *  Returns the data of head.
     *
     * @return  data, the dequeued data
     */
    public T delete() {
        T data = this.peek();
        if (this.tail != null) { // initial condition
            this.head = this.head.getNext(); // current tail points to new tail
            if (this.head != null) {
                this.head.setPrevNode(tail);
            }
        }
        return data;
    }

    /**
     *  Returns the data of head.
     *
     * @return  this.head.getData(), the head data in Queue.
     */
    public T peek() {
        return this.head.getData();
    }

    /**
     *  Returns the head object.
     *
     * @return  this.head, the head object in Queue.
     */
    public LinkedList<T> getHead() {
        return this.head;
    }

    /**
     *  Returns the tail object.
     *
     * @return  this.tail, the last object in Queue
     */
    public LinkedList<T> getTail() {
        return this.tail;
    }

    /**
     *  Returns the iterator object.
     *
     * @return  this, instance of object
     */
    public Iterator<T> iterator() {
        return new QueueIterator<>(this.head);
    }
}

public class QueueManager<T> {
    // queue data
    private final String name; // name of queue
    private int count = 0; // number of objects in queue
    public final Queue<T> queue = new Queue<>(); // queue object

    /**
     *  Queue constructor
     *  Title with empty queue
     */
    public QueueManager(String name) {
        this.name = name;
    }

    /**
     *  Queue constructor
     *  Title with series of Arrays of Objects
     */
    public QueueManager(String name, T[]... seriesOfObjects) {
        this.name = name;
        this.addList(seriesOfObjects);
    }

    /**
     * Add a list of objects to queue
     */
    public void addList(T[]... seriesOfObjects) {  //accepts multiple generic T lists
        for (T[] objects: seriesOfObjects)
            for (T data : objects) {
                this.queue.add(data);
                this.count++;
                printQueue();
            }
    }

    // add single object to queue
    public void add(T data) {
        this.queue.add(data);
        this.count++;
        printQueue();
    }

    // delete objects from queue
    public void deleteList(int count) {
        for (int i = 0; i < count; i++) {
            this.queue.delete();
            this.count--;
            printQueue();
        }
    }

    /**
     * Print any array objects from queue
     */
    public void printQueue() {
        System.out.println(this.name + " count: " + count);
        System.out.print(this.name + " data: ");
        for (T data : queue)
            System.out.print(data + " ");
        System.out.println();
    }
}

class QueueTester {
    public static void main(String[] args) {
        // create queue manager
        QueueManager<Integer> qm = new QueueManager<>("Queue");

        // add objects to queue
        qm.addList(new Integer[]{1, 2, 3, 4, 5});

        // delete objects from queue
        qm.deleteList(5);
    }
}

QueueTester.main(null);
Queue count: 1
Queue data: 1 
Queue count: 2
Queue data: 1 2 
Queue count: 3
Queue data: 1 2 3 
Queue count: 4
Queue data: 1 2 3 4 
Queue count: 5
Queue data: 1 2 3 4 5 
Queue count: 4
Queue data: 2 3 4 5 
Queue count: 3
Queue data: 3 4 5 
Queue count: 2
Queue data: 4 5 
Queue count: 1
Queue data: 5 
Queue count: 0
Queue data: