Add the following methods to the Array Bounded Stack class a

Add the following methods to the Array Bounded Stack 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 Ar -ray Bounded Stack, not by calling the previously defined public methods of the class. 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 to String method. int size () -returns a count of how many items are currently on the stack. Do not add any instance variables to the Array Bounded Stack class in order to implement this method. void popSome(int count)-removes the top count elements from the stack; throws StackUnderflowException if there are less than count elements on the stack. Boolean swap Start ()-if there are less than two elements on the stack re- turns false; otherwise it reverses the order of the top two elements on the stack and returns true. T poptop () -the \"c|assic\" pop operation, if the stack is empty it throws StackUnderflowException; otherwise it both removes and returns the top element of the stack.

Solution

Program:


interface Stack<T> {
Stack<T> push(T ele) throws StackException;
T pop();
}

class StackException extends Exception
{
public StackException( String message )
{
   super( message ); // Calls Exception\'s constructor
}
}
public class ArrayBoundedStack<T> implements Stack<T> {

private int total;

private Node first;

private class Node {
private T ele;
private Node next;
}

public ArrayBoundedStack() { }

public ArrayBoundedStack<T> push(T ele)
{
Node current = first;
first = new Node();
first.ele = ele;
first.next = current;
total++;
return this;
}

public T pop()
{
if (first == null)
           try {
               throw new StackException(\"Stack UnderFlow\");
           } catch (StackException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
T ele = first.ele;
first = first.next;
total--;
return ele;
}
  
public void popSome(int count)
{
   for (int i = 0; i < count; i++) {
       if (first == null)
           try {
               throw new StackException(\"Stack UnderFlow\");
           } catch (StackException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
T ele = first.ele;
first = first.next;
total--;
       }
  

}

@Override
public String toString()
{
StringBuilder sb = new StringBuilder();
Node tmp = first;
while (tmp != null) {
sb.append(tmp.ele).append(\", \");
tmp = tmp.next;
}
return sb.toString();
}
  
public int getSize()
{
StringBuilder sb = new StringBuilder();
int size=0;
Node tmp = first;
while (tmp != null) {
size++;
tmp = tmp.next;
}
return size;
}
public boolean swapStart()
{
if(total <2)
   return false;
else
{
   Node tmp = first;
   Node sec= tmp.next;
   first.next=sec.next;
sec.next=first;
first=sec;
}
return true;
}
  
public static void main(String[] args) throws StackException {
   ArrayBoundedStack<String> greeting = new ArrayBoundedStack<>();

   greeting.push(\"!\").push(\"World\").push(\"Hello, \");
   System.out.println(\"toString Method Called......Before Swapping : \"+greeting);
   //Implementation Example Of swapStart() method
   greeting.swapStart();
   System.out.println(\"toString Method Called......After Swapping : \"+greeting);
   //Implementation Example Of toString() method
   System.out.println(\"toString Method Called : \"+greeting);
   //Implementation of getting size of Stack
   System.out.println(\"Size of Stack Is: \"+ greeting.getSize());
   //Implementation method of pop method.
   System.out.println(\"Pop Method Is Called\"+greeting.pop());
   System.out.println(\"toString Method Called : \"+greeting);
   //Implementation method of popSome method.
   greeting.popSome(1);
   System.out.println(\"toString Method Called : \"+greeting);
  
   }
}

Output:

toString Method Called......Before Swapping : Hello, , World, !,
toString Method Called......After Swapping : World, Hello, , !,
toString Method Called : World, Hello, , !,
Size of Stack Is: 3
Pop Method Is CalledWorld
toString Method Called : Hello, , !,
toString Method Called : !,

 Add the following methods to the Array Bounded Stack class, and create a test driver for each to show that they work correctly. In order to practice your array
 Add the following methods to the Array Bounded Stack class, and create a test driver for each to show that they work correctly. In order to practice your array
 Add the following methods to the Array Bounded Stack class, and create a test driver for each to show that they work correctly. In order to practice your array

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site