Advertisement
angeles734

1tp1

Sep 15th, 2021
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.78 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.  
  5. import java.util.Scanner;
  6. import java.util.Random;
  7. import java.util.ArrayList;
  8.  
  9. public class EJERC4 {
  10.  
  11.     public static void main(String[] args) {
  12.        
  13. Scanner sc =new Scanner(System.in);
  14.          
  15.         int OP;
  16.         ArrayList <Integer> numeros= new ArrayList<>();
  17.        
  18.         do {
  19.          OP=Menu(sc,"Ingrese una opción del menú: ");
  20.             System.out.println("");
  21.            
  22.            
  23.             switch (OP){
  24.            
  25.             case 1:
  26.                      vectorDinamico(sc,numeros);
  27.                      Mostrar(sc,numeros);
  28.                      SumaPares(numeros);
  29.                      ProductoImp(numeros);
  30.                      System.out.println("Promedio de los números ingresados: "+  Promedio(numeros));
  31.                      System.out.println("Valores mayores al promedio: "+  MayorProm(numeros));
  32.                      numeros.clear();
  33.                      break;
  34.             case 2:
  35.                      vectorAleatorio(sc,numeros);
  36.                      Mostrar(sc,numeros);
  37.                      SumaPares(numeros);
  38.                      ProductoImp(numeros);
  39.                      System.out.println("Promedio de los números ingresados: "+  Promedio(numeros));
  40.                      System.out.println("Valores mayores al promedio: "+  MayorProm(numeros));
  41.                      numeros.clear();
  42.                       break;    
  43.             case 3:
  44.                      System.out.println("MUCHAS GRACIAS!!, HASTA LUEGO.");
  45.                      break;
  46.             default:
  47.                      System.out.println("Opción no valida");
  48.             }
  49.        
  50.              } while (OP!=3);
  51.          
  52.          sc.close();
  53.          
  54.     }
  55.  
  56.     public static int Menu(Scanner sc,String message ){
  57.         int opcion;
  58.         do {
  59.             System.out.println("-----------------------------------");
  60.             System.out.println(" // ELIJA UNA OPCIÓN DEL MENÚ//");
  61.             System.out.println("1.Ingresar números al arreglo ");
  62.             System.out.println("2.Generar números aleatoriamente ");
  63.             System.out.println("3.Salir");
  64.            
  65.             opcion=ReadInt(sc,message);
  66.             if(opcion<0 || opcion>3){
  67.                 System.out.println("\nError: Ingrese una opción valida");
  68.             }
  69.         }while(opcion<0 || opcion>3);
  70.         return opcion;
  71.        
  72.     }
  73.     public static int ReadInt(Scanner sc, String message) {
  74.         int num;
  75.         String line;
  76.         while (true) {
  77.             try {
  78.                 System.out.print(message);
  79.                 line = sc.nextLine();
  80.                 num = Integer.parseInt(line);
  81.                 break;
  82.             }
  83.              catch (Exception e) {
  84.                 System.out.println("\nERROR al cargar un número entero");
  85.             }
  86.         }
  87.         return num;
  88.     }
  89.    
  90.     public static char getChar(Scanner sc, String message)
  91.     {
  92.         char character;
  93.         while (true) {
  94.             try {
  95.                 System.out.print(message);
  96.                 character = sc.nextLine().charAt(0);
  97.                 break;
  98.             }
  99.             catch (Exception StringIndexOutOfBoundsException) {
  100.                  System.out.println("\nERROR de ingreso ");
  101.             }
  102.        }
  103.             return character;
  104.     }
  105.        
  106.     public static char ReadChar(Scanner sc,String message)
  107.     {
  108.         char rta;
  109.         do {
  110.             rta = getChar(sc,message);
  111.             if (rta!='s' & rta!='S' & rta!='N' & rta!='n') {
  112.                 System.out.println("Error!!Ingrese S o N " );
  113.             }
  114.         }while (rta!='s' & rta!='S' & rta!='N' & rta!='n');
  115.         return rta;
  116.     }
  117.    
  118.  
  119.  static void vectorDinamico(Scanner sc,ArrayList<Integer> numeros)
  120.     {  
  121.         char resp;
  122.          
  123.        
  124.         do {
  125.             numeros.add(ReadInt(sc,"Ingrese un número entero: "));        
  126.             resp=ReadChar(sc, "\n¿Desea cargar más números? (S/N): ");
  127.            
  128.         }while ((resp=='S')|| (resp=='s'));
  129.        
  130.     }
  131.    
  132.     public static void vectorAleatorio(Scanner sc,ArrayList<Integer> numeros)
  133.     {  
  134.         Random rnd=new Random();
  135.         char resp;
  136.         System.out.println("Se generó el primer número");
  137.         do {
  138.             numeros.add(rnd.nextInt(100)+1);    
  139.             resp=ReadChar(sc, "\n¿Desea generar otro número? (S/N): ");
  140.                
  141.         }while ((resp=='S')|| (resp=='s'));
  142.  
  143.     }  
  144.    
  145.     public static void Mostrar(Scanner sc,ArrayList<Integer> numeros)
  146.     {
  147.         System.out.println("\nVector de números generados:");
  148.         for (int i=0; i < numeros.size(); i++ )
  149.         {
  150.             System.out.println("Posicion"+ (i+1)+ ": " + numeros.get(i));
  151.         }
  152.    
  153.     }
  154.    
  155.       public static void SumaPares(ArrayList<Integer> numeros)
  156.       {
  157.           int suma=0;
  158.           for (int i=0; i < numeros.size(); i++ )
  159.           {
  160.               if (EsPar(numeros.get(i)))
  161.               {
  162.                   suma= numeros.get(i) + suma;
  163.               }
  164.           }
  165.           System.out.println("Suma de los números pares: "+ suma);
  166.       }
  167.      
  168.       public static boolean EsPar(int num)
  169.       {
  170.           boolean par=false;
  171.           if (num %2 ==0)
  172.           {
  173.               par=true;
  174.           }
  175.           return par;
  176.       }
  177.      
  178.       public static void ProductoImp(ArrayList<Integer> numeros)
  179.       {
  180.           int produc=1;
  181.           int cont=0;
  182.           for (int i=0; i < numeros.size(); i++ ) {
  183.               if(!EsPar(numeros.get(i))) {
  184.                   produc=numeros.get(i)*produc;
  185.                   cont=cont+1;
  186.               }
  187.           }
  188.           if (cont==0) {
  189.               System.out.println("Producto de los números impares: No se ingresaron números impares");
  190.           }
  191.           else
  192.           System.out.println("Producto de los números impares: "+ produc);
  193.       }
  194.      
  195.       public static float Promedio(ArrayList<Integer>numeros)
  196.       {
  197.           float prom;
  198.         float suma=0;
  199.           for (int i=0; i < numeros.size(); i++ ) {
  200.               suma=numeros.get(i) +suma;
  201.           }
  202.           prom=suma/numeros.size();
  203.           return prom;
  204.       }
  205.       public static int MayorProm(ArrayList<Integer>numeros)
  206.       {
  207.           int may=0;
  208.           for (int i=0; i < numeros.size(); i++ ) {
  209.               if(numeros.get(i) > Promedio(numeros)) {
  210.                   may=may+1;
  211.               }
  212.           }
  213.           return may;
  214.       }
  215.      
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement