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

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...
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...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
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...
Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
* Make sure you turn in your code (take a screen shot of your code in...
* Make sure you turn in your code (take a screen shot of your code in R)and answers. Conduct the hypothesis and solve questions by using R. 2) A random sample of 12 graduates of a secretarial school averaged 73.2 words per minute with a standard deviation of 7.9 words per minute on a typing test. What can we conclude, at the .05 level, regarding the claim that secretaries at this school average less than 75 words per minute on...
Using the zip file provided, create a secure login/registration app. Make sure that your game pages...
Using the zip file provided, create a secure login/registration app. Make sure that your game pages (at least two) are unavailable to unregistered users. Upload a zip file with your working PHP application. Contents of zip Index.php ----------------- <!DOCTYPE html> <html>     <head>         <meta charset="UTF-8">         <title></title>     </head>     <body>         <?php         // 1. display error message in the session (if any): $_SESSION['error']         // 2. display either the user's name and the game menu or the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT