Question

In: Computer Science

Develop and test two Java classes: an array-based stack ADT that implements the provided StackInterface.java a...

Develop and test two Java classes:

  1. an array-based stack ADT that implements the provided StackInterface.java
  2. a linked list-based queue ADT that implements the provided QueueInterface.java
  • You may not make any changes to StackInterface.java or QueueInterface.java
  • The classes must be generic
  • The attached generic singly linked list node class, LLNode.java, must be used for the queue implementation; you may not make any modifications to this class
  • Your implementations cannot throw any exceptions

Each ADT must include a toString( ) method:

  • The stack's toString( ) must display items in the stack in order from top to bottom
  • The queue's toString( ) must display the items in the queue in order from front to rear

You are encouraged to review the rubric below for details on how the project will be graded.

This what i need to work with below:

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

package interfaces;

public interface StackInterface {

   void push(E element); // add an element to the stack - always at the "top"
  
   E pop(); // remove and return the top of the stack
  
   E peek(); // return the top of the stack ... without removing
  
   boolean isEmpty();
  
   boolean isFull();
  
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

package interfaces;

public interface QueueInterface {

   void enqueue(E element); // add an element to the queue - always at the end of the queue
  
   E dequeue(); // remove and return the front of the queue
  
   boolean isEmpty();
  
   boolean isFull();
  
}
-----------------------------------------------------------------------------------------------------------------------------------------------------

package nodes;


public class LLNode {
  
   private E info;
   private LLNode next;
  
   public LLNode(E info) {
       this.info = info;
       next = null;
   }
  
   public E getInfo() {
       return info;
   }
  
   public void setInfo(E info) {
       this.info = info;
   }
  
   public LLNode getNext() {
       return next;
   }
  
   public void setNext(LLNode next) {
       this.next = next;
   }
}

Array doesnt need to be extended when stack is full and the lenght of array can be up to 6

Solutions

Expert Solution

Answer : An array based stack ADT

public class ArrayStack implements Stack {

        private Object[] theArray;
        private int topOfStack;
        private static final int DEFAULT_CAPACITY = 10;

        /**
         * Construct the stack.
         */
        public ArrayStack() {
            theArray = new Object[DEFAULT_CAPACITY];
            topOfStack = -1;
        }

        /**
         * Test if the stack is logically empty.
         *
         * @return true if empty, false otherwise.
         */
        public boolean isEmpty() {
            return topOfStack == -1;
        }

        /**
         * Make the stack logically empty.
         */
        public void makeEmpty() {
            topOfStack = -1;
        }

        /**
         * Get the most recently inserted item in the stack. * Does not alter
         * the stack.
         *
         * @return the most recently inserted item in the stack.
         * @throws UnderflowException if the stack is empty.
         */
        public Object top() {
            if (isEmpty()) {
                throw new UnderflowException("ArrayStack top");
            }
            return theArray[ topOfStack];
        }

        /**
         * Remove the most recently inserted item from the stack.
         *
         * @throws UnderflowException if the stack is empty.
         */
        public void pop() {

            if (isEmpty()) {
                throw new UnderflowException("ArrayStack pop");
            }
            topOfStack--;
        }

        /**
         * Return and remove the most recently inserted item * from the stack.
         *
         * @return the most recently inserted item in the stack.
         * @throws Underflow if the stack is empty.
         */
        public Object topAndPop() {
            if (isEmpty()) {
                throw new UnderflowException("ArrayStack topAndPop");
            }
            return theArray[ topOfStack--];
        }

        /**
         * Insert a new item into the stack.
         *
         * @param x the item to insert.
         */
        public void push(Object x) {
            if (topOfStack + 1 == theArray.length) {
                doubleArray();
            }
            theArray[ ++topOfStack] = x;
        }

        /**
         * Internal method to extend theArray.
         */
        private void doubleArray() {
            Object[] newArray;

            newArray = new Object[theArray.length * 2];
            for (int i = 0; i < theArray.length; i++) {
                newArray[ i] = theArray[ i];
            }
            theArray = newArray;
        }
    }

Related Solutions

write an implementation of the ADT stack that uses a resizeable array to represent the stack...
write an implementation of the ADT stack that uses a resizeable array to represent the stack items. Anytime the stack becomes full, double the size of the array. Maintain the stack's top entry at the end of the array. Please use c++ for this question.
Write an array-based implementation of the ADT list that expands the size of the array of...
Write an array-based implementation of the ADT list that expands the size of the array of list entries as needed so that the list can always accommodate a new entry. Also reduce the size of the array as needed to accommodate several removals. When the size of the array is greater than 20 and the number of entries in the list is less than half the size of the array, reduce the size of the array so that it is...
3.1 Implement the stack ADT using array (4 marks) 3.1.1 Implement the pop() operation in the...
3.1 Implement the stack ADT using array 3.1.1 Implement the pop() operation in the stack (1 mark) Implement a stack class named Stack2540Array using array. The starter code is as follows. The instance variables and most operations are provided. You need to implement the pop operation. Make sure that your program checks whether the stack is empty in the pop operation. import java . io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain strings             red, orange, yellow, blue, indigo, violet write the statement to insert the String element “pink” to the end of the list. Assume the front of the list is on the left. 2. Outline the basic steps to remove a node from the beginning of a list. Completed answers will be given an immediate upvote :)
In java based on the classes provided finish the program per the requirements: ----------------------------------------------Class1 package cardgame;...
In java based on the classes provided finish the program per the requirements: ----------------------------------------------Class1 package cardgame; { private String suit;// spades,diamonds,clubs,hearts private int value; // 1 to 13 public static final String[] SUITS ={"clubs","hearts","diamonds","spades"}; /** * @return the suit */ public String getSuit() { return suit; } /** * @param suit the suit to set */ public void setSuit(String suit) { this.suit = suit; } /** * @return the value */ public int getValue() { return value; } /** *...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm. Create a second Java Application that implements an Insertion sort algorithm to sort the same array. Again, output the array in its original order, then output the array after it has been sorted...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20...
Create a Java Application that implements a Selection sort algorithm to sort an array of 20 unsorted numbers. You should initiate this array yourself and first output the array in its original order, then output the array after it has been sorted by the selection sort algorithm.
(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?
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue...
JAVA: Implement a Queue ADT using a circular array with 5 string elements. Create a Queue object and try various operations below. Queue myQueue = new Queue(); myQueue.enqueue(“CPS 123”); myQueue.enqueue(“CPS 223”); myQueue.enqueue(“CPS 323”); myQueue.dequeue(); myQueue.enqueue(“CPS 113”); myQueue.enqueue(“CPS 153”); string course = myQueue.front(); // course should be CPS 223 size = myQueue.size(); // size should be 4 // output course and size
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students...
(JAVA) Write a program that maintains student test scores in a two-dimesnional array, with the students identified by rows and test scores identified by columns. Ask the user for the number of students and for the number of tests (which will be the size of the two-dimensional array). Populate the array with user input (with numbers in {0, 100} for test scores). Assume that a score >= 60 is a pass for the below. Then, write methods for: Computing the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT