Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-UNIVERSIDAD NACIONAL DE JUJUY- FACULTAD DE INGENIERÍA -ESTRUCTURAS DE DATOS
- //-TRABAJO PRACTICO N°1: ARREGLOS, ARREGLOS DINÁMICOS, OBJETOS BÁSICOS -GRUPO 3.2
- //-------------------------------------------------------------------------------------------------------
- import java.util.Scanner;
- import java.util.Random;
- import java.util.ArrayList;
- public class EJERC4 {
- public static void main(String[] args) {
- Scanner sc =new Scanner(System.in);
- int OP;
- ArrayList <Integer> numeros= new ArrayList<>();
- do {
- OP=Menu(sc,"Ingrese una opción del menú: ");
- System.out.println("");
- switch (OP){
- case 1:
- vectorDinamico(sc,numeros);
- Mostrar(sc,numeros);
- SumaPares(numeros);
- ProductoImp(numeros);
- System.out.println("Promedio de los números ingresados: "+ Promedio(numeros));
- System.out.println("Valores mayores al promedio: "+ MayorProm(numeros));
- numeros.clear();
- break;
- case 2:
- vectorAleatorio(sc,numeros);
- Mostrar(sc,numeros);
- SumaPares(numeros);
- ProductoImp(numeros);
- System.out.println("Promedio de los números ingresados: "+ Promedio(numeros));
- System.out.println("Valores mayores al promedio: "+ MayorProm(numeros));
- numeros.clear();
- break;
- case 3:
- System.out.println("MUCHAS GRACIAS!!, HASTA LUEGO.");
- break;
- default:
- System.out.println("Opción no valida");
- }
- } while (OP!=3);
- sc.close();
- }
- public static int Menu(Scanner sc,String message ){
- int opcion;
- do {
- System.out.println("-----------------------------------");
- System.out.println(" // ELIJA UNA OPCIÓN DEL MENÚ//");
- System.out.println("1.Ingresar números al arreglo ");
- System.out.println("2.Generar números aleatoriamente ");
- System.out.println("3.Salir");
- opcion=ReadInt(sc,message);
- if(opcion<0 || opcion>3){
- System.out.println("\nError: Ingrese una opción valida");
- }
- }while(opcion<0 || opcion>3);
- return opcion;
- }
- public static int ReadInt(Scanner sc, String message) {
- int num;
- String line;
- while (true) {
- try {
- System.out.print(message);
- line = sc.nextLine();
- num = Integer.parseInt(line);
- break;
- }
- catch (Exception e) {
- System.out.println("\nERROR al cargar un número entero");
- }
- }
- return num;
- }
- public static char getChar(Scanner sc, String message)
- {
- char character;
- while (true) {
- try {
- System.out.print(message);
- character = sc.nextLine().charAt(0);
- break;
- }
- catch (Exception StringIndexOutOfBoundsException) {
- System.out.println("\nERROR de ingreso ");
- }
- }
- return character;
- }
- public static char ReadChar(Scanner sc,String message)
- {
- char rta;
- do {
- rta = getChar(sc,message);
- if (rta!='s' & rta!='S' & rta!='N' & rta!='n') {
- System.out.println("Error!!Ingrese S o N " );
- }
- }while (rta!='s' & rta!='S' & rta!='N' & rta!='n');
- return rta;
- }
- static void vectorDinamico(Scanner sc,ArrayList<Integer> numeros)
- {
- char resp;
- do {
- numeros.add(ReadInt(sc,"Ingrese un número entero: "));
- resp=ReadChar(sc, "\n¿Desea cargar más números? (S/N): ");
- }while ((resp=='S')|| (resp=='s'));
- }
- public static void vectorAleatorio(Scanner sc,ArrayList<Integer> numeros)
- {
- Random rnd=new Random();
- char resp;
- System.out.println("Se generó el primer número");
- do {
- numeros.add(rnd.nextInt(100)+1);
- resp=ReadChar(sc, "\n¿Desea generar otro número? (S/N): ");
- }while ((resp=='S')|| (resp=='s'));
- }
- public static void Mostrar(Scanner sc,ArrayList<Integer> numeros)
- {
- System.out.println("\nVector de números generados:");
- for (int i=0; i < numeros.size(); i++ )
- {
- System.out.println("Posicion"+ (i+1)+ ": " + numeros.get(i));
- }
- }
- public static void SumaPares(ArrayList<Integer> numeros)
- {
- int suma=0;
- for (int i=0; i < numeros.size(); i++ )
- {
- if (EsPar(numeros.get(i)))
- {
- suma= numeros.get(i) + suma;
- }
- }
- System.out.println("Suma de los números pares: "+ suma);
- }
- public static boolean EsPar(int num)
- {
- boolean par=false;
- if (num %2 ==0)
- {
- par=true;
- }
- return par;
- }
- public static void ProductoImp(ArrayList<Integer> numeros)
- {
- int produc=1;
- int cont=0;
- for (int i=0; i < numeros.size(); i++ ) {
- if(!EsPar(numeros.get(i))) {
- produc=numeros.get(i)*produc;
- cont=cont+1;
- }
- }
- if (cont==0) {
- System.out.println("Producto de los números impares: No se ingresaron números impares");
- }
- else
- System.out.println("Producto de los números impares: "+ produc);
- }
- public static float Promedio(ArrayList<Integer>numeros)
- {
- float prom;
- float suma=0;
- for (int i=0; i < numeros.size(); i++ ) {
- suma=numeros.get(i) +suma;
- }
- prom=suma/numeros.size();
- return prom;
- }
- public static int MayorProm(ArrayList<Integer>numeros)
- {
- int may=0;
- for (int i=0; i < numeros.size(); i++ ) {
- if(numeros.get(i) > Promedio(numeros)) {
- may=may+1;
- }
- }
- return may;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement