Roke98

Ejercicio2-Tp1-UNJu

Sep 13th, 2022 (edited)
767
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.05 KB | None | 0 0
  1. package Practico1;
  2.  
  3. import Practico1.Helper;
  4. import java.util.Scanner;
  5. import java.util.Random;
  6. import java.util.Arrays;
  7. import java.util.Iterator;
  8.  
  9. public class Ejercicio2 {
  10.     public static void main(String[] args) {
  11.         // TODO Auto-generated method stub
  12.         menu();
  13.     }
  14.  
  15.     @SuppressWarnings("static-access")
  16.     public static void menu() {
  17.         int opcion;
  18.         Character seguir;
  19.          do {  
  20.             int dimen =Helper.getPositiveInt("Ingrese tamaño de los arreglos");
  21.             int x = Helper.getPositiveInt("Ingrese una Valor X (Para mostrar valores mayores a este)");
  22.             int y = Helper.getPositiveInt("Ingrese un valor Y (Para mostrar valores menores a este)");
  23.             System.out.println("1 - Ingresar valores");
  24.             System.out.println("2 - Generar valores aleatorios ");
  25.             opcion = Helper.getInteger("Ingrese una opcion");
  26.             int[] arrayPrimo = new int[dimen];
  27.             int[] arrayInvertido = new int[dimen];
  28.             int[] arrayMayorX;
  29.             int[] arrayMenorY;
  30.            
  31.             switch (opcion) {
  32.             case 1:
  33.                 arrayPrimo =cargarPrimo(arrayPrimo);
  34.                 arrayMayorX = new int[mayorX(arrayPrimo, x)] ;
  35.                 arrayMayorX = cargarMayorX(arrayPrimo, arrayMayorX, x);
  36.                 arrayMenorY = new int[menorY(arrayPrimo, y)] ;
  37.                 arrayMenorY = cargarMenorY(arrayPrimo, arrayMenorY, y);
  38.                 arrayInvertido = invertirPrimo(arrayPrimo, arrayInvertido);
  39.                
  40.                 Helper.mostrarArrayUnaDimension("El arreglo de numeros Primos ingresados es: ", arrayPrimo, "");
  41.                 System.out.println("-------------------------------------------------------------------");
  42.                 Helper.mostrarArrayUnaDimension("Los elementos mayores a " + x + " son:", arrayMayorX,"");
  43.                 System.out.println("-------------------------------------------------------------------");
  44.                 Helper.mostrarArrayUnaDimension("Los numeros menores a "+ y +" son", arrayMenorY, "");
  45.                 System.out.println("-------------------------------------------------------------------");
  46.                 Helper.mostrarArrayUnaDimension("El arreglo invertido es: ", arrayInvertido, "");
  47.                
  48.                 break;
  49.             case 2:
  50.                 arrayPrimo = generarPrimo(arrayPrimo);
  51.                 arrayMayorX = new int[mayorX(arrayPrimo, x)] ;
  52.                 arrayMayorX = cargarMayorX(arrayPrimo, arrayMayorX, x);
  53.                 arrayMenorY = new int[menorY(arrayPrimo, y)] ;
  54.                 arrayMenorY = cargarMenorY(arrayPrimo, arrayMenorY, y);
  55.                 arrayInvertido = invertirPrimo(arrayPrimo, arrayInvertido);
  56.                
  57.                 Helper.mostrarArrayUnaDimension("El arreglo de numeros Primos generados es: ", arrayPrimo, "");
  58.                 System.out.println("-------------------------------------------------------------------");
  59.                 Helper.mostrarArrayUnaDimension("Los elementos mayores a " + x + " son:", arrayMayorX,"");
  60.                 System.out.println("-------------------------------------------------------------------");
  61.                 Helper.mostrarArrayUnaDimension("Los numeros menores a "+ y +" son", arrayMenorY, "");
  62.                 System.out.println("-------------------------------------------------------------------");
  63.                 Helper.mostrarArrayUnaDimension("El arreglo invertido es: ", arrayInvertido, "");
  64.                
  65.                 break;
  66.             default:
  67.                 System.out.println("Opcion No Valida ");
  68.             }
  69.            
  70.             System.out.println();
  71.             seguir = Helper.getCharacter("s: Para ingresar cargar otro arreglo // Cualquier otro caracter para finalizar...");
  72.         }while(seguir.equals('s'));
  73.         System.out.println("Gracias :D");
  74.         /*Se usa un modulo agregado a la Clase Helper para la impresion de los arrglos. El mismo cuenta con:
  75.          Una Firama de tipo "Void" que no devuelve ningun valor, solo mostrara un msj.
  76.          la Precondiciones son: ingresar un msj de inicio, el array a mostrar y un msj de fin (en este caso no lo usamos) */
  77.     }
  78.    
  79.     //Modulo para cargar el array con valores mayores a X
  80.     static public int[] cargarMayorX(int[] arrayPrimo, int[] arrayMayorX, int x) {//PostCond: recive el arrayPrimo(cargado), el arrayMenorX(vacio) y la variable "x"
  81.         int i = 0;
  82.         for (int elementos:arrayPrimo) {
  83.             if(x < elementos) {
  84.                 arrayMayorX[i] = elementos;
  85.                 i++;
  86.             }
  87.         }return arrayMayorX;//PostCond: retorna el arrayMayorX ahora con los valores cargados
  88.     }
  89.    
  90.     //Modulo para cargar el array con valores menores a Y.Firma de tipo  Int[]
  91.     static public int[] cargarMenorY(int[] arrayPrimo, int[] arrayMenorY, int y) {//PostCond: recive el arrayPrimo(cargado), el arrayMayorY(vacio) y la variable "y"
  92.         int i = 0;
  93.         for (int elementos:arrayPrimo) {
  94.             if(elementos < y) {
  95.                 arrayMenorY[i] = elementos;
  96.                 i++;
  97.             }
  98.         }return arrayMenorY;//PostCond: retorna el arrayMenorY ahora con los valores cargados
  99.     }
  100.    
  101.     //Modulo para determinar el tamaño que tendra el array de nros mayores a X
  102.     static public int mayorX(int[] arrayPrimo, int x) {//PreCond: recibe el arrayPrimo(ahora con los valores cargados) y la variable "x"
  103.         int i = 0;
  104.         for (int elemento: arrayPrimo) {
  105.             if(x < elemento) {
  106.                 i++;
  107.             }
  108.         }return i;//PostCond: devolvera una variable de tipo entero
  109.     }
  110.    
  111.     //Modulo para determinar el tamaño que tendra el array de nros menores a Y. Firma de tipo Int
  112.     static public int menorY(int[] arrayPrimo, int y) {//PreCond: recibe el arrayPrimo(ahora con los valores cargados) y la variable "y"
  113.         int i =0;
  114.         for (int elemento: arrayPrimo) {
  115.             if (y > elemento) {
  116.                 i++;
  117.             }
  118.         }return i;//PostCond: devolvera una variable de tipo entero
  119.     }
  120.    
  121.     //Modulo para invertir la posicion de los nros en el arrayPrimo
  122.     static public int[] invertirPrimo(int[] arrayPrimo, int[] arrayInvertido) {
  123.         for (int i = 0; i < arrayPrimo.length; i++) {
  124.             arrayInvertido[i] = arrayPrimo[arrayPrimo.length-1-i];
  125.         }return arrayInvertido;
  126.     }
  127.    
  128.     //Modulo para cargar por consola nros en el arrayPrimo. Firma de tipo Int[]
  129.     static public int[] cargarPrimo(int[] arrayPrimo) {//PreCond: recive el arrayPrimo, el cual esta vacio
  130.         int i = 0, numero;
  131.         while(i < arrayPrimo.length) {
  132.             numero = Helper.getInteger("ingrrese valor positivo");
  133.             if(primo(numero)==true) {//Se llama el modulo para validar que el nro cargado sea Primo
  134.                 arrayPrimo[i] = numero;
  135.                 i ++;
  136.             }else {
  137.                 System.out.println("El valor ingresado NO es Primo, reintentar...");
  138.             }
  139.         }return arrayPrimo;//PostCond: devuelve o retorna el mismo array con los valores cargados
  140.     }
  141.    
  142.     //Modulo para generar y cargar los nros en el arrayPrimo. Firma de tipo Int[]
  143.     static public int[] generarPrimo (int[] arrayPrimo) {//PreCond: recive el arrayPrimo, el cual esta vacio
  144.         Random alea = new Random();
  145.         int i = 0, numero;
  146.         while (i < arrayPrimo.length) {
  147.             numero = alea.nextInt(100);
  148.             if (primo(numero)) {//Se llama el modulo para validar que el nro generado sea Primo
  149.                 arrayPrimo[i]=numero;
  150.                 i++;
  151.             }
  152.         }return arrayPrimo;//PostCond: devuelve o retorna el mismo array con los valores cargados
  153.     }
  154.    
  155.     //Este modulo valida si el valor ingresado sea un numero PRIMO. Firma del tipo Boolean
  156.     static public Boolean primo(int numero) {//PreCond: recibel la variable numero
  157.         if(numero == 0 || numero == 1 || numero == 4){
  158.             return false;
  159.         }
  160.         for (int x = 2; x<numero/2; x++) {
  161.             if(numero % x == 0) {
  162.                 return false;//PostCond: retornara un false si la condicion se cumple
  163.             }
  164.         }return true;//PostCond: retornara un true si la condicon no se cumple
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment