Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Practico1;
- import Practico1.Helper;
- import java.util.Scanner;
- import java.util.Random;
- import java.util.Arrays;
- import java.util.Iterator;
- public class Ejercicio2 {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- menu();
- }
- @SuppressWarnings("static-access")
- public static void menu() {
- int opcion;
- Character seguir;
- do {
- int dimen =Helper.getPositiveInt("Ingrese tamaño de los arreglos");
- int x = Helper.getPositiveInt("Ingrese una Valor X (Para mostrar valores mayores a este)");
- int y = Helper.getPositiveInt("Ingrese un valor Y (Para mostrar valores menores a este)");
- System.out.println("1 - Ingresar valores");
- System.out.println("2 - Generar valores aleatorios ");
- opcion = Helper.getInteger("Ingrese una opcion");
- int[] arrayPrimo = new int[dimen];
- int[] arrayInvertido = new int[dimen];
- int[] arrayMayorX;
- int[] arrayMenorY;
- switch (opcion) {
- case 1:
- arrayPrimo =cargarPrimo(arrayPrimo);
- arrayMayorX = new int[mayorX(arrayPrimo, x)] ;
- arrayMayorX = cargarMayorX(arrayPrimo, arrayMayorX, x);
- arrayMenorY = new int[menorY(arrayPrimo, y)] ;
- arrayMenorY = cargarMenorY(arrayPrimo, arrayMenorY, y);
- arrayInvertido = invertirPrimo(arrayPrimo, arrayInvertido);
- Helper.mostrarArrayUnaDimension("El arreglo de numeros Primos ingresados es: ", arrayPrimo, "");
- System.out.println("-------------------------------------------------------------------");
- Helper.mostrarArrayUnaDimension("Los elementos mayores a " + x + " son:", arrayMayorX,"");
- System.out.println("-------------------------------------------------------------------");
- Helper.mostrarArrayUnaDimension("Los numeros menores a "+ y +" son", arrayMenorY, "");
- System.out.println("-------------------------------------------------------------------");
- Helper.mostrarArrayUnaDimension("El arreglo invertido es: ", arrayInvertido, "");
- break;
- case 2:
- arrayPrimo = generarPrimo(arrayPrimo);
- arrayMayorX = new int[mayorX(arrayPrimo, x)] ;
- arrayMayorX = cargarMayorX(arrayPrimo, arrayMayorX, x);
- arrayMenorY = new int[menorY(arrayPrimo, y)] ;
- arrayMenorY = cargarMenorY(arrayPrimo, arrayMenorY, y);
- arrayInvertido = invertirPrimo(arrayPrimo, arrayInvertido);
- Helper.mostrarArrayUnaDimension("El arreglo de numeros Primos generados es: ", arrayPrimo, "");
- System.out.println("-------------------------------------------------------------------");
- Helper.mostrarArrayUnaDimension("Los elementos mayores a " + x + " son:", arrayMayorX,"");
- System.out.println("-------------------------------------------------------------------");
- Helper.mostrarArrayUnaDimension("Los numeros menores a "+ y +" son", arrayMenorY, "");
- System.out.println("-------------------------------------------------------------------");
- Helper.mostrarArrayUnaDimension("El arreglo invertido es: ", arrayInvertido, "");
- break;
- default:
- System.out.println("Opcion No Valida ");
- }
- System.out.println();
- seguir = Helper.getCharacter("s: Para ingresar cargar otro arreglo // Cualquier otro caracter para finalizar...");
- }while(seguir.equals('s'));
- System.out.println("Gracias :D");
- /*Se usa un modulo agregado a la Clase Helper para la impresion de los arrglos. El mismo cuenta con:
- Una Firama de tipo "Void" que no devuelve ningun valor, solo mostrara un msj.
- la Precondiciones son: ingresar un msj de inicio, el array a mostrar y un msj de fin (en este caso no lo usamos) */
- }
- //Modulo para cargar el array con valores mayores a X
- static public int[] cargarMayorX(int[] arrayPrimo, int[] arrayMayorX, int x) {//PostCond: recive el arrayPrimo(cargado), el arrayMenorX(vacio) y la variable "x"
- int i = 0;
- for (int elementos:arrayPrimo) {
- if(x < elementos) {
- arrayMayorX[i] = elementos;
- i++;
- }
- }return arrayMayorX;//PostCond: retorna el arrayMayorX ahora con los valores cargados
- }
- //Modulo para cargar el array con valores menores a Y.Firma de tipo Int[]
- static public int[] cargarMenorY(int[] arrayPrimo, int[] arrayMenorY, int y) {//PostCond: recive el arrayPrimo(cargado), el arrayMayorY(vacio) y la variable "y"
- int i = 0;
- for (int elementos:arrayPrimo) {
- if(elementos < y) {
- arrayMenorY[i] = elementos;
- i++;
- }
- }return arrayMenorY;//PostCond: retorna el arrayMenorY ahora con los valores cargados
- }
- //Modulo para determinar el tamaño que tendra el array de nros mayores a X
- static public int mayorX(int[] arrayPrimo, int x) {//PreCond: recibe el arrayPrimo(ahora con los valores cargados) y la variable "x"
- int i = 0;
- for (int elemento: arrayPrimo) {
- if(x < elemento) {
- i++;
- }
- }return i;//PostCond: devolvera una variable de tipo entero
- }
- //Modulo para determinar el tamaño que tendra el array de nros menores a Y. Firma de tipo Int
- static public int menorY(int[] arrayPrimo, int y) {//PreCond: recibe el arrayPrimo(ahora con los valores cargados) y la variable "y"
- int i =0;
- for (int elemento: arrayPrimo) {
- if (y > elemento) {
- i++;
- }
- }return i;//PostCond: devolvera una variable de tipo entero
- }
- //Modulo para invertir la posicion de los nros en el arrayPrimo
- static public int[] invertirPrimo(int[] arrayPrimo, int[] arrayInvertido) {
- for (int i = 0; i < arrayPrimo.length; i++) {
- arrayInvertido[i] = arrayPrimo[arrayPrimo.length-1-i];
- }return arrayInvertido;
- }
- //Modulo para cargar por consola nros en el arrayPrimo. Firma de tipo Int[]
- static public int[] cargarPrimo(int[] arrayPrimo) {//PreCond: recive el arrayPrimo, el cual esta vacio
- int i = 0, numero;
- while(i < arrayPrimo.length) {
- numero = Helper.getInteger("ingrrese valor positivo");
- if(primo(numero)==true) {//Se llama el modulo para validar que el nro cargado sea Primo
- arrayPrimo[i] = numero;
- i ++;
- }else {
- System.out.println("El valor ingresado NO es Primo, reintentar...");
- }
- }return arrayPrimo;//PostCond: devuelve o retorna el mismo array con los valores cargados
- }
- //Modulo para generar y cargar los nros en el arrayPrimo. Firma de tipo Int[]
- static public int[] generarPrimo (int[] arrayPrimo) {//PreCond: recive el arrayPrimo, el cual esta vacio
- Random alea = new Random();
- int i = 0, numero;
- while (i < arrayPrimo.length) {
- numero = alea.nextInt(100);
- if (primo(numero)) {//Se llama el modulo para validar que el nro generado sea Primo
- arrayPrimo[i]=numero;
- i++;
- }
- }return arrayPrimo;//PostCond: devuelve o retorna el mismo array con los valores cargados
- }
- //Este modulo valida si el valor ingresado sea un numero PRIMO. Firma del tipo Boolean
- static public Boolean primo(int numero) {//PreCond: recibel la variable numero
- if(numero == 0 || numero == 1 || numero == 4){
- return false;
- }
- for (int x = 2; x<numero/2; x++) {
- if(numero % x == 0) {
- return false;//PostCond: retornara un false si la condicion se cumple
- }
- }return true;//PostCond: retornara un true si la condicon no se cumple
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment