Question

In: Computer Science

You are provided with a StackInterface to implement your stack, make sure to implement the data...

You are provided with a StackInterface to implement your stack, make sure to implement the data structure using java.util.ArrayList and naming your implementation ArrayListStack (make sure to use generics like you have been for the data structures so far). The provided StackTester assumes you name your implementation that way. Note that you have also been provided a PalindromeTester with the specific isPalindrome() method you are required to implement, following the approach above. The method should take whitespace, case-sensitivity, digits, and symbols into account (see documentation above method).

Additionally, provide your implementation for a CircularArrayQueue<E> based on the java.util.Queue interface, passing the JUnit tests provided for the following six methods.

Queue interface methods to implement:

return type method + description
boolean add(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateException if no space is currently available.
E element()
Retrieves, but does not remove, the head of this queue.
boolean offer(E e)
Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions.
E peek()
Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
E poll()
Retrieves and removes the head of this queue, or returns null if this queue is empty.
E remove()

Retrieves and removes the head of this queue.

StackInterface.java

public interface StackInterface<E> {

    /**
     * Returns true if the stack is empty; otherwise, returns false
     *
     * @return true if empty, false otherwise
     */
    public boolean empty();

    /**
     * Returns the object at the top of the stack without removing it
     *
     * @return reference (shallow copy) of object at top of stack
     */
    public E peek();

    /**
     * Returns the object at the top of the stack and removes it
     *
     * @return reference of removed object from top of stack
     */
    public E pop();

    /**
     * Pushes an item onto the top of the stack and returns the item pushed.
     *
     * @param obj object to push onto top of stack
     * @return item that was pushed
     */
    public E push(E obj);
}

Solutions

Expert Solution

Hi,

I have Implemented the stack using the arraylist, following is the code for it. It should be straightforward to understand. Let me know if you need more help to understand this. To check for palindrome, you need to pass each character as an object to the following stack.


import java.util.ArrayList;

public class ArrayListStack implements StackInterface<Object> {

    ArrayList<Object> stack = new ArrayList<>();

    /**
     * Returns true if the stack is empty; otherwise, returns false
     *
     * @return true if empty, false otherwise
     */
    @Override
    public boolean empty() {
        return stack.isEmpty();
    }

    /**
     * Returns the object at the top of the stack without removing it
     *
     * @return reference (shallow copy) of object at top of stack
     */
    @Override
    public Object peek() {
        if (empty())
            return null;
        else
            return stack.get(stack.size() - 1);
    }

    /**
     * Returns the object at the top of the stack and removes it
     *
     * @return reference of removed object from top of stack
     */
    @Override
    public Object pop() {
        if (empty())
            return null;
        else {
            Object obj = stack.get(stack.size() - 1);
            stack.remove(stack.size() - 1);
            return obj;
        }
    }

    /**
     * Pushes an item onto the top of the stack and returns the item pushed.
     *
     * @param obj object to push onto top of stack
     * @return item that was pushed
     */
    @Override
    public Object push(Object obj) {
        stack.add(obj);
        return null;
    }

}

For the queue, the implementation is as following


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Queue;

public class QueueArrayList implements Queue<Object> {

    private int currentSize; // Current Circular Queue Size
    private int maxSize; // Circular Queue maximum size

    private int rear;// rear position of Circular queue(new element enqueued at rear).
    private int front; // front position of Circular queue(element will be dequeued from front).

    private ArrayList<Object> queue;

    // initialize the class with size of circular queue
    QueueArrayList(int size) {
        maxSize = size;
        queue = new ArrayList<>(maxSize);
        this.front = -1;
        this.rear = -1;
        currentSize = 0;
    }

    // Inserts the specified element into this queue if it is possible to do so
    // immediately without violating capacity restrictions, returning true upon
    // success and throwing an IllegalStateException if no space is currently
    // available.
    @Override
    public boolean add(Object e) {
        // check if queue size if full
        if (currentSize != maxSize) {
            front = (front + 1) % maxSize;
            queue.add(front, e);
            currentSize++;
            return true;
        } else {
            throw new IllegalStateException();
        }
    }

    // Retrieves, but does not remove, the head of this queue.
    @Override
    public Object element() {
        return queue.get(front);
    }

    // Inserts the specified element into this queue if it is possible to do so
    // immediately without violating capacity restrictions.
    @Override
    public boolean offer(Object e) {
        //check if queue size is full
        if (currentSize != maxSize) {
            queue.add(front, e);
            front = (front + 1) % maxSize;
            currentSize++;
            return true;
        }
        return false;
    }

    // Retrieves, but does not remove, the head of this queue, or returns null if
    // this queue is empty.
    @Override
    public Object peek() {
        if (front == -1 && rear == -1)
            return null;
        else
            return queue.get(front);
    }

    // Retrieves and removes the head of this queue, or returns null if this queue
    // is empty.
    @Override
    public Object poll() {
        if (front == -1 && rear == -1)
            return null;
        else {
            return remove();
        }
    }

    // Retrieves and removes the head of this queue.
    @Override
    public Object remove() {
        Object o = null;
        //check if queue is empty
        if (currentSize == 0)
            return null;
        else {
            rear = (rear + 1) % maxSize;
            o = queue.get(rear);
            queue.remove(rear);
            currentSize--;
        }
        return o;
    }

    @Override
    public boolean remove(Object o) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean contains(Object o) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public Iterator<Object> iterator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object[] toArray() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object[] toArray(Object[] a) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public boolean containsAll(Collection c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean addAll(Collection c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean removeAll(Collection c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean retainAll(Collection c) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void clear() {
        // TODO Auto-generated method stub

    }

}

Related Solutions

Implement a stack in C++ using an array, not an array list. Make your stack size...
Implement a stack in C++ using an array, not an array list. Make your stack size 5 when you test it, but do not hardcode this! You should be able to change the size for testing purposes with the change of one variable. DO NOT use Stack class defined in C++ Implement the following methods in your stack class. stack() creates an empty stacks, stack s is new and empty. push(item) adds a new item to the stack s, stacks...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For...
1. Implement the stack abstract data type. Write it as a separate class called Stack. For simplicity the data type to be stored in the stack is String but you can also use generic type. 2. Test your class implementation by calling push() and pop(). If the stack is empty (size equals zero) pop() should return null and print that “stack is empty”. If stack is full (size equals max) push() returns false and prints that “stack is full”. This...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
Implement a class named stack pair that provides a pair of stacks. Make the class a...
Implement a class named stack pair that provides a pair of stacks. Make the class a template class. So, you will have two files: stack pair.h and stack pair.template, following the style of the text. The basic idea is that two stacks can share a single static array. This may be advantageous if only one of the stacks will be in heavy use at any one time. • The class should have various methods to manipulate the stack: T pop...
Start with the provided code for the class linkedListType. Be sure to implement search, insert, and...
Start with the provided code for the class linkedListType. Be sure to implement search, insert, and delete in support of an unordered list (that code is also provided). Now, add a new function called insertLast that adds a new item to the END of the list, instead of to the beginning of the list. Note: the link pointer of the last element of the list is NULL. Test your new function in main. Submit a .zip of your entire project....
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Find quantitative data about something that you are interested in. Make sure to get data on...
Find quantitative data about something that you are interested in. Make sure to get data on at least 50 individuals. 50 football players height a. You don’t need to collect the data yourself, but you do need to find out and explain how the data was collected. b. In order to be useful, this sample needs to be representative of some population.        i. What population is represented by your sample   ii. Describe biases that may result from your sampling method....
Make sure to include comments that explain all your steps (starts with #) Make sure to...
Make sure to include comments that explain all your steps (starts with #) Make sure to include comments that explain all your steps (starts with #) Write a program that prompts the user for a string (a sentence, a word list, single words etc.), counts the number of times each word appears and outputs the total word count and unique word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt...
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
(JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT