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 Tp3ejercicio3 {
- static Random aleatorio = new Random();
- static Scanner sc = new Scanner(System.in);
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Queue<String> cola0 = new Queue<>();
- Queue<String> cola1 = new Queue<>();
- int opcion;
- System.out.println("Ejercicio 3 Tp3");
- System.out.println("Vamos a unir dos colas sin que se repita el mismo valor en la cola resultante");
- do {
- opcion = menu();
- switch (opcion) {
- case 1:
- System.out.println("Primer cola");
- cola0 = cargarCola();
- System.out.println("Cola 1 ingresada:");
- System.out.println(cola0);
- System.out.println("Segunda cola");
- cola1 = cargarCola();
- System.out.println("Cola 2 ingresada:");
- System.out.println(cola1);
- System.out.println("La unión sin elementos repetidos es: ");
- System.out.println(unirColas(cola0,cola1));
- break;
- case 2:
- cola0 = generarCola();
- System.out.println("Primera cola generada:");
- System.out.println(cola0);
- cola1 = generarCola();
- System.out.println("Segunda cola generada:");
- System.out.println(cola1);
- System.out.println("La unión sin elementos repetidos es: ");
- System.out.println(unirColas(cola0,cola1));
- 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 dos colas");
- System.out.println("2.Generar colas aleatoriamente");
- System.out.println("3.Salir");
- op = Helper.getPositiveInt("Ingrese una opcion");
- return op;
- }
- public static Queue<String> cargarCola() {
- Queue<String> cola = new Queue<>();
- char rpta = ' ';
- do {
- System.out.println("Agregue un elemento a la cola");
- cola.add(sc.nextLine());
- rpta = Helper.getCharacter("Seguir agregando? s(cualquier tecla)/n");
- }while(rpta!='n');
- return cola;
- }
- public static Queue<String> unirColas(Queue<String> cola1, Queue<String> cola2) {
- int tamaño = cola1.size()+cola2.size();
- Queue<String> cola = new Queue<>(tamaño);
- String elemento = "";
- for(int i=0; i<tamaño;i++) {
- if (!cola1.isEmpty()) {
- elemento += cola1.peek();
- if(!cola.contains(elemento))cola.add(elemento);
- if(cola1.peek()!=null) cola1.remove();
- elemento = "";
- }
- if (!cola2.isEmpty()) {
- elemento += (String)cola2.peek();
- if(!cola.contains(elemento))cola.add(elemento);
- if(cola2.peek()!=null)cola2.remove();
- elemento = "";
- }
- }
- return cola;
- }
- public static Queue<String> generarCola() {
- Queue<String> cola = new Queue<>();
- int op;
- do {
- op = menu2();
- switch (op) {
- case 1:
- cola = colaChar();
- break;
- case 2:
- cola = colaInt();
- break;
- default:
- System.out.println("No es una opción correcta ");
- break;
- }
- }while(op!=1&op!=2);
- return cola;
- }
- public static int menu2() {
- int op;
- System.out.println();
- System.out.println("1.Crear cola de caracteres");
- System.out.println("2.Crear cola de enteros");
- op = Helper.getPositiveInt("Ingrese una opcion");
- return op;
- }
- public static Queue<String> colaInt(){
- Queue<String> cola = new Queue<>();
- String elemento = "";
- while(!cola.isFull()) {
- int aux = aleatorio.nextInt(1,100);
- elemento += aux;
- cola.add(elemento);
- elemento = "";
- }
- return cola;
- }
- public static Queue<String> colaChar(){
- Queue<String> cola = new Queue<>();
- String elemento = "";
- while(!cola.isFull()){
- int aux = aleatorio.nextInt(25)+97;
- elemento += (char)aux;
- cola.add(elemento);
- elemento = "";
- }
- return cola;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement