In: Computer Science
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedStack, not by calling the previously defined public methods of the class.
a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class and for testing and debugging applications that use the class. Assume each stacked element already provided its own reasonable toString method.
b. intsize()—returns a count of how many items are currently on the stack. Do not add any instance variables to the ArrayBoundedStack class in order to implement this method.
c. void popSome(int count)—removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack .
d. booleanswapStart()—if there are less than two elements on the stack returns false; otherwise it reverses the order of the top two elements on the stack and returns true.
e. T poptop( )—the “classic” pop operation, if the stack is empty it throws StackUnderflowException; otherwise it both removes and returns the top element of the stack.
Answer:
StackUnderflowException.java
package basic;
public class StackUnderflowException extends
RuntimeException
{
public StackUnderflowException()
{
super();
}
public StackUnderflowException(String
message)
{
super(message);
}
}
ArrayBoundedStack.java
package basic;
import java.net.*;
import java.util.Scanner;
public class ArrayBoundedStack{
static int totalStacksize;
static int stack [];
static int top;
ArrayBoundedStack(){
top = -1;
totalStacksize=10;
stack= new
int[10];
}
ArrayBoundedStack(int
totalStacksize){
this.top =
-1;
this.totalStacksize=10;
this.stack= new
int[totalStacksize];
}
void push(int i) {
top=top+1;
stack[top]=i;
}
int poptop() throws
StackUnderflowException {
if( top==-1)
{
throw new
StackUnderflowException("StackUnderFLow");
}
return
stack[top--];
}
void popSome(int count) throws
StackUnderflowException {
for(int i
=0;i<count;i++) {
if(top==-1)
{
throw new StackUnderflowException();
}
top=top-1;
}
}
boolean swapStart() {
if (top<1)
{
return false;
}
int temp =
stack[top];
stack[top]=stack[top-1];
stack[top-1]=temp;
return
true;
}
public String toString() {
int i=0;
String
StackString="";
while(i<=top)
{
if (StackString=="")
StackString=
""+stack[i++];
else
StackString=
StackString+","+stack[i++];
}
return
StackString;
}
public int size(){
int front=
top;
int
count=0;
while(front!=-1)
{
count++;
front--;
}
return
count;
}
public static void main(String[]
args) {
ArrayBoundedStack abs = new
ArrayBoundedStack(20);
abs.push(10);
abs.push(20);
//stack is like [10,20] and top is
point at 20
//for testing to String
method
System.out.println("stack to string
as it is : " + abs.toString());
//for testing size method
System.out.println("size of stack :
" + abs.size());
//testing booleanswapStart()
method
System.out.println( "Top two
elements are swapped : " +abs.swapStart());
//testing popsome() method
System.out.println("poping 2
elements fron stack : " );
abs.popSome(2);
//deleted full stack
System.out.println("------
popSome() ran successfully---------");
//testing poptop method and will
give error stackUnder flow error;
System.out.println( "popped element
is : "+abs.poptop());
}
}
output: