Leonel_G

Tp3Ej3

Oct 12th, 2021
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. import java.util.*;
  2. public class tp3ej3 {
  3.     public static void main(String[] args) {
  4.         int cont =0,dimension;
  5.         Scanner entrada = new Scanner(System.in);
  6.         Random random = new Random();
  7.         dimension=Helper.validarNumero(entrada, "'ingrese dimension de cola 1");
  8.         Queue <Integer> cola1 = new Queue<Integer>(dimension);
  9.         dimension=Helper.validarNumero(entrada, "'ingrese dimension de cola 2");
  10.         Queue <Integer> cola2 = new Queue<Integer>(dimension);
  11.         Queue <Integer> cola3 = new Queue<Integer>(cola1.dimensiones()+cola2.dimensiones());
  12.        
  13.         while (cont!=3){
  14.             switch(cont){
  15.                 case 1:
  16.                     ingresarValores(cola1,entrada);
  17.                     ingresarValores(cola2, entrada);
  18.                     unirColaIntercalada(cola1, cola2,cola3);
  19.                     System.out.println(cola3.toString());
  20.                 case 2:
  21.                     ingresarValoresRandom(cola1, random);
  22.                     ingresarValoresRandom(cola2, random);
  23.                     unirColaIntercalada(cola1, cola2, cola3);
  24.                     System.out.println(cola3.toString());
  25.             }
  26.             cont=Helper.validarNumero(entrada, "'opcion' \n1-Manual \n2-Random \n3-salir");
  27.         }
  28.     }
  29.  
  30.     public static void ingresarValores(Queue<Integer> cola,Scanner entrada) {
  31.         int cont=1;
  32.         while (cont!=2){
  33.             if (cola.estaLlena()){
  34.                 cola.offer(Helper.validarNumero(entrada, "ingrese un valor"));
  35.             }
  36.             cont=Helper.elegirOpcion(entrada, "'seguir cargando' \n1-Si \n2-No");
  37.         }
  38.         System.out.println("Valores ingresados"+cola.toString());
  39.     }
  40.     public static void unirColaIntercalada(Queue<Integer> cola1,Queue<Integer> cola2, Queue<Integer> cola3){
  41.         boolean continuar=true;
  42.         while (continuar){
  43.             if(!cola1.estaVacia()){
  44.                 cola3.offer(cola1.pool());
  45.             }
  46.             if(!cola2.estaVacia()){
  47.                 cola3.offer(cola2.pool());
  48.             }
  49.             if (cola1.estaVacia() && cola2.estaVacia()){
  50.                 continuar=false;
  51.             }
  52.         }
  53.     }
  54.     public static void ingresarValoresRandom(Queue<Integer> cola,Random random){
  55.         for (int cta = random.nextInt(10) + 1; cta > 0 ; --cta) {
  56.             cola.offer(random.nextInt(500));
  57.         }
  58.         System.out.println("Valores ingresados"+cola.toString());
  59.     }
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment