Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.47 KB | None | 0 0
  1. import data_structures.*;
  2.  
  3. public class P2Driver {
  4.     private Queue<Integer> testQueue;
  5.     private Stack<Integer> testStack;
  6.    
  7.     private Queue<String> stringQueue;
  8.     private Stack<String> stringStack;
  9.    
  10.     private int intCount = 100;
  11.    
  12.     private void runTests() {
  13.         testQueue = new Queue<Integer>();
  14.         testStack = new Stack<Integer>();
  15.        
  16.         testStack.makeEmpty();
  17.         testQueue.makeEmpty();
  18.        
  19.         testStack.push(new Integer(1));
  20.         testStack.push(new Integer(3));
  21.         testStack.push(new Integer(5));
  22.         testStack.push(new Integer(7));
  23.        
  24.         for(Integer i : testStack)
  25.             System.out.println(i);
  26.        
  27.         System.out.println("\nQuick Queue test (Prints 0):");
  28.         try {
  29.             testQueue.enqueue(new Integer(0));
  30.             System.out.println(testQueue.dequeue());
  31.             testQueue.makeEmpty();
  32.         } catch (Exception e) {
  33.             System.out.println("Error adding to Queue");
  34.         }
  35.        
  36.        
  37.         System.out.println("\nQuick Stack test (Prints 0): ");
  38.         try{
  39.             testStack.push(new Integer(0));
  40.             System.out.println(testStack.pop());
  41.             testStack.makeEmpty();
  42.         } catch (Exception e) {
  43.             System.out.println("Error adding to stack");
  44.         }
  45.        
  46.         try {
  47.             System.out.println("\nTesting Queue: Adding Integers");
  48.            
  49.             System.out.println("\nLinked List empty (should be true): " + testQueue.isEmpty());
  50.             for(int i = 0 ; i < intCount ; i++ ) {
  51.                 testQueue.enqueue(new Integer(i));
  52.             }
  53.             System.out.println("Linked List empty (should be false): " + testQueue.isEmpty());
  54.             System.out.println("Linked List size: " + testQueue.size());
  55.             System.out.println("Peek first: " + testQueue.peek());
  56.            
  57.             System.out.println("\nTesting Queue: Removing Integers");
  58.             for(int i = 0 ; i < intCount ; i++ )       {
  59.                 System.out.println(testQueue.dequeue());
  60.             }
  61.         } catch (Exception e) {
  62.             System.out.println("Error with massive queue");
  63.         }
  64.        
  65.         try {
  66.             System.out.println("\nTesting Stack: Adding Integers");
  67.             System.out.println("\nLinked List empty (should be true): " + testStack.isEmpty());
  68.             for( int i = 0 ; i < intCount ; i++ )
  69.                 testStack.push(new Integer(i));
  70.             System.out.println("Linked List empty (should be false): " + testStack.isEmpty());
  71.             System.out.println("Linked List size: " + testStack.size());
  72.             System.out.println("Peek first: " + testStack.peek());
  73.        
  74.             System.out.println("\nTesting Queue: Removing Integers");
  75.             while(!testStack.isEmpty())
  76.                 System.out.println(testStack.pop());
  77.         } catch (Exception e) {
  78.             System.out.println("Error with massive Stack");
  79.         }
  80.        
  81.         testQueue.makeEmpty();
  82.         testStack.makeEmpty();
  83.        
  84.         stringQueue = new Queue<String>();
  85.         stringStack = new Stack<String>();
  86.        
  87.         String[] arrString = {
  88.             new String("test1"),
  89.             new String("test2"),
  90.             new String("test3"),
  91.             new String("test4"),
  92.             new String("test5")
  93.         };
  94.        
  95.        
  96.         System.out.println("\n\nTest Queue with strings:");
  97.         try {
  98.             for(String i : arrString)
  99.                 stringQueue.enqueue(i);
  100.            
  101.             System.out.println("\nStrings in Queue before remove:");
  102.             for(String i : stringQueue)
  103.                 System.out.println(i);
  104.            
  105.             try {
  106.                 System.out.println("\nString 'test1' found: " + stringQueue.contains(new String("test1")));
  107.             } catch (Exception e) {
  108.                 System.out.println("Error locating string in Stack");
  109.             }
  110.            
  111.             try {
  112.                 System.out.println("String 'test1' Removed: " + stringQueue.remove(new String("test1")));
  113.             } catch (Exception e) {
  114.                 System.out.println("Error Removing string in Stack");
  115.                 System.out.println(e);
  116.             }
  117.            
  118.             for(int i = 0 ; i < 4 ; i++)
  119.                 System.out.println(stringQueue.dequeue());
  120.         } catch (Exception e) {
  121.                 System.out.println("Error with manipulating strings ");
  122.         }
  123.        
  124.         stringStack.makeEmpty();
  125.        
  126.         System.out.println("\n\nTest Stack with strings:");
  127.             for(String i : arrString)
  128.                 stringStack.push(i);
  129.            
  130.             System.out.println("\nStrings in Stack before remove:");
  131.             for(String i : stringStack)
  132.                 System.out.println(i);
  133.            
  134.             try {
  135.                 System.out.println("\nString 'test1' found: " + stringStack.contains(new String("test1")));
  136.             } catch (Exception e) {
  137.                 System.out.println("Error locating string in Stack");
  138.             }
  139.            
  140.             try {
  141.                 System.out.println("String 'test1' Removed: " + stringStack.remove(new String("test1")));
  142.             } catch (Exception e) {
  143.                 System.out.println("Error Removing string in Stack");
  144.             }
  145.            
  146.             for(int i = 0 ; i < 4 ; i++)
  147.                 System.out.println(stringStack.pop());
  148.        
  149.        
  150.     }
  151.    
  152.     public P2Driver() {
  153.         runTests();
  154.     }
  155.    
  156.     public static void main(String [] args) {
  157.         new P2Driver();
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement