Question

Use Java: Also: Please include screenshots if possible. Create a class called AbstractStackTest. Write an abstract...

Use Java:

Also: Please include screenshots if possible.

  1. Create a class called AbstractStackTest.
    1. Write an abstract method called makeStack that returns a Stack of Strings. Use the Stack interface as the return type, not a specific implementation!
  2. Write a class named NodeStackTest that extends your AbstractStackTest class. Implement the makeStack method to return a NodeStack.
  1. Repeat parts 1 and 2 for the Queue interface and the NodeQueue implementation.
  2. Write a new stack implementation, ArrayStack. It should be generic and use an array to store the values in the stack.
    1. Note that, in Java, you cannot create an array of a generic type, i.e. T[] array = new T[10]; will cause a compiler error. Instead, use an Object array to store the elements in your stack. This means that you will have to cast the value that you return in your peek and dequeue methods, e.g. return (T)array[0];
    1. Create your array to be some small initial size (e.g. 2). If your array fills up, you will need to make a new, larger array (remember Arrays.copyOf()).You should double the size of the array each time. Do not worry about making the array smaller! Once you make a new array, don’t forget to update your stack’s field to point to the new array!
  3. Write a new class called ArrayStackTest that extends AbstractStackTest. Implement the makeStack method to return an instance of your ArrayStack class. Run your tests to make sure that they all pass on your ArrayStack implementation.
  4. Repeat parts 4 and 7 but implement an ArrayQueue.

Use existing code:

public interface Stack {

    public void push(T value);

    T pop();

    T peek();

    int size();

}

-------------------

public interface Queue {

    void enqueue (T value);

    T dequeue();

    T peek();

    int size();
}

-------------------

public class NodeStack implements Stack {

    private Node top;
    private int size;

    public NodeStack() {
        top = null;
        size = 0;
    }

    @Override
    public void push(T value) {
        Node node = new Node<>(value);
        node.setNext(top);
        this.top = node;
        this.size++;
    }

    @Override
    public T pop() {
        T value = top.getValue();
        this.top = top.getNext();
        size--;
        return value;
    }

    @Override
    public T peek() {
        return top.getValue();
    }

    @Override
    public int size() {
        return this.size;
    }
}

-------------------

public class NodeQueue implements Queue {

    private Node front;
    private Node back;
    private int size;


    public NodeQueue() {
        this.front = null;
        this.back = null;
        this.size = 0;
    }

    @Override
    public void enqueue(T value) {
        Node node = new Node<>(value);

        if(front ==null){
            front = node;
            back=node;
        } else {
            back.setNext(node);
            back = node;
        }

        size++;
    }

    @Override
    public T peek() {
        return front.getValue();
    }


    @Override
    public T dequeue() {
        T value = front.getValue();
        front = front.getNext();

        if(front ==null){
            back = null;
        }
        size--;
        return value;
    }

    @Override
    public int size() {
        return size;
    }
}

------------------

public class Node<T> {

    private T value;
    private Node<T> next;

    public Node(T value) {
        this.value = value;
        this.next=null;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public Node<T> getNext() {
        return next;
    }

    public void setNext(Node<T> next) {
        this.next = next;
    }
}

Homework Answers

Answer #1

Hello! :)

Here are the complete codes to solve the assignment:

AbstractQueueTest.java:

public abstract class AbstractQueueTest
{
    public abstract Queue<String> makeQueue();
}

AbstractStackTest.java:

public abstract class AbstractStackTest
{
    public abstract Stack<String> makeStack();
}

ArrayQueue.java:

import java.util.Arrays;

public class ArrayQueue<T> implements Queue<T>
{
    private Object data[];
    private int front;
    private int back;
    private int size;

    public ArrayQueue()
    {
        data = new Object[2];
        front = 0;
        back = 0;
        size = 0;
    }

    @Override
    public void enqueue(T value)
    {
        if(front == data.length)
        {
            final int previousLength = data.length;
            final int nextLength = previousLength * 2;
            data = Arrays.copyOf(data, nextLength);
        }
        ++size;
        data[front++] = value;
    }

    @Override
    public T peek()
    {
        return (T)data[back];
    }


    @Override
    public T dequeue()
    {
        --size;
        return (T)data[back++];
    }

    @Override
    public int size()
    {
        return this.size;
    }
}

ArrayQueueTest.java:

public class ArrayQueueTest extends AbstractQueueTest
{
    @Override
    public Queue<String> makeQueue()
    {
        return new ArrayQueue<>();
    }
}

ArrayStack.java:

import java.util.Arrays;

public class ArrayStack<T> implements Stack<T>
{
    private Object data[];
    private int size;

    public ArrayStack()
    {
        data = new Object[2];
        size = 0;
    }

    @Override
    public void push(T value)
    {
        if(size == data.length)
        {
            final int previousLength = data.length;
            final int nextLength = previousLength * 2;
            data = Arrays.copyOf(data, nextLength);
        }
        
        data[size++] = value;
    }

    @Override
    public T pop()
    {
        return (T)data[--size];
    }

    @Override
    public T peek()
    {
        return (T)data[size - 1];
    }

    @Override
    public int size()
    {
        return this.size;
    }
}

ArrayStackTest.java:

public class ArrayStackTest extends AbstractStackTest
{
    @Override
    public Stack<String> makeStack()
    {
        return new ArrayStack<>();
    }
}

Node.java:

public class Node<T> {

    private T value;
    private Node<T> next;

    public Node(T value) {
        this.value = value;
        this.next=null;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

    public Node<T> getNext() {
        return next;
    }

    public void setNext(Node<T> next) {
        this.next = next;
    }
}

NodeQueue.java:

public class NodeQueue<T> implements Queue<T> {

    private Node front;
    private Node back;
    private int size;


    public NodeQueue() {
        this.front = null;
        this.back = null;
        this.size = 0;
    }

    @Override
    public void enqueue(T value) {
        Node node = new Node<>(value);

        if(front ==null){
            front = node;
            back=node;
        } else {
            back.setNext(node);
            back = node;
        }

        size++;
    }

    @Override
    public T peek() {
        return (T)front.getValue();
    }


    @Override
    public T dequeue() {
        T value = (T)front.getValue();
        front = front.getNext();

        if(front ==null){
            back = null;
        }
        size--;
        return value;
    }

    @Override
    public int size() {
        return size;
    }
}

NodeQueueTest.java:

public class NodeQueueTest extends AbstractQueueTest
{
    public Queue<String> makeQueue()
    {
        return new NodeQueue<>();
    }
}

NodeStack.java:

public class NodeStack<T> implements Stack<T> {

    private Node top;
    private int size;

    public NodeStack() {
        top = null;
        size = 0;
    }

    @Override
    public void push(T value) {
        Node node = new Node<>(value);
        node.setNext(top);
        this.top = node;
        this.size++;
    }

    @Override
    public T pop() {
        T value = (T)top.getValue();
        this.top = top.getNext();
        size--;
        return value;
    }

    @Override
    public T peek() {
        return (T)top.getValue();
    }

    @Override
    public int size() {
        return this.size;
    }
}

NodeStackTest.java:

public class NodeStackTest extends AbstractStackTest
{
    @Override
    public Stack<String> makeStack()
    {
        return new NodeStack<>();
    }
}

Queue.java:

public interface Queue<T> {

    void enqueue (T value);

    T dequeue();

    T peek();

    int size();
}

Stack.java:

public interface Stack<T> {

    public void push(T value);

    T pop();

    T peek();

    int size();
}

Test.java:

public class Test
{
    private static int status;

    public static void test(boolean condition)
    {
        final int line = Thread.currentThread().getStackTrace()[2].getLineNumber();

        if(condition)
        {
            System.out.print("TEST PASSED");
        }
        else
        {
            System.out.print("TEST FAILED");
            status = 1;
        }

        System.out.println(" at line: " + line);
    }

    public static void main(String[] args)
    {
        status = 0;

        // Testing ArrayStack

        Stack arrayStack = new ArrayStackTest().makeStack();

        test(arrayStack.size() == 0);

        arrayStack.push("A");
        arrayStack.push("B");
        arrayStack.push("C");
        arrayStack.push("D");

        test(arrayStack.size() == 4);

        test(arrayStack.peek() == "D");
        test(arrayStack.pop() == "D");
        test(arrayStack.peek() == "C");
        test(arrayStack.pop() == "C");

        // Testing NodeStack

        Stack nodeStack = new NodeStackTest().makeStack();

        test(nodeStack.size() == 0);

        nodeStack.push("A");
        nodeStack.push("B");
        nodeStack.push("C");
        nodeStack.push("D");

        test(nodeStack.size() == 4);

        test(nodeStack.peek() == "D");
        test(nodeStack.pop() == "D");
        test(nodeStack.peek() == "C");
        test(nodeStack.pop() == "C");

        // Testing ArrayQueue

        Queue arrayQueue = new ArrayQueueTest().makeQueue();

        test(arrayQueue.size() == 0);

        arrayQueue.enqueue("A");
        arrayQueue.enqueue("B");
        arrayQueue.enqueue("C");
        arrayQueue.enqueue("D");

        test(arrayQueue.size() == 4);

        test(arrayQueue.peek() == "A");
        test(arrayQueue.dequeue() == "A");
        test(arrayQueue.peek() == "B");
        test(arrayQueue.dequeue() == "B");

        // Testing NodeQueue

        Queue nodeQueue = new NodeQueueTest().makeQueue();

        test(nodeQueue.size() == 0);

        nodeQueue.enqueue("A");
        nodeQueue.enqueue("B");
        nodeQueue.enqueue("C");
        nodeQueue.enqueue("D");

        test(nodeQueue.size() == 4);

        test(nodeQueue.peek() == "A");
        test(nodeQueue.dequeue() == "A");
        test(nodeQueue.peek() == "B");
        test(nodeQueue.dequeue() == "B");

        System.exit(status);
    }
}

Here is a screenshot of a demo run:

Hope this helps! :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue...
You must alter the Queue class you created in L5 to make it a CIRCULAR Queue class . Call your class Queue. it must be a template class. public class Queue { } I have put a driver program in the module . It is called CircularQueue.java This driver program should then run with your Queue class (no modifications allowed to the driver program). Your Queue class should have at least the following methods: one or more constructors, enqueue, dequeue,...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue:...
(Please use Java Eclipse) QUESTION 1: For the question below, assume the following implementation of LinkedQueue: public static final class LinkedQueue<T> implements QueueInterface<T> {     private Node<T> firstNode;     private Node<T> lastNode;     public LinkedQueue() {         firstNode = null;         lastNode = null;     }     @Override     public T getFront() {         if (isEmpty()) {             return null;         }         return firstNode.getData();     }     @Override     public boolean isEmpty() {         return firstNode == null;    ...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
Use the TestTime.java/TestTime.cpp le to compare the two queue implementations. Note that enqueue function when implemented...
Use the TestTime.java/TestTime.cpp le to compare the two queue implementations. Note that enqueue function when implemented using an array has O(1) complexity, and when using two arrays has O(n) complexity, where n is the current size of the queue. So, the two stack implementation should take more time, which is substantiated by the experiment (time output). public class TestTime {    public static void main(String[] args) throws Exception {        for (int maxSize = 10000; maxSize <= 50000; maxSize...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return...
class Car{ private String make; public Car(String make){ this.make = make; } public String toString(){ return make; } } class Node<T>{ public T data; public Node next; public Node(T data){ this.data = data; this.next = null; } } public class StackLinkedList<T>{ //Single field of type Node    //to represent the front of the stack //operation push() /* The push operation is equivalent to the inserting a node at the head of the list. */ public void push(T data){ } //operation...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class...
1. BQUEUE.h #pragma once #include class bqnode { public: int time; bqnode *prev, *next; }; class BQUEUE { public: BQUEUE(); ~BQUEUE(); BQUEUE(const BQUEUE &); void Enqueue(int); void Dequeue(); void Print(); private: bqnode * front; //use ONLY one pointer }; 2. BQUEUE.cpp #include "BQUEUE.h" using namespace std; BQUEUE::BQUEUE() { } BQUEUE::~BQUEUE() { } BQUEUE::BQUEUE(const BQUEUE & otherList) { if (otherList.front == NULL) return; front = new bqnode(); bqnode *curr = front; bqnode *oldnode = otherList.front; curr->time = oldnode->time; curr->next = NULL;...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is...
Please solve this problem in java. import java.util.Arrays; public class PriorityQueue { /* This class is finished for you. */ private static class Customer implements Comparable { private double donation; public Customer(double donation) { this.donation = donation; } public double getDonation() { return donation; } public void donate(double amount) { donation += amount; } public int compareTo(Customer other) { double diff = donation - other.donation; if (diff < 0) { return -1; } else if (diff > 0) { return...
(JAVA) Why is my toString method not printing out the last node? It will not print...
(JAVA) Why is my toString method not printing out the last node? It will not print out "A" which is for Alice. Everything else is working just fine except for this. package driver; import exception.StackException; import stack.*; public class StackDriver { public static void main(String[] args) throws Exception { StackInterface<Painting> painting = new LinkedStack <Painting>(); try { System.out.println("Peeking at top of player stack.\n" + painting.peek()); }catch (StackException e) { System.out.println(e); System.out.println("Let's double check if it's empty: " + painting.isEmpty()); }...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ public class QueueLinkedList<T> { private Node front; private Node rear; private int size; public QueueLinkedList() { } public void enqueue(T info) { } public T dequeue() { } public boolean isEmpty() } public int size() { } } Run this program ( Homework2Driver.java ) to test. Comment out...
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists....
Write in Java (Not Javascript) Provide an implementation of priority queue using double-ended doubly linked lists. Recall that double-ended means keeping first and last references and doubly linked feature allows us to go backwards, using a prev reference at each Link. Also, note that the greater the number, the lower the priority. For instance, 2 is of higher priority compared to 5. Specifically, write a class LinkedListPriorityQ which implements the priority queue methods: boolean isEmpty() void enqueue(int item) int dequeue()...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT