Question

In: Computer Science

28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...

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.

Solutions

Expert Solution

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:



Related Solutions

java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
Add the following method below to the CardDeck class, and create a test driver to show...
Add the following method below to the CardDeck class, and create a test driver to show that they work correctly. int cardsRemaining() //returns a count of the number of undealt cards remaining in the deck. Complete in Java programming language. // Models a deck of cards. Includes shuffling and dealing. //---------------------------------------------------------------------- package Homework4; import java.util.Random; import java.util.Iterator; import javax.swing.ImageIcon; public class CardDeck { public static final int NUMCARDS = 52; protected ABList<Card> deck; protected Iterator<Card> deal; public CardDeck() { deck...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following...
IN JAVA: Repeat Exercise 28, but add the methods to the LinkedStack class. Add the following methods to the LinkedStacked 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 LinkedStacked, not by calling the previously defined public methods of the class. - String toString()—creates and returns a string that correctly represents the current stack....
In this class add Comparable interface. In the driver program create a few objects and In...
In this class add Comparable interface. In the driver program create a few objects and In the driver program create a few objects and compare them . then create a list of those objects and sort them .A Quadratic is bigger than another Quadratic if it opens faster package pack2; /** * This is a program for defining a quadratic equation * @author sonik */ public class Quadratic { public int coeffX2 = 0; public int coeffX = 0; public...
Create a (partial) BST class and a driver program to test it. The tree node will...
Create a (partial) BST class and a driver program to test it. The tree node will store integers as the data/key field (single field). Note that you will need to guarantee there are no duplicates in your insert function (the tree should refuse to insert a duplicate key). Call your files “tree.h”, “tree.cpp” and “main.cpp”. In addition, draw a picture of your tree (see note about random values below) Public methods to include: Constructor Copy Constructor Overloaded Assignment Operator Destructor...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods:...
1. Please create a New Class with the Class Name: Class17Ex Please add the ten methods: 1. numberOfStudents 2. getName 3. getStudentID 4. getCredits 5. getLoginName 6. getTime 7. getValue 8. getDisplayValue 9. sum 10. max Show Class17Ex.java file with full working please. Let me know if you have any questions.
Hi, I want to implement the following methods with a driver class In the comment block...
Hi, I want to implement the following methods with a driver class In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations. here is the Implement class: import java.util.Iterator; // Do not modify the given code. @SuppressWarnings("unchecked") // Given public class MyArrayList { private T[] data; // Given...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String...
Code in Java Given the LinkedList class that is shown below Add the following methods: add(String new_word): Adds a linkedlist item at the end of the linkedlist print(): Prints all the words inside of the linkedlist length(): Returns an int with the length of items in the linkedlist remove(int index): removes item at specified index itemAt(int index): returns LinkedList item at the index in the linkedlist public class MyLinkedList { private String name; private MyLinkedList next; public MyLinkedList(String n) {...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount...
java Objective: Create a class. Create objects. Use methods of a class. Create a class BankAccount to represent a bank account according to the following requirements: A bank account has three attributes: accountnumber, balance and customer name. Add a constructor without parameters. In the initialization of the attributes, set the number and the balance to zero and the customer name to an empty string. Add a constructor with three parameters to initialize all the attributes by specific values. Add a...
Add code (see below for details) to the methods "set" and "get" in the following class,...
Add code (see below for details) to the methods "set" and "get" in the following class, ArrayTen.java, so that these two methods catch the exception java.lang.ArrayIndexOutOfBoundsException if an illegal index is used, and in turn throw java.lang.IndexOutOfBoundsException instead. Modify the "main" method to catch java.lang.IndexOutOfBoundsException and, when such an exception is caught, print the exception as well as the stack trace. public class ArrayTen { private String myData[] = new String[10]; public void set(int index, String value) { myData[index] =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT