Part A is done, I do not know how to start these steps..
Please find below the solution:
I have covered the bonus section also:
//StackClass.java
import java.util.NoSuchElementException;
public class StackClass {
//instance variables
int topElement,capacity,currentSize;
int stackArray[] = new int[capacity];
//constructor that takes capacity
public StackClass(int capacity)
{
this.capacity=capacity;
topElement=-1;
currentSize=0;
stackArray = new int[capacity];
}
//constructor without parameter
public StackClass()
{
this.capacity=4;
topElement=-1;
currentSize=0;
stackArray = new int[capacity];
}
//push function
public void push(int n)
{
if(isFull())
throw new StackOverflowError("Stack already full");
stackArray[currentSize]=n;
topElement++;
currentSize++;
}
//empty function
public boolean isEmpty()
{
return currentSize==0;
}
public int pop()
{
if(isEmpty())
throw new NoSuchElementException("Stack under flow");
int n= stackArray[topElement];
topElement--;
currentSize--;
return n;
}
//it will return the top element
public int peek()
{
if(isEmpty())
throw new NoSuchElementException("Stack under flow");
return stackArray[topElement];
}
//it will return the capacity
public int getCapacity()
{
return this.capacity;
}
//it will return the size
public int getSize()
{
return this.currentSize;
}
//check if array is full or not
public boolean isFull()
{
return currentSize==capacity;
}
//toString function
public String toString()
{
StringBuffer s = new StringBuffer();
if(isEmpty())
s.append("Empty Stack\n");
else{
int j=topElement;
for(j=topElement;j>=0;j--)
{
s.append(stackArray[j]+"\n");
}
}
return "Current elements in Stack \n"+s;
}
//this is the bonus section
//i have implemeneted
//check for two stack eqaulity
public boolean equals(StackClass anotherStack)
{
boolean flag = true;
if(this.getCapacity()!=anotherStack.getCapacity())
return false;
if(this.getSize()!=anotherStack.getSize())
return false;
int index=0;
int t1[]=new int[this.getSize()];
int t2[]=new int[anotherStack.getSize()];
while(!this.isEmpty())
{
t1[index]=this.pop();
t2[index]=anotherStack.pop();
index++;
}
for(int i=0;i<t1.length;i++)
{
if(t1[i]!=t2[i])
{
flag=false;
break;
}
}
for(int l=t1.length-1;l>=0;l--)
{
this.push(t1[l]);
anotherStack.push(t2[l]);
}
return flag;
}
//copy the stack to another stack
public StackClass(StackClass another)
{
this.stackArray=new int[another.getCapacity()];
this.topElement=-1;
this.currentSize=0;
this.capacity=another.capacity;
int a[] = new int[stackArray.length];
for(int i=0;i<a.length;i++)
{
a[i]=another.pop();
}
for(int j=a.length-1;j>=0;j--)
{
this.push(a[j]);
another.push(a[j]);
}
}
}
//TestStack.java
public class TestStack {
public static void main(String[] args) {
//created a stack
StackClass stack = new StackClass();
//pushed some values
stack.push(34);
stack.push(23);
stack.push(90);
stack.push(12);
//created one more stack by using constructor
StackClass another = new StackClass(stack);
//checking for equality of
//it should return true
//because another is created using copy constructor
System.out.println("Is stack equal to another stack : "+stack.equals(another));
//getting the peek element
System.out.println("Stack peek element : "+stack.peek());
System.out.println("another Stack peek element : "+another.peek());
System.out.println("Is stack full : "+ stack.isFull());
System.out.println("Removing the top element from stack : "+stack.pop());
System.out.println("Checking the top element after removing : "+ stack.peek());
System.out.println("Capacity of stack : "+another.getCapacity());
//it will call toString method:
System.out.println(stack);
}
}
OUTPUT:
UML diagram:
Please do let me know if u have any doubts...
Part A is done, I do not know how to start these steps.. "In computer science,...
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...
PYTHON please help this is very confusing! THANK YOU! Q5 18 Points In this question, we will implement the DrippingStack class. This is a fixed capacity stack; it's size does not increase or decrease automatically. Capacity of this DrippingStack will be determined during initialization. When push is invoked with the DrippingStack at full capacity, rather than throwing a FullStack exception, a more typical semantic is to accept the pushed element at the top while dripping the oldest element from the...
Create a C++ program. Include comment, input and output file. STACK IMPLEMENTATION USING AN link list IMPLEMENT THE FOLLOWING STACK OPERATIONS using a TEMPLATED CLASS. WRITE ALL THE FULL-FUNCTION DEFINITIONS NEEDED for the OPERATIONS. OUTPUT: PRINT ALL THE ELEMENTS ON THE STACK. Stack Operations initializestack: Initializes the stack to an empty state. isEmptyStack: Determines whether the stack is empty. If the stack is empty, it returns the value true; otherwise, it returns the value false. isFul1stack: Determines whether the stack...
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...
Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...
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...
can i please get help on this? i don't know how to do it and I'm so confused. This is in java. This is what I need to do. Thank you so much. In this lab assignment, you are to write a class IntegerSet that represents a set of integers (by definition, a set contains no duplicates). This ADT must be implemented as a singly linked list of integers (with no tail reference and no dummy head node) , but...
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...
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...
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...