Create a stack interface Stack that uses a generic type T and has the following
ID: 3850587 • Letter: C
Question
Create a stack interface Stack that uses a generic type T and has the following abstract methods: isEmpty, is Full, peek (returns T), pop(returns T), void push(T), void clear() to clear the contents of the stack, and int size (returns the number of elements in the stack). void reverse() reverses the contents of the stack (in your implementation you can use an additional local stack in your method to help you with the reverse operation.) void display() prints out the contents of the stack Write your two stack implementations (ArrayStack and ListStack) using the stack interface. Your stack implementations must have all instance variables as private. Both implementations must a constructor that initializes the capacity of the stack. For the ListStack, the stack is full when the size reaches the initial capacity. You must also implement public String toString() in both classes. It returns a String representation of the stack contents. Create a simple main class QuestionOne that creates two stacks of Integers of capacity = 20 one that uses the ArrayStack and the other uses the ListStack implementations. Push 20 values in each: in the first (1, 2, 3, ..., 20) and in the second (20, 19, .., 13, ..., l). Call the following methods for both stacks in the given order: display, reverse, display, peek, pop, pop, reverse, size, isFull, isEmpty, display, clear, display, isEmpty.Explanation / Answer
Stack.java
public interface Stack<T>
{
boolean isEmpty();
boolean isFull();
T peek();
T pop();
void push(T t);
void clear();
int size();
void reverse();
void display();
}
ArrayStack.java
public class ArrayStack<T> implements Stack<T>
{
private int capacity;
private int size=0;
private T[] stack;
ArrayStack(int c)
{
capacity = c;
stack = (T[])new Object[c];
}
public T peek()
{
if(!isEmpty())
{
T t = stack[size()-1];
return t;
}
else System.out.println("Stack is Empty(Underflow)!!!");
return null;
}
public T pop()
{
if(!isEmpty())
{
T t = stack[size()-1];
size--;
return t;
}
else System.out.println("Stack is Empty(Underflow)!!!");
return null;
}
public void push(T element)
{
if(!isFull())
{
stack[size()]=element;
size++;
System.out.println("Element added!!!");
}
else System.out.println("Stack is Full(Overflow)!!!");
}
public void clear()
{
size=0;
}
public int size()
{
return this.size;
}
public boolean isEmpty()
{
return size()==0;
}
public boolean isFull()
{
return capacity<=size();
}
public void reverse()
{
if(!isEmpty())
{
T[] stk = (T[])new Object[capacity];
for(int i=size()-1;i>=0;i--)
{
stk[i] = stack[size()-i-1];
}
stack = stk;
System.out.println("Reverse Completed!!!");
}
else System.out.println("Stack is Empty(Underflow)!!!");
}
public void display()
{
System.out.println(this);
}
public String toString()
{
String str="";
for(int i=0;i<size();i++)
{
str+=stack[i]+" ";
}
return str;
}
}
ListStack.java
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class ListStack<T> implements Stack<T>
{
private int capacity;
private List<T> stack = new ArrayList<T>();
ListStack(int c)
{
capacity = c;
}
public T peek()
{
if(!isEmpty())
{
T t = stack.get(size()-1);
return t;
}
else System.out.println("Stack is Empty(Underflow)!!!");
return null;
}
public T pop()
{
if(!isEmpty())
{
T t = stack.get(size()-1);
stack.remove(size()-1);
return t;
}
else System.out.println("Stack is Empty(Underflow)!!!");
return null;
}
public void push(T element)
{
if(!isFull()){
stack.add(element);
System.out.println("Element added!!!");
}
else System.out.println("Stack is Full(Overflow)!!!");
}
public void clear()
{
stack.clear();
}
public int size()
{
return stack.size();
}
public boolean isEmpty()
{
return size()==0;
}
public boolean isFull()
{
return capacity<=size();
}
public void reverse()
{
if(!isEmpty())
{
Collections.reverse(stack);
System.out.println("Reverse Completed!!!");
}
else System.out.println("Stack is Empty(Underflow)!!!");
}
public void display()
{
System.out.println(this);
}
public String toString()
{
String str="";
for(int i=0;i<size();i++)
{
str+=stack.get(i)+" ";
}
return str;
}
}
QuestionOne.java
public class QuestionOne{
public static void main(String a[])
{
ArrayStack<Integer> stack1 = new ArrayStack<Integer>(20);
ListStack<Integer> stack2 = new ListStack<Integer>(20);
for(int i=1;i<=20;i++)
{
stack1.push(i);
stack2.push(21-i);
}
//methods on ArrayStack
System.out.println("-----------Methods on ArrayStack------------");
stack1.display();
stack1.reverse();
stack1.display();
stack1.peek();
stack1.pop();
stack1.pop();
stack1.reverse();
System.out.println(stack1.size());
System.out.println(stack1.isFull());
System.out.println(stack1.isEmpty());
stack1.display();
stack1.clear();
stack1.display();
System.out.println(stack1.isEmpty());
//methods on ListStack
System.out.println("-----------Methods on ListStack------------");
stack2.display();
stack2.reverse();
stack2.display();
stack2.peek();
stack2.pop();
stack2.pop();
stack2.reverse();
System.out.println(stack2.size());
System.out.println(stack2.isFull());
System.out.println(stack2.isEmpty());
stack2.display();
stack2.clear();
stack2.display();
System.out.println(stack2.isEmpty());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.