Advertisement
jtentor

Ejercicio 3 Trabajo p`práctico 2 - 2017

Sep 14th, 2017 (edited)
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. import java.util.Stack;
  2. import java.util.Random;
  3.  
  4. public class TP2E03 {
  5.  
  6.     public static void main(String[] args) {
  7.         TP2E03 excercise = new TP2E03();
  8.         excercise.execute();
  9.     }
  10.    
  11.     private void execute() {
  12.         Stack<Integer> myStack1 = new Stack<Integer>();
  13.         Stack<Integer> myStack2 = new Stack<Integer>();
  14.         Random random = new Random();
  15.  
  16.         for (int max = random.nextInt(10) + 1; max > 0; max--) {
  17.             myStack1.push(random.nextInt(100));
  18.         }
  19.  
  20.         for (int max = random.nextInt(10) + 1; max > 0; max--) {
  21.             myStack2.push(random.nextInt(100));
  22.         }
  23.        
  24.         System.out.println("Pila 1 " + myStack1.toString());
  25.         System.out.println("Pila 2 " + myStack2.toString());
  26.        
  27.         System.out.println("Union  " + Union(myStack1, myStack2));
  28.        
  29.         System.out.println("Pila 1 " + myStack1.toString());
  30.         System.out.println("Pila 2 " + myStack2.toString());
  31.        
  32.     }
  33.    
  34.    
  35.     private Stack<Integer> Union(Stack<Integer> stack1, Stack<Integer> stack2) {
  36.         Stack<Integer> result = new Stack<Integer>();
  37.  
  38.         Integer[] data = new Integer[stack1.size()];
  39.         stack1.toArray(data);
  40.         for (int i = 0; i < data.length; i++) {
  41.             result.push(data[i]);
  42.         }
  43.        
  44.         data = new Integer[stack2.size()];
  45.         stack2.toArray(data);
  46.         for (int i = 0; i < data.length; i++) {
  47.             result.push(data[i]);
  48.         }
  49.        
  50.         return result;
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement