Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Practico3;
- import java.util.Random;
- public class Ejercicio3 {
- static Random aleatorio = new Random();
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- principal();
- }
- public static void principal() {
- int opcion;
- Queue<Integer> cola1 = null;
- Queue<Character> cola2 = null;
- Queue<Object> union = null;
- do {
- cola1 = menuColaInt();
- //System.out.println(cola1);
- cola2 = menuColaChar();
- //System.out.println(cola2);
- union = new Queue<>(cola1.size() + cola2.size());
- union = union(cola1, cola2);
- System.out.println("\nLA UNION DE LA DOS COLAS ES:\n" + union.toString());
- opcion = Helper.getPositiveInt("Desea cargar otras colas??(Si=1//No=2) ");
- }while (opcion!=2);
- System.out.println("Gracias :D");
- }
- public static Queue<Object> union(Queue<Integer> cola1, Queue<Character> cola2){
- Queue<Object> result = new Queue<>(cola1.size() + cola2.size());
- boolean aux = true;
- while (!cola1.isEmpty()||!cola2.isEmpty()) {
- if(cola1.isEmpty()) {
- aux = false;
- }
- if(cola2.isEmpty()) {
- aux = true;
- }
- if(!cola1.isEmpty() && aux){
- if(!result.contains(cola1.peek())) {
- result.offer(cola1.pool());
- aux = false;
- }else {
- cola1.remove();
- }
- continue;
- }
- if(!cola2.isEmpty() && !aux){
- if (!result.contains(cola2.peek())){
- result.offer(cola2.pool());
- aux = true;
- }else {
- cola2.remove();
- }
- }
- }
- return result;
- }
- public static Queue<Integer> menuColaInt() {
- Queue<Integer> colaAux =null;
- int opcion;
- do {
- System.out.println("CARGAR COLA DE NUMEROS ENTEROS");
- System.out.println("1) Cargar manualmente");
- System.out.println("2) Generar valores aleatorios");
- opcion = Helper.getPositiveInt("Ingrese una opcion");
- switch(opcion) {
- case 1:
- colaAux = new Queue<>(Helper.getPositiveInt("Ingrese el tamaΓ±o de la cola"));
- colaAux = cargarColaInt(colaAux);
- System.out.println("Cola de enteros cargada: " + colaAux.toString());
- break;
- case 2:
- colaAux = new Queue<>();
- colaAux = generarColaInt(colaAux);
- System.out.println("Cola de enteros generada: " + colaAux.toString());
- break;
- default:
- System.out.println("opcion no valida");
- }
- }while(opcion!=1 && opcion!=2);
- return colaAux;
- }
- public static Queue<Character> menuColaChar() {
- Queue<Character> colaAux =null;
- int opcion;
- do {
- System.out.println("CARGAR COLA DE LETRAS O CARACTERES");
- System.out.println("1) Cargar manualmente");
- System.out.println("2) Generar letras aleatorios");
- opcion = Helper.getPositiveInt("Ingrese una opcion");
- switch(opcion) {
- case 1:
- colaAux = new Queue<>(Helper.getPositiveInt("Ingrese el tamaΓ±o de la cola"));
- colaAux = cargarColaChar(colaAux);
- System.out.println("Cola de caracteres cargada: " + colaAux.toString());
- break;
- case 2:
- colaAux = new Queue<>();
- colaAux = generarColaChar(colaAux);
- System.out.println("Cola de caracteres generada: " + colaAux.toString());
- break;
- default:
- System.out.println("opcion no valida");
- }
- }while(opcion!=1 && opcion!=2);
- return colaAux;
- }
- public static Queue<Integer> cargarColaInt(Queue<Integer> queue){
- do {
- queue.offer(Helper.getPositiveInt("Ingrese un numero"));
- }while(!queue.isFull());
- return queue;
- }
- public static Queue<Character> cargarColaChar(Queue<Character>queue){
- do {
- queue.offer(Helper.getCharacter("Ingrese un caracter"));
- }while(!queue.isFull());
- return queue;
- }
- public static Queue<Integer> generarColaInt(Queue<Integer> queue){
- do {
- queue.offer(Helper.randomInt(100));
- }while(!queue.isFull());
- return queue;
- }
- public static Queue<Character> generarColaChar(Queue<Character> queue){
- do {
- char aux = (char) (aleatorio.nextInt(26)+'a');
- queue.offer(aux);
- }while(!queue.isFull());
- return queue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment