Advertisement
angeles734

tp1 ...

Sep 15th, 2021
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.30 KB | None | 0 0
  1. //-UNIVERSIDAD NACIONAL DE JUJUY- FACULTAD DE INGENIERÍA                        -ESTRUCTURAS DE DATOS
  2. //-TRABAJO PRACTICO N°1: ARREGLOS, ARREGLOS DINÁMICOS, OBJETOS BÁSICOS          -GRUPO 3.2
  3. //-------------------------------------------------------------------------------------------------------  
  4. import java.util.Random;
  5. import java.util.Scanner;
  6.  
  7. public class EJERC2 {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner sc =new Scanner(System.in);
  11.        
  12.        
  13.         int OP;
  14.         int dim=PositivoInt(sc,"Ingrese la dimensión del arreglo: ");
  15.         int V[]=new int [dim];
  16.        
  17.         do {
  18.          OP=Menu(sc,"Ingrese una opción del menú: ");
  19.             System.out.println("");
  20.            
  21.             switch (OP){
  22.            
  23.             case 1:
  24.                      cargarVector(sc,V);
  25.                      Mostrar(V);
  26.                      MostrarInvertido(V);
  27.                      Pares(V);
  28.                      break;
  29.             case 2:
  30.                      VectorAleatorio(V);
  31.                      Mostrar(V);
  32.                      MostrarInvertido(V);  
  33.                      Pares(V);
  34.                      break;    
  35.             case 3:
  36.                     System.out.println("MUCHAS GRACIAS!!, HASTA LUEGO.");
  37.                     break;
  38.             default:
  39.                  System.out.println("Opción no valida");
  40.             }
  41.        
  42.              } while (OP!=3);
  43.          
  44.          sc.close();
  45.          
  46.     }
  47.  
  48.     public static int Menu(Scanner sc,String message ){
  49.         int opcion;
  50.         do {
  51.             System.out.println("-----------------------------------");
  52.             System.out.println(" // ELIJA UNA OPCIÓN DEL MENÚ//");
  53.             System.out.println("1.Ingresar números al arreglo ");
  54.             System.out.println("2.Generar números aleatoriamente ");
  55.             System.out.println("3.Salir");
  56.            
  57.             opcion=ReadInt(sc,message);
  58.             if(opcion<0 || opcion>3){
  59.                 System.out.println("\nError: Ingrese una opción valida");
  60.             }
  61.         }while(opcion<0 || opcion>3);
  62.         return opcion;
  63.        
  64.     }
  65.     public static int ReadInt(Scanner sc, String message) {
  66.         int num;
  67.         String line;
  68.         while (true) {
  69.             try {
  70.                 System.out.print(message);
  71.                 line = sc.nextLine();
  72.                 num = Integer.parseInt(line);
  73.                 break;
  74.             }
  75.              catch (Exception e) {
  76.                 System.out.println("\nERROR al cargar un número entero");
  77.             }
  78.         }
  79.         return num;
  80.     }
  81.     //Valida que el tamaño del vector sea positivo y entero
  82.     public static int PositivoInt(Scanner sc, String message)
  83.     {
  84.         int num;
  85.         while(true) {
  86.              num=ReadInt(sc,message);
  87.              if (num>0) {
  88.                  
  89.                  break;
  90.              }
  91.              else {
  92.                  System.out.println("El valor de dimensión del vector debe ser positivo y entero");
  93.              }
  94.         }
  95.      
  96.         return num;
  97.        
  98.     }
  99.    
  100.     public static void cargarVector(Scanner sc, int V[])
  101.     {  
  102.         System.out.println("Cargando vector de números múltiplos de 7 pero no de 3");
  103.         for (int i=0; i < V.length; i++ )
  104.         {
  105.             int num=ReadInt(sc,"Ingrese un número entero: ");
  106.             if ( Multiplo7(num) &&  !Multiplo3(num))
  107.             {
  108.                 V[i]=num;
  109.             }else {
  110.                 i=i-1;
  111.             }      
  112.         }
  113.     }
  114.    
  115.     public static void VectorAleatorio( int V[])
  116.     {
  117.             Random rnd=new Random();
  118.             for (int i =0; i < V.length; i++ )
  119.             {
  120.                 int num=rnd.nextInt(100)+1;
  121.                 if ( Multiplo7(num) &&  !Multiplo3(num))
  122.                 {
  123.                     V[i]=num;
  124.                 }else {
  125.                     i=i-1;
  126.                 }      
  127.             }
  128.            
  129.     }
  130.    
  131.  
  132.     public static boolean Multiplo7(int num)
  133.     {
  134.         boolean multiplo=false;
  135.         if (num % 7 ==0) {
  136.             multiplo=true;
  137.         }
  138.        
  139.         return multiplo;
  140.     }
  141.    
  142.     public static boolean Multiplo3(int num)
  143.     {
  144.         boolean multiplo=false;
  145.         if (num % 3 ==0) {
  146.             multiplo=true;
  147.         }
  148.        
  149.         return multiplo;
  150.     }
  151.    
  152.    
  153.     public static void Mostrar(int V[])
  154.    
  155.     {  
  156.         System.out.println("\nVector generado");
  157.    
  158.         for (int i=0; i < V.length; i++ )
  159.         {
  160.             System.out.println("Posicion"+ (i+1)+ ": " + V[i]);
  161.         }
  162.     }
  163.    
  164.     public static void MostrarInvertido(int V[])
  165.     {  
  166.         System.out.println("\nVector invertido");
  167.         for (int i=V.length-1; i >= 0; i-- )
  168.         {
  169.             System.out.println("Posicion"+ (i+1)+ ": " + V[i]);
  170.         }
  171.     }
  172.    
  173.     public static void Pares(int V[])
  174.     {
  175.        
  176.         int contPar=0;
  177.         for (int i=0; i < V.length; i++ )
  178.         {
  179.             if (V[i] % 2 ==0)
  180.                 contPar=contPar +1;
  181.         }
  182.           System.out.println("\nCantidad de números pares ingresados:" + contPar);
  183.           System.out.println("\nCantidad de números impares ingresados:" + (V.length - contPar));
  184.     }
  185.    
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement