Advertisement
LEANDRONIEVA

Ejercicio3Tp3

Oct 11th, 2022 (edited)
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. import tp0.Helper;
  5.  
  6. public class Tp3ejercicio3 {
  7.     static Random aleatorio = new Random();
  8.     static Scanner sc = new Scanner(System.in);
  9.  
  10.     public static void main(String[] args) {
  11.         // TODO Auto-generated method stub
  12.         Queue<String> cola0 = new Queue<>();
  13.         Queue<String> cola1 = new Queue<>();
  14.         int opcion;
  15.  
  16.         System.out.println("Ejercicio 3 Tp3");
  17.         System.out.println("Vamos a unir dos colas sin que se repita el mismo valor en la cola resultante");
  18.        
  19.         do {
  20.             opcion = menu();
  21.             switch (opcion) {
  22.             case 1:
  23.                 System.out.println("Primer cola");
  24.                 cola0 = cargarCola();
  25.                 System.out.println("Cola 1 ingresada:");
  26.                 System.out.println(cola0);
  27.                 System.out.println("Segunda cola");
  28.                 cola1 = cargarCola();
  29.                 System.out.println("Cola 2 ingresada:");
  30.                 System.out.println(cola1);
  31.                 System.out.println("La unión sin elementos repetidos es: ");
  32.                 System.out.println(unirColas(cola0,cola1));
  33.                 break;
  34.             case 2:
  35.                 cola0 = generarCola();
  36.                 System.out.println("Primera cola generada:");
  37.                 System.out.println(cola0);
  38.                 cola1 = generarCola();
  39.                 System.out.println("Segunda cola generada:");
  40.                 System.out.println(cola1);
  41.                 System.out.println("La unión sin elementos repetidos es: ");
  42.                 System.out.println(unirColas(cola0,cola1));
  43.                 break;
  44.             case 3:
  45.                 System.out.println("Programa Terminado");
  46.                 break;
  47.             default:
  48.                 System.out.println("No es una opción correcta ");
  49.                 break;
  50.             }
  51.         }while(opcion!=3);
  52.     }
  53.  
  54.     public static int menu() {
  55.         int op;
  56.         System.out.println();
  57.         System.out.println("1.Ingresar manualmente dos colas");
  58.         System.out.println("2.Generar colas aleatoriamente");
  59.         System.out.println("3.Salir");
  60.         op = Helper.getPositiveInt("Ingrese una opcion");
  61.  
  62.         return op;
  63.     }
  64.    
  65.     public static Queue<String> cargarCola() {
  66.         Queue<String> cola = new Queue<>();
  67.         char rpta = ' ';
  68.         do {
  69.             System.out.println("Agregue un elemento a la cola");
  70.             cola.add(sc.nextLine());
  71.             rpta = Helper.getCharacter("Seguir agregando? s(cualquier tecla)/n");
  72.         }while(rpta!='n');
  73.        
  74.         return cola;
  75.     }
  76.    
  77.     public static Queue<String> unirColas(Queue<String> cola1, Queue<String> cola2) {
  78.         int tamaño = cola1.size()+cola2.size();
  79.         Queue<String> cola = new Queue<>(tamaño);
  80.         String elemento = "";
  81.        
  82.         for(int i=0; i<tamaño;i++) {
  83.             if (!cola1.isEmpty()) {
  84.                 elemento += cola1.peek();
  85.                 if(!cola.contains(elemento))cola.add(elemento);
  86.                 if(cola1.peek()!=null) cola1.remove();
  87.                 elemento = "";
  88.             }
  89.             if (!cola2.isEmpty()) {
  90.                 elemento += (String)cola2.peek();
  91.                 if(!cola.contains(elemento))cola.add(elemento);
  92.                 if(cola2.peek()!=null)cola2.remove();
  93.                 elemento = "";
  94.             }
  95.         }
  96.        
  97.         return cola;
  98.     }
  99.    
  100.     public static Queue<String> generarCola() {
  101.         Queue<String> cola = new Queue<>();
  102.         int op;
  103.        
  104.         do {
  105.             op = menu2();
  106.             switch (op) {
  107.             case 1:    
  108.                 cola = colaChar();
  109.                 break;
  110.             case 2:                    
  111.                 cola = colaInt();  
  112.                 break;
  113.             default:
  114.                 System.out.println("No es una opción correcta ");
  115.                 break;
  116.             }
  117.         }while(op!=1&op!=2);
  118.        
  119.         return cola;   
  120.     }
  121.    
  122.     public static int menu2() {
  123.         int op;
  124.         System.out.println();
  125.         System.out.println("1.Crear cola de caracteres");
  126.         System.out.println("2.Crear cola de enteros");
  127.        
  128.         op = Helper.getPositiveInt("Ingrese una opcion");
  129.  
  130.         return op;
  131.     }
  132.    
  133.     public static Queue<String> colaInt(){
  134.         Queue<String> cola = new Queue<>();
  135.         String elemento = "";
  136.        
  137.         while(!cola.isFull()) {
  138.             int aux = aleatorio.nextInt(1,100);
  139.             elemento += aux;
  140.             cola.add(elemento);
  141.             elemento = "";
  142.         }
  143.        
  144.         return cola;
  145.     }
  146.    
  147.     public static Queue<String> colaChar(){
  148.         Queue<String> cola = new Queue<>();
  149.         String elemento = "";
  150.        
  151.         while(!cola.isFull()){
  152.             int aux = aleatorio.nextInt(25)+97;
  153.             elemento += (char)aux;
  154.             cola.add(elemento);
  155.             elemento = "";
  156.         }
  157.        
  158.         return cola;
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement