In: Computer Science
Develop the following class: Class Name: ImprovedArrayBasedStack Access Modifier: public Implements: ImprovedStackInterface Instance variables Name: top Access modifier: private Data type: int Name: stack Access modifier: private Data type: T[] (an array of parameterized type) Constructors Name: ImprovedArrayBasedStack Access modifier: public Parameters: none (default constructor) Task: sets the value of top to -1 sets the stack to refer to an array of Objects with 100 elements which are type cast to T[] Name: ImprovedArrayBasedStack Access modifier: public Parameters: size (data type int) Task: sets the value of top to -1 sets the stack to refer to an array of Objects with the number of elements equal to the size parameter which are type cast to T[] Methods Name: push Access modifier: public Parameters: item (data type T, parameterized type) Return type: void Throws: StackFullException Task: if the value of top is less than the length of the stack minus 1 then increase the value of top by 1 and place the item at the top of the stack, otherwise throw a StackFullException with the message "Not enough room for one item" Name: push Access modifier: public Parameters: item1 (data type T, parameterized type), item2 (data type T, parameterized type) Return type: void Throws: StackFullException Task: if the value of top is less than the length of the stack minus 2, then increase the value of top by 1 and place item1 at the top of the stack, then increase the value of top by 1 and place item2 at the top of the stack, otherwise throw a StackFullException with the message "Not enough room for two items" Name: pop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Task: if the value of top is greater than -1 then remove the item at the top of the stack by replacing it with null and decrease the value of top by 1, otherwise throw a StackEmptyException with the message "No item to remove" Name: doublePop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Task: if the value of top is greater than 0, then remove the item at the top of the stack by replacing it with null and decrease the value of top by 1, then remove the item at the top of the stack by replacing it with null and decrease the value of top by 1, otherwise throw a StackEmptyException with the message "There are less than two items in the stack" Name: top Access modifier: public Parameters: none Return type: T (parameterized type) Throws: StackEmptyException Task: if the value of top is greater than -1 then return the item at the top of the stack, otherwise throw a StackEmptyException with the message "Top attempted on an empty stack"
The Implementation of the all classes are provided below:
// ImprovedStackInterface is an interface which contains all the methods used for stack implementation
public interface ImprovedStackInterface<T> {
public void push(T item ) throws StackFullException;
public void push(T item1, T item2) throws StackFullException;
public void pop() throws StackEmptyException;
public void doublePop() throws StackEmptyException;
public T top() throws StackEmptyException;
}
//ImprovedArrayBasedStack class implements the ImprovedStackInterface as per the question
public class ImprovedArrayBasedStack<T> implements ImprovedStackInterface<T> {
private int top;
private T[] stack;
public ImprovedArrayBasedStack(){
top=-1;
stack = (T[])new Object[100];
}
public ImprovedArrayBasedStack(int size){
top=-1;
stack = (T[])new Object[size];
}
public void push(T item ) throws StackFullException{
if(top<(stack.length-1)){
top=top+1;
stack[top]=item;
}else{
throw new StackFullException("Not enough room for one item");
}
}
public void push(T item1, T item2) throws StackFullException{
if(top<(stack.length-2)){
top=top+1;
stack[top]=item1;
top=top+1;
stack[top]=item2;
}else{
throw new StackFullException("Not enough room for two items");
}
}
public void pop() throws StackEmptyException{
if(top>-1){
stack[top]=null;
top=top-1;
}else{
throw new StackEmptyException("No item to remove");
}
}
public void doublePop() throws StackEmptyException{
if(top>0){
stack[top]=null;
top=top-1;
stack[top]=null;
top=top-1;
}else{
throw new StackEmptyException("There are less than two items in the stack");
}
}
public T top() throws StackEmptyException{
if(top>-1){
return stack[top];
}else{
throw new StackException("Top attempted on an empty stack");
}
}
}
// StackFullException class is used to define the custom exception when stack is full
public class StackFullException extends Exception {
StackFullException(String s){
super(s);
}
}
//StackEmptyException is used to create a custom exception when stack is empty
public class StackEmptyException extends Exception {
StackEmptyException(String s){
super(s);
}
}
//DriverClassForStackGenerics is used to test whether all operations are working properly or not.
public class DriverClassForStackGenerics {
public static <T> void main(String[] args) throws StackFullException, StackEmptyException{
ImprovedArrayBasedStack st= new ImprovedArrayBasedStack(5);
st.push(10);
System.out.println("The Top elment in the stack after st.push(10) is "+st.top());
st.push(11);
System.out.println("The Top elment in the stack after st.push(11) is "+st.top());
st.push(12);
System.out.println("The Top elment in the stack after st.push(12) is "+st.top());
st.push(13,14);
System.out.println("The Top elment in the stack after st.push(12) is "+st.top());
st.pop();
System.out.println("The Top elment in the stack after st.pop() is "+st.top());
st.doublePop();
System.out.println("The Top elment in the stack after st.doublePop(10) is "+st.top());
st.pop();
System.out.println("The Top elment in the stack after st.doublePop(10) is "+st.top());
}
}
The output of the above program is as below:
The
sample output of all the exceptions handled are :