Advertisement
LEANDRONIEVA

Ejercicio2Tp3

Oct 11th, 2022
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. import tp0.Helper;
  5.  
  6. public class Tp3ejercicio2 {
  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<Integer> cola = new Queue<>();
  13.         int opcion,num;
  14.  
  15.         System.out.println("Ejercicio 2 Tp 3");
  16.         System.out.println("Dadas una cola de enteros y un número se eliminará ese número, sus múltiplos y divisores de la cola");
  17.        
  18.         do {
  19.             opcion = menu();
  20.             switch (opcion) {
  21.             case 1:        
  22.                 cola = cargarCola();
  23.                 System.out.println("La cola ingresada es:");
  24.                 System.out.println(cola);
  25.                 num = Helper.getInteger("Ingrese el valor que vamos a eliminar", "Debe ser un elemento de la cola");
  26.                 cola = procedimiento(cola,num);
  27.                 System.out.println("Cola actual:");
  28.                 System.out.println(cola);
  29.                 break;
  30.             case 2:
  31.                 cola = generarCola();
  32.                 System.out.println("La cola generada es:");
  33.                 System.out.println(cola);
  34.                 num = Helper.getInteger("Ingrese el valor que vamos a eliminar", "Debe ser un elemento de la cola");
  35.                 cola = procedimiento(cola,num);
  36.                 System.out.println("Cola actual:");
  37.                 System.out.println(cola);
  38.                 break;
  39.             case 3:
  40.                 System.out.println("Programa Terminado");
  41.                 break;
  42.             default:
  43.                 System.out.println("No es una opción correcta ");
  44.                 break;
  45.             }
  46.         }while(opcion!=3);
  47.     }
  48.  
  49.     public static int menu() {
  50.         int op;
  51.         System.out.println();
  52.         System.out.println("1.Ingresar manualmente");
  53.         System.out.println("2.Generar aleatoriamente");
  54.         System.out.println("3.Salir");
  55.         op = Helper.getPositiveInt("Ingrese una opcion");
  56.  
  57.         return op;
  58.     }
  59.    
  60.     public static Queue<Integer> cargarCola() {
  61.         int n = Helper.getPositiveInt("Ingrese el tamaño de la cola");
  62.         Queue<Integer> cola = new Queue<>(n);
  63.        
  64.         while(!cola.isFull()) {
  65.             System.out.println("Ingrese un número entero a la cola");
  66.             cola.add(Helper.getInteger("Agregue un elemento a la cola"));
  67.         }
  68.        
  69.         return cola;
  70.     }
  71.    
  72.     public static Queue<Integer> generarCola(){
  73.         Queue<Integer> cola = new Queue<Integer>();
  74.        
  75.         while(!cola.isFull()) {
  76.             int aux = aleatorio.nextInt(1,500);
  77.             cola.add(aux);
  78.         }
  79.         return cola;
  80.     }
  81.  
  82.     public static Queue<Integer> procedimiento(Queue<Integer> cola1, int num){
  83.         Queue<Integer> cola2 = new Queue<Integer>();
  84.        
  85.         while(!cola1.isEmpty()) {
  86.             if ((cola1.peek()%num==0)|(num%cola1.peek()==0)) {
  87.                 cola1.remove();
  88.             }else {
  89.                 cola2.add(cola1.remove());
  90.             }
  91.         }
  92.         return cola2;
  93.     }
  94. }
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement