In Java. What would the methods of this class look like?
StackADT.java
public interface StackADT<T>
{
/** Adds one element to the top of this stack.
* @param element element to be pushed onto stack
*/
public void push (T element);
/** Removes and returns the top element from this stack.
* @return T element removed from the top of the stack
*/
public T pop();
/** Returns without removing the top element of this
stack.
* @return T element on top of the stack
*/
public T peek();
/** Returns true if this stack contains no elements.
* @return boolean whether or not this stack is empty
*/
public boolean isEmpty();
/** Returns the number of elements in this stack.
* @return int number of elements in this stack
*/
public int size();
/** Returns a string representation of this stack.
* @return String representation of this stack
*/
public String toString();
}
EmptyCollectionsException.java
public class EmptyCollectionException extends
RuntimeException
{
/**
* Sets up this exception with an appropriate message.
* @param collection String representing the name of the
collection
*/
public EmptyCollectionException (String collection)
{
super ("The " + collection + " is empty.");
}
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//ArrayStack.java
public class ArrayStack<T> implements StackADT<T> {
// private fields
private T[] stack;
private int top;// index of top of stack
private static final int DEFAULT_CAPACITY = 20;
// default constructor
public ArrayStack() {
// calling other constructor, passing default capacity
this(DEFAULT_CAPACITY);
}
// constructor taking an initial capacity to initialize stack array, and
// sets top to -1
public ArrayStack(int capacity) {
stack = (T[]) new Object[capacity];
top = -1;
}
@Override
public void push(T element) {
// checking if stack is full
if (top + 1 == stack.length)
expandCapacity(); // expanding capacity
// incrementing top and then adding element at new top index
stack[++top] = element;
}
// helper method to expand capacity by 20
private void expandCapacity() {
// creating a new array, copying items from old array, replacing old
// array with new
T[] newArray = (T[]) new Object[stack.length + 20];
for (int i = 0; i < stack.length; i++)
newArray[i] = stack[i];
stack = newArray;
}
@Override
public T pop() {
if (isEmpty()) {
// stack is empty
throw new EmptyCollectionException("Stack");
}
// otherwise returning value at top index and then decrementing top
return stack[top--];
}
@Override
public T peek() {
if (isEmpty()) {
// stack is empty
throw new EmptyCollectionException("Stack");
}
// otherwise returning value at top index
return stack[top];
}
@Override
public boolean isEmpty() {
return top == -1;
}
@Override
public int size() {
return top + 1; // count of elements
}
public int getTop() {
return top; // top index
}
public int getlength() {
return stack.length; // current capacity of the stack
}
@Override
public String toString() {
if (isEmpty()) {
// empty
return "The stack is empty";
}
String str = "STACK: ";
// appending data of each value from top to bottom into str and
// returning it
for (int i = top; i >= 0; i--) {
str += stack[i];
// appending a ", " if there are more elements
if (i > 0) {
str += ", ";
}
}
return str + "."; // ading "." at the end and returning
}
// main method for testing. remove if not needed
public static void main(String[] args) {
// creating an ArrayStack
ArrayStack<Integer> stk = new ArrayStack<Integer>();
// adding numbers from 1 to 5
for (int i = 1; i <= 5; i++) {
stk.push(i);
// displaying stack after each push
System.out.println(stk);
}
// looping until stk is empty
while (!stk.isEmpty()) {
// popping top element and displaying updated stack
stk.pop();
System.out.println(stk);
}
}
}
/*OUTPUT*/
STACK: 1.
STACK: 2, 1.
STACK: 3, 2, 1.
STACK: 4, 3, 2, 1.
STACK: 5, 4, 3, 2, 1.
STACK: 4, 3, 2, 1.
STACK: 3, 2, 1.
STACK: 2, 1.
STACK: 1.
The stack is empty
In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...
In addition to the base files, three additional files are attached: EmptyCollectionException.java, LinearNode.java, and StackADT.java. These files will need to be added to your Java project. They provide data structure functionality that you will build over. It is suggested that you test if these files have been properly added to your project by confirming that Base_A05Q1.java compiles correctly. Complete the implementation of the ArrayStack class. Specifically, complete the implementations of the isEmpty, size, and toString methods. See Base_A05Q1.java for a...
There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...
Complete the implementation of the LinkedStack class presented in Chapter 13. Specifically, complete the implementations of the peek, isEmpty, size, and toString methods. See Base_A06Q1.java for a starting place and a description of these methods. Here is the base given: /** * Write a description of the program here. * * @author Lewis et al., (your name) * @version (program version) */ import java.util.Iterator; public class Base_A06Q1 { /** * Program entry point for stack testing. * @param args Argument...
I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array. //Array implementation of stacks. import java.util.Arrays; public class ArrayStack implements Stack { //Declare a class constant called DEFAULT_STACK_SIZE with the value 10. private static final int DEFAULT_STACK_SIZE = 10; /* Declare two instance variables: 1. An integer called...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private static...
Java.
Must not use Java API java.util.Stack
/**
A class of stacks whose entries are stored in an array.
Implement all methods in ArrayStack class using resizable
array strategy, i.e. usedoubleArray()
Must throw StackException during exception events in methods:
peek(), pop(), ArrayStack(int initialCapacity)
Do not change or add data fields
Do not add new methods
*/
import java.util.Arrays; public class Arraystack«Т> implements Stack!nterface«T> private TI stack;// Array of stack entries private int topIndex; /7 Index of top entry private...
Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...
I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts { public static void main(String[] args) { Scanner input = new Scanner(System.in); String sentence; String again; do { System.out .println("Enter a sentence, I will tell you if it is a palindrome: ");...
Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...
JAVA Lab Create a class called ArrayBasedStack. Declare the following variables: • data: references an array storing elements in the list • topOfStack: an int value representing the location of the stack top in the array • INITIAL_CAPACITY: the default capacity of the stack public class ArrayBasedStack <E> { private E[] data; private int topOfStack; private static final int INITIAL_CAPACITY = 5; } Add a constructor that will initialize the stack with a user-defined initial capacity. The top of the...