Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.util.Scanner;
- import tp0.Helper;
- public class Tp3ejercicio2 {
- static Random aleatorio = new Random();
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Queue<Integer> cola = new Queue<>();
- int opcion,num;
- System.out.println("Ejercicio 2 Tp 3");
- 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");
- do {
- opcion = menu();
- switch (opcion) {
- case 1:
- cola = cargarCola();
- System.out.println("La cola ingresada es:");
- System.out.println(cola);
- num = Helper.getInteger("Ingrese el valor que vamos a eliminar", "Debe ser un elemento de la cola");
- cola = procedimiento(cola,num);
- System.out.println("Cola actual:");
- System.out.println(cola);
- break;
- case 2:
- cola = generarCola();
- System.out.println("La cola generada es:");
- System.out.println(cola);
- num = Helper.getInteger("Ingrese el valor que vamos a eliminar", "Debe ser un elemento de la cola");
- cola = procedimiento(cola,num);
- System.out.println("Cola actual:");
- System.out.println(cola);
- break;
- case 3:
- System.out.println("Programa Terminado");
- break;
- default:
- System.out.println("No es una opción correcta ");
- break;
- }
- }while(opcion!=3);
- }
- public static int menu() {
- int op;
- System.out.println();
- System.out.println("1.Ingresar manualmente");
- System.out.println("2.Generar aleatoriamente");
- System.out.println("3.Salir");
- op = Helper.getPositiveInt("Ingrese una opcion");
- return op;
- }
- public static Queue<Integer> cargarCola() {
- int n = Helper.getPositiveInt("Ingrese el tamaño de la cola");
- Queue<Integer> cola = new Queue<>(n);
- while(!cola.isFull()) {
- System.out.println("Ingrese un número entero a la cola");
- cola.add(Helper.getInteger("Agregue un elemento a la cola"));
- }
- return cola;
- }
- public static Queue<Integer> generarCola(){
- Queue<Integer> cola = new Queue<Integer>();
- while(!cola.isFull()) {
- int aux = aleatorio.nextInt(1,500);
- cola.add(aux);
- }
- return cola;
- }
- public static Queue<Integer> procedimiento(Queue<Integer> cola1, int num){
- Queue<Integer> cola2 = new Queue<Integer>();
- while(!cola1.isEmpty()) {
- if ((cola1.peek()%num==0)|(num%cola1.peek()==0)) {
- cola1.remove();
- }else {
- cola2.add(cola1.remove());
- }
- }
- return cola2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement