mirror of
https://gitlab.com/tutorial-java-rafa-munoz/tutorial-java-2024-25/tutorialjava2024-25.git
synced 2025-11-09 18:03:09 +01:00
feat(UtilsArrays): added
This commit is contained in:
25
src/tutorialJava/UtilsArrays.java
Normal file
25
src/tutorialJava/UtilsArrays.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package tutorialJava;
|
||||
|
||||
public class UtilsArrays {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param a
|
||||
*/
|
||||
public static void inicializarArray(int a[], int limiteSup) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
a[i] = (int) Math.round(Math.random() * limiteSup);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param a
|
||||
*/
|
||||
public static void muestraArray(int a[]) {
|
||||
for (int i = 0; i < a.length; i++) {
|
||||
System.out.print(a[i] + " ");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -11,11 +11,11 @@ public class Ejemplo01_Bucle_For {
|
||||
* @param args
|
||||
*/
|
||||
public static void main (String args[]) {
|
||||
cuentaAdelanteYAtras();
|
||||
// cuentaAdelanteYAtras();
|
||||
// cuentaAdelanteConMultiplos();
|
||||
// tablaMultiplicar();
|
||||
// mayorDeUnaSerieDeNumeros();
|
||||
// numerosPrimos();
|
||||
numerosPrimos();
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ public class Ejemplo01_Bucle_For {
|
||||
|
||||
// Cuenta hacia delante
|
||||
System.out.println("Cuenta adelante con bucle for");
|
||||
for (int i = 0; i < 10; i++) {
|
||||
for (int i = 0; i == i; i++) {
|
||||
System.out.println("Valor de i: " + i);
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public class Ejemplo01_Bucle_For {
|
||||
public static void tablaMultiplicar () {
|
||||
int factor = Integer.parseInt(JOptionPane.showInputDialog("Introduzca número"));;
|
||||
|
||||
for (int i = 1; i < 11; i++) {
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
System.out.println(i + " x " + factor + " = " + (i * factor));
|
||||
}
|
||||
}
|
||||
@@ -76,11 +76,11 @@ public class Ejemplo01_Bucle_For {
|
||||
|
||||
for (int i = 0; num != 0; i++) {
|
||||
if (i == 0) { // Estoy en la primera iteración del bucle
|
||||
mayor = Integer.parseInt(JOptionPane.showInputDialog("Introduzca número: "));
|
||||
mayor = Integer.parseInt(JOptionPane.showInputDialog("Introduzca número: (0 para salir)"));
|
||||
}
|
||||
else {
|
||||
num = Integer.parseInt(JOptionPane.showInputDialog("Introduzca número: "));
|
||||
if (num > mayor && num!= 0) {
|
||||
num = Integer.parseInt(JOptionPane.showInputDialog("Introduzca número: (0 para salir)"));
|
||||
if (num > mayor && num != 0) {
|
||||
mayor = num;
|
||||
}
|
||||
}
|
||||
@@ -111,21 +111,23 @@ public class Ejemplo01_Bucle_For {
|
||||
*/
|
||||
public static void numerosPrimos () {
|
||||
|
||||
// int num = 7037;
|
||||
|
||||
for (int num = 100000000; true; num++) {
|
||||
|
||||
for (int num = 1; num < 1000000; num++) {
|
||||
boolean esPrimo = true;
|
||||
|
||||
for (int i = 2; i < num; i++) {
|
||||
if (num % i == 0) {
|
||||
esPrimo = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (esPrimo == true) {
|
||||
System.out.println(num + " es primo");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package tutorialJava.capitulo3_bucles.ejercicios.bloque05;
|
||||
|
||||
public class Ej01_sumaCifrasDeUnNumero {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int num = 2345;
|
||||
int sumaDigitos = 0;
|
||||
|
||||
for (int i = 10000000; i > 0; i/=10) {
|
||||
int digito = num / i;
|
||||
num = num % i;
|
||||
sumaDigitos += digito;
|
||||
}
|
||||
|
||||
System.out.println("Suma: " + sumaDigitos);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package tutorialJava.capitulo3_bucles.ejercicios.bloque05;
|
||||
|
||||
public class Ej02_SerieFibonacci {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int actual = 0, ant = 1, ant_1 = 1;
|
||||
|
||||
int cantidadValores = 6;
|
||||
|
||||
for (int i = 0; i < cantidadValores; i++) {
|
||||
ant_1 = ant;
|
||||
ant = actual;
|
||||
actual = ant + ant_1;
|
||||
System.out.print(actual + " ");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package tutorialJava.capitulo3_bucles.ejercicios.bloque05;
|
||||
|
||||
public class Ej03_NumerosPerfectos {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int limInf = 1, limSup = 150000;
|
||||
int sumaDivisores = 0;
|
||||
|
||||
|
||||
for (int i = limInf; i <= limSup; i++) {
|
||||
sumaDivisores = 0;
|
||||
for (int j = 1; j < i; j++) {
|
||||
if (i % j == 0) { // j es divisor de i
|
||||
sumaDivisores += j;
|
||||
}
|
||||
}
|
||||
// En "sumaDivisores" tengo la suma de todos
|
||||
// los divisores de i
|
||||
if (i == sumaDivisores) {
|
||||
System.out.println(i + " es un número perfecto");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package tutorialJava.capitulo3_bucles.ejercicios.bloque05;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Ej04_JuegoBuscarNumeroPensado {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int numeroPensado, tirada;
|
||||
Scanner sc = new Scanner(System.in);
|
||||
|
||||
numeroPensado = (int) Math.round(Math.random() * 100);
|
||||
|
||||
do {
|
||||
System.out.println("Dame un número: ");
|
||||
tirada = sc.nextInt();
|
||||
|
||||
if (tirada < numeroPensado) {
|
||||
System.out.println("Mi número pensado es mayor");
|
||||
}
|
||||
else {
|
||||
if (tirada > numeroPensado) {
|
||||
System.out.println("Mi número pensado es menor");
|
||||
}
|
||||
else {
|
||||
System.out.println("Enhorabuena, lo has encontrado");
|
||||
}
|
||||
}
|
||||
|
||||
} while(tirada != numeroPensado);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package tutorialJava.capitulo3_bucles.ejercicios.bloque05;
|
||||
|
||||
public class Ej05_TrianguloPascal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int filas = 5;
|
||||
|
||||
for (int i = 0; i < filas; i++) {
|
||||
for (int j = 0; j < (filas - i); j++) {
|
||||
System.out.print(" ");
|
||||
}
|
||||
|
||||
for (int j = 0; j <= i; j++) {
|
||||
int valor = (int) (factorial(i) /
|
||||
(factorial(j) * factorial (i - j) ));
|
||||
System.out.print(valor + " ");
|
||||
}
|
||||
System.out.println("");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
public static float factorial (int n) {
|
||||
if (n == 0) {
|
||||
return 1;
|
||||
}
|
||||
for (int i = n - 1; i > 1; i--) {
|
||||
n *= i;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ public class Ej01_EjemploArraysPrimitivos {
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
primerEjemploArray();
|
||||
// segundoEjemploArray();
|
||||
// primerEjemploArray();
|
||||
segundoEjemploArray();
|
||||
// tercerEjemploArray();
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public class Ej01_EjemploArraysPrimitivos {
|
||||
*/
|
||||
public static void segundoEjemploArray() {
|
||||
// Otra forma de inicializar el array
|
||||
int array2[] = new int[] {88, 89, 90, 4, 5, 6, 7, 8, 9, 10};
|
||||
int array2[] = new int[] {88, 89, 90, 4, 5, 8, 9, 10};
|
||||
|
||||
// Recorrido del array para imprimir sus valores en pantalla.
|
||||
// Este tipo de bucle se conoce como "for each".
|
||||
@@ -59,22 +59,22 @@ public class Ej01_EjemploArraysPrimitivos {
|
||||
float media;
|
||||
|
||||
// Declaración del array
|
||||
int array[] = new int[1000000];
|
||||
int array[] = new int[10];
|
||||
|
||||
// Recorro para inicializar el array
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
array[i] = (int) Math.round(Math.random()*10000);
|
||||
array[i] = (int) Math.round(Math.random() * 100);
|
||||
}
|
||||
|
||||
// Recorro e imprimo en pantalla
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
System.out.print(array[i] + " ");
|
||||
for (int n : array) {
|
||||
System.out.print(n + " ");
|
||||
}
|
||||
System.out.println("");
|
||||
|
||||
// Recorro para obtener la suma
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
suma += array[i];
|
||||
for (int n : array) {
|
||||
suma += n;
|
||||
}
|
||||
// Calculo la media
|
||||
media = suma / ((float) array.length);
|
||||
|
||||
15
src/tutorialJava/capitulo4_Arrays/EjemploMetodos.java
Normal file
15
src/tutorialJava/capitulo4_Arrays/EjemploMetodos.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package tutorialJava.capitulo4_Arrays;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class EjemploMetodos {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int array[] = new int[10];
|
||||
UtilsArrays.inicializarArray(array, 10000);
|
||||
UtilsArrays.muestraArray(array);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user