Advertisement
TwiNNeR

samo pochnata zad1 l6

Dec 8th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.28 KB | None | 0 0
  1. package labs678;
  2.  
  3. import java.util.EmptyStackException;
  4.  
  5. public class StackTest {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner jin = new Scanner(System.in);
  9.         int k = jin.nextInt();
  10.         System.out.println("Test#" + k);
  11.         if (k == 0) {
  12.             System.out.println("testing: Stack::push(T) , Stack::pop():T , T is Integer");
  13.             int n = jin.nextInt();
  14.             Stack<Integer> stack = new Stack<Integer>();
  15.             System.out.println("Pushing elements:");
  16.             for (int i = 1; i <= n; ++i) {
  17.                 if (i > 1)
  18.                     System.out.print(" ");
  19.                 System.out.print(i);
  20.                 stack.push(i);
  21.             }
  22.             System.out.println();
  23.             System.out.println("Poping elements:");
  24.             for (int i = n; i >= 1; --i) {
  25.                 if (i < n)
  26.                     System.out.print(" ");
  27.                 System.out.print(stack.pop());
  28.             }
  29.             System.out.println();
  30.         }
  31.         if (k == 1) {
  32.             System.out.println("testing: Stack::push(T) , Stack::pop():T , T is String");
  33.  
  34.             int n = jin.nextInt();
  35.             Stack<String> stack = new Stack<String>();
  36.             System.out.println("Pushing elements:");
  37.             for (int i = 0; i < n; ++i) {
  38.                 if (i > 0)
  39.                     System.out.print(" ");
  40.                 String next = jin.next();
  41.                 System.out.print(next);
  42.                 stack.push(next);
  43.             }
  44.             System.out.println();
  45.             System.out.println("Poping elements:");
  46.             for (int i = 0; i < n; ++i) {
  47.                 if (i > 0)
  48.                     System.out.print(" ");
  49.                 System.out.print(stack.pop());
  50.             }
  51.         }
  52.         if (k == 2) {
  53.             System.out.println("testing: Stack::push(T) , Stack::pop():T , Stack::isEmpty():boolean, T is Double");
  54.  
  55.             Stack<Double> stack = new Stack<Double>();
  56.             System.out.println("Pushing elements:");
  57.             boolean flag = false;
  58.             while (jin.hasNextDouble()) {
  59.                 double d = jin.nextDouble();
  60.                 stack.push(d);
  61.                 if (flag)
  62.                     System.out.print(" ");
  63.                 System.out.printf("%.2f", d);
  64.                 flag = true;
  65.             }
  66.             int i = 0;
  67.             System.out.println();
  68.             System.out.println("Poping elements:");
  69.             while (!stack.isEmpty()) {
  70.                 if (i > 0)
  71.                     System.out.print(" ");
  72.                 ++i;
  73.                 System.out.printf("%.2f", stack.pop());
  74.             }
  75.         }
  76.         if (k == 3) {
  77.             System.out.println(
  78.                     "testing: Stack::push(T) , Stack::pop():T , Stack::isEmpty():boolean , Stack::peek():T , T is Long");
  79.  
  80.             int n = jin.nextInt();
  81.             Stack<Long> stack = new Stack<Long>();
  82.             LinkedList<Long> control_stack = new LinkedList<Long>();
  83.             boolean exact = true;
  84.             for (int i = 0; exact && i < n; ++i) {
  85.                 if (Math.random() < 0.5) {// add
  86.                     long to_add = (long) (Math.random() * 456156168);
  87.                     stack.push(to_add);
  88.                     control_stack.addFirst(to_add);
  89.                 } else {
  90.                     exact &= control_stack.isEmpty() == stack.isEmpty();
  91.                     if (exact && !stack.isEmpty()) {
  92.                         if (Math.random() > 0.7)
  93.                             exact &= control_stack.removeFirst().equals(stack.pop());
  94.                         else
  95.                             exact &= control_stack.peekFirst().equals(stack.peek());
  96.                     }
  97.                 }
  98.             }
  99.             System.out.println("Your stack outputs compared to the built in java stack were the same? " + exact);
  100.         }
  101.         if (k == 4) {
  102.             System.out.println("testing: Stack::pop():T , Stack::isEmpty():boolean , Stack::peek():T , T is Long");
  103.  
  104.             Stack<Date> test_stack = new Stack<Date>();
  105.  
  106.             System.out.println("Stack empty? " + test_stack.isEmpty());
  107.             try {
  108.                 test_stack.pop();
  109.                 System.out.println("NO exeption was thrown when trying to pop from an empty stack!");
  110.  
  111.             } catch (Exception e) {
  112.                 System.out.print("Exeption thrown when trying to pop from an empty stack ");
  113.                 System.out.println(e.getClass().getSimpleName());
  114.             }
  115.             try {
  116.                 test_stack.peek();
  117.                 System.out.println("NO exeption was thrown when trying to peek in an empty stack!");
  118.             } catch (Exception e) {
  119.                 System.out.print("Exeption thrown when trying to peek in an empty stack ");
  120.                 System.out.println(e.getClass().getSimpleName());
  121.             }
  122.  
  123.         }
  124.     }
  125.  
  126. }
  127.  
  128. class Stack<T> {
  129.     private Node<T> vrv;
  130.  
  131.     public void push(T element) {
  132.         Node<T> dodadi = new Node<T>(null, element);
  133.         dodadi.sledbenik = vrv;
  134.         vrv = dodadi;
  135.     }
  136.  
  137.     public T peek() {
  138.         if (isEmpty())
  139.             throw new EmptyStackException();
  140.         return vrv.vrednost;
  141.     }
  142.    
  143.     public T pop() {
  144.        
  145.     }
  146.  
  147.     public boolean isEmpty() {
  148.         return (vrv == null) ? true : false;
  149.     }
  150.  
  151.     private class Node<T> {
  152.         protected T vrednost;
  153.         protected Node<T> sledbenik;
  154.  
  155.         Node(Node<T> n, T v) {
  156.             vrednost = v;
  157.             sledbenik = n;
  158.         }
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement