/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to
change this license
*/
package com.mycompany.taller_arreglos;
import javax.swing.*;
/*
Taller POO
Integrantes:
Sebastian Giraldo
Luis Perez
John Mejia
Daniel Charris
*/
public class Taller_Arreglos {
public static void main(String[] args) {
int v[] = new int[100];
String str = "";
int opcion, salir, n = 0, aux = 0;
do {
str = "Menu\n\n"
+ "1. Crear vector\n"
+ "2. Mostrar valores primos\n"
+ "3. Mostrar valores pares\n"
+ "4. Buscar valor en el vector\n"
+ "5. Buscar elemento central y cambiar por 0\n\n"
+ "Digite su opción";
opcion = Integer.parseInt(JOptionPane.showInputDialog(str));
switch (opcion) {
case 1:
n = Integer.parseInt(JOptionPane.showInputDialog("Se creara un
vector con valores aleatorios de 1 a 100;\n" + "Digite el tamaño del vector"));
crearVector(n, v);
break;
case 2:
mostrarPrimos(n, v);
break;
case 3:
JOptionPane.showMessageDialog(null, "Los elementos pares del
vector son\n" + elementosPares(n, v));
break;
case 4:
aux = Integer.parseInt(JOptionPane.showInputDialog("Digite el
valor a buscar dentro del vector creado\n"));
buscarValor(aux, n, v);
break;
case 5:
JOptionPane.showMessageDialog(null, "La posción central del
vector sera cambiada por 0\n"
+ "El nuevo vector sera\n" + (centralPosicion(n, v)));
break;
default:
JOptionPane.showMessageDialog(null, "Opción no valida");
break;
}
salir = JOptionPane.showConfirmDialog(null, "Desea Salir?");
} while (salir == 1);
public static void crearVector(int n, int[] v) {
String str = "";
for (int i = 0; i < n; i++) {
v[i] = (int) (Math.random() * 100 + 1);
str = str + v[i] + " ";
}
JOptionPane.showMessageDialog(null, "El vector ceado es:\n " + str);
}
public static void mostrarPrimos(int n, int[] v) {
String str = "";
for (int i = 0; i < n; i++) {
if (esPrimo(v[i])) {
str = str + v[i] + " ";
}
}
if (str == "") {
JOptionPane.showMessageDialog(null, "El vector no tiene números
primos");
} else {
JOptionPane.showMessageDialog(null, "Los números primos del vector son\
n " + str);
}
}
public static boolean esPrimo(int numero) {
// El 0 y 4 no son primos
if (numero == 0 || numero == 4) {
return false;
}
for (int x = 2; x < numero / 2; x++) {
// Si es divisible por cualquiera de estos números, no
// es primo
if (numero % x == 0) {
return false;
}
}
// Si no se pudo dividir por ninguno de los de arriba, sí es primo
return true;
}
public static String elementosPares(int n, int[] v) {
String str = "";
for (int i = 0; i < n; i++) {
if (v[i] % 2 == 0) {
str = str + v[i] + " ";
}
}
return str;
}
public static void buscarValor(int aux, int n, int[] v) {
String str = "";
for (int i = 0; i < n; i++) {
if (v[i] == aux) {
str = "Valor encontrado";
}
}
if (str == "") {
str = "Valor no encontrado";
}
JOptionPane.showMessageDialog(null, str);
}
public static String centralPosicion(int n, int[] v) {
String str = "";
if (n % 2 == 0) {
v[n / 2] = 0;
} else {
v[(n - 1) / 2] = 0;
}
for (int i = 0; i < n; i++) {
str = str + v[i] + " ";
}
return str;
}
}