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 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 beginning of the array. Use c++ to write this code.
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 beginning of the array. Use c++ to write this code.
Write a program that implements a stack of integers, and exercises the stack based on commands...
Write a program that implements a stack of integers, and exercises the stack based on commands read from cin. To do this, write a class called Stack with exactly the following members: class Stack { public: bool isEmpty(); // returns true if stack has no elements stored int top(); // returns element from top of the stack // throws runtime_error("stack is empty") int pop(); // returns element from top of the stack and removes it // throws runtime_error("stack is empty")...
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...
Java Data Structures (Stack and Recursion) Using the CODE provided BELOW (WITHOUT IMPORTING any classes from...
Java Data Structures (Stack and Recursion) Using the CODE provided BELOW (WITHOUT IMPORTING any classes from Java Library) modify the classes and add the following methods to the code provided below. 1. Add a recursive method hmTimes() to the CODE BELOW that states how many times a particular value appears on the stack. 2. Add a recursive method insertE() to the CODE BELOW that allows insert a value at the end of the stack. 3. Add a recursive method popLast()...
Problem 1 Write a Java program that implements a two-dimensional array of grades for students in...
Problem 1 Write a Java program that implements a two-dimensional array of grades for students in a class. The grades should be as follows: Row 1: 87, 45 Row 2: 69, 88, 90, 94 Row 3: 79, 87, 94 Compute and print out the maximum number of columns in the array. Hint: use a for loop. (5 Points.) */ /* Problem 2 * Suppose there are 4 candidates in 6 districts running for an election. Let's assume the candidates' names...
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; } /** *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT