From d52834bd8d210c40c52a2b86a0908c30cf7d8813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=20Mu=C3=B1oz?= Date: Tue, 19 Nov 2024 09:46:58 +0100 Subject: [PATCH] feat(ch 4): solved ex blocks 9 & 10 --- .../bloque09/Ejercicio01_StringFromArray.java | 33 ++++++++++++++ .../bloque09/Ejercicio02_findAndReplace.java | 35 +++++++++++++++ .../bloque09/Ejercicio03_addInteger.java | 35 +++++++++++++++ .../Ejercicio04_countOccurrences.java | 33 ++++++++++++++ .../bloque09/Ejercicio05_removeInteger.java | 43 ++++++++++++++++++ ...PrimitivaArrayConValoresSinRepeticion.java | 40 +++++++++++++++++ .../Ejercicio02_OndaDeAsteriscosAlAzar.java | 44 +++++++++++++++++++ ...03_NumerosAlAzarHastaMediaProximaA500.java | 21 +++++++++ ...Ejercicio04_MatrizConBordeYCeroAlAzar.java | 39 ++++++++++++++++ ...cio05_MatrizConBordeYCerosYUnosAlAzar.java | 40 +++++++++++++++++ 10 files changed, 363 insertions(+) create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio01_StringFromArray.java create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio02_findAndReplace.java create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio03_addInteger.java create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio04_countOccurrences.java create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio05_removeInteger.java create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio01_LoteriaPrimitivaArrayConValoresSinRepeticion.java create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio02_OndaDeAsteriscosAlAzar.java create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio03_NumerosAlAzarHastaMediaProximaA500.java create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio04_MatrizConBordeYCeroAlAzar.java create mode 100644 src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio05_MatrizConBordeYCerosYUnosAlAzar.java diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio01_StringFromArray.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio01_StringFromArray.java new file mode 100644 index 0000000..1432ac9 --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio01_StringFromArray.java @@ -0,0 +1,33 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque09; + +public class Ejercicio01_StringFromArray { + + /** + * + * @param args + */ + public static void main(String[] args) { + int a[] = new int[] {97, 98, 99, 100}; + + System.out.println("String devuelto: " + stringFromArray(a)); + } + + /** + * + * @param b + * @return + */ + public static String stringFromArray (int b[]) { + StringBuffer sb = new StringBuffer(); + + for (int num : b) { + sb.append((char) num); + } + +// for (int i = 0; i < b.length; i++) { +// sb.append((char) b[i]); +// } + return sb.toString(); + } + +} diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio02_findAndReplace.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio02_findAndReplace.java new file mode 100644 index 0000000..01c3ce6 --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio02_findAndReplace.java @@ -0,0 +1,35 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque09; + +import tutorialJava.UtilsArrays; + +public class Ejercicio02_findAndReplace { + + /** + * + * @param args + */ + public static void main(String[] args) { + int a[] = new int[] {97, 98, 99, 100}; + + UtilsArrays.imprimeArray(a); + findAndReplace(a, 99, 115); + UtilsArrays.imprimeArray(a); + + } + + /** + * + * @param b + * @return + */ + public static void findAndReplace (int b[], int buscado, int reemplazo) { + StringBuffer sb = new StringBuffer(); + + for (int i = 0; i < b.length; i++) { + if (b[i] == buscado) { + b[i] = reemplazo; + } + } + } + +} diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio03_addInteger.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio03_addInteger.java new file mode 100644 index 0000000..3d22955 --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio03_addInteger.java @@ -0,0 +1,35 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque09; + +import tutorialJava.UtilsArrays; + +public class Ejercicio03_addInteger { + + /** + * + * @param args + */ + public static void main(String[] args) { + int a[] = new int[] {97, 98, 99, 100}; + + UtilsArrays.imprimeArray(a); + UtilsArrays.imprimeArray(addInteger(a, 99)); + + } + + /** + * + * @param b + * @return + */ + public static int[] addInteger (int b[], int agregado) { + int c[] = new int[b.length + 1]; + + for (int i = 0; i < b.length; i++) { + c[i] = b[i]; + } + + c[c.length - 1] = agregado; + return c; + } + +} diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio04_countOccurrences.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio04_countOccurrences.java new file mode 100644 index 0000000..ca0038e --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio04_countOccurrences.java @@ -0,0 +1,33 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque09; + +import tutorialJava.UtilsArrays; + +public class Ejercicio04_countOccurrences { + + /** + * + * @param args + */ + public static void main(String[] args) { + int a[] = new int[] {97, 98, 99, 100, 99, 98}; + + UtilsArrays.imprimeArray(a); + System.out.println("Ocurrencias de 99: " + countOcurrences(a, 99)); + + } + + /** + * + * @param b + * @return + */ + public static int countOcurrences (int b[], int occurrence) { + int cont = 0; + + for (int i : b) { + if (i == occurrence) cont++; + } + return cont; + } + +} diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio05_removeInteger.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio05_removeInteger.java new file mode 100644 index 0000000..a8ab7fd --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque09/Ejercicio05_removeInteger.java @@ -0,0 +1,43 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque09; + +import tutorialJava.UtilsArrays; + +public class Ejercicio05_removeInteger { + + /** + * + * @param args + */ + public static void main(String[] args) { + int a[] = new int[] {99, 98, 99, 100, 99, 98}; + + UtilsArrays.imprimeArray(a); + UtilsArrays.imprimeArray(removeInteger(a, 99)); + + } + + /** + * + * @param b + * @return + */ + public static int[] removeInteger (int b[], int forRemove) { + int contOccurrences = + Ejercicio04_countOccurrences.countOcurrences(b, forRemove); + int c[] = new int[b.length - contOccurrences]; + + int j = 0; // Sólo se utiliza en el nuevo array creado + for (int num : b) { + if (num != forRemove) { + c[j] = num; + j++; + } + } + return c; + } + +} + + + + diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio01_LoteriaPrimitivaArrayConValoresSinRepeticion.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio01_LoteriaPrimitivaArrayConValoresSinRepeticion.java new file mode 100644 index 0000000..775ce29 --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio01_LoteriaPrimitivaArrayConValoresSinRepeticion.java @@ -0,0 +1,40 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque10; + +public class Ejercicio01_LoteriaPrimitivaArrayConValoresSinRepeticion { + + /** + * + * @param args + */ + public static void main(String[] args) { + // Declaración de variables + int apuesta[] = new int[7]; + boolean numEncontradoEnPosicionesPrevias; + + // Un bucle que recorre el array de izquiera a derecha + for (int i = 0; i < apuesta.length; i++) { + + // No sé cuántas veces tendré que genera un número al azar para asegurarme + // de que no existe en los valores que se sitúan a la izquierda de la + // posición marcada por el valor de la "i". + do { + numEncontradoEnPosicionesPrevias = false; + apuesta[i] = (int) Math.round(Math.random() * (49 - 1) + 1); + + // Busco el valor de la posición de "i" en las posiciones a la izquierda + // de ese valor + for (int j = 0; j < i; j++) { + if (apuesta[i] == apuesta[j]) { + numEncontradoEnPosicionesPrevias = true; + } + } + } while (numEncontradoEnPosicionesPrevias == true); + } + + // Recorro y muestro el array + for (int i = 0; i < apuesta.length; i++) { + System.out.print(apuesta[i] + " "); + } + } + +} diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio02_OndaDeAsteriscosAlAzar.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio02_OndaDeAsteriscosAlAzar.java new file mode 100644 index 0000000..e53f9e9 --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio02_OndaDeAsteriscosAlAzar.java @@ -0,0 +1,44 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque10; + +public class Ejercicio02_OndaDeAsteriscosAlAzar { + + /** + * + * @param args + */ + public static void main(String[] args) { + // Declaración de variables + char matriz[][] = new char[10][30]; + int asteriscosEnCadaLinea; + + // Relleno toda la matriz de espacios en blanco + for (int i = 0; i < matriz.length; i++) { + for (int j = 0; j < matriz[i].length; j++) { + matriz[i][j] = ' '; + } + } + + // Recorro todas las filas + for (int i = 0; i < matriz.length; i++) { + // Determina la cantidad de asteriscos a introducir en cada línea + asteriscosEnCadaLinea = (int) Math.round(Math.random() * 30); + + // Llenar tantos asteriscos desde la izquierda como indique la variable + for (int j = 0; j < asteriscosEnCadaLinea; j++) { + matriz[i][j] = '*'; + } + + } + + // Imprimo en consola + for (int i = 0; i < matriz.length; i++) { + for (int j = 0; j < matriz[i].length; j++) { + System.out.print(matriz[i][j]); + } + System.out.println(); + } + + + } + +} diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio03_NumerosAlAzarHastaMediaProximaA500.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio03_NumerosAlAzarHastaMediaProximaA500.java new file mode 100644 index 0000000..df5c722 --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio03_NumerosAlAzarHastaMediaProximaA500.java @@ -0,0 +1,21 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque10; + +public class Ejercicio03_NumerosAlAzarHastaMediaProximaA500 { + + public static void main(String[] args) { + int numAzar, suma = 0, contador = 0; + float media; + + + do { + numAzar = (int) Math.round(Math.random() * 1000); + suma += numAzar; + contador++; + media = suma / (float) contador; + } while (media < 499.5 || media > 500.5); + + System.out.println("He necesitado " + contador + " números para tener una media " + + " de " + media); + } + +} diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio04_MatrizConBordeYCeroAlAzar.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio04_MatrizConBordeYCeroAlAzar.java new file mode 100644 index 0000000..1d8f525 --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio04_MatrizConBordeYCeroAlAzar.java @@ -0,0 +1,39 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque10; + +public class Ejercicio04_MatrizConBordeYCeroAlAzar { + + public static void main(String[] args) { + // Declaración de variables + char matriz[][] = new char[20][10]; + int posicionAzarFila, posicionAzarColumna; + + // La relleno de espacios en blanco + for (int i = 0; i < matriz.length; i++) { + for (int j = 0; j < matriz[i].length; j++) { + if (i == 0 || i == (matriz.length - 1) || j == 0 || j == (matriz[0].length - 1)) { + matriz[i][j] = '*'; + } + else { + matriz[i][j] = ' '; + } + } + } + + // Posición del 0 con valores al azar en fila y columna + posicionAzarFila = (int) Math.round(Math.random() * (matriz.length - 2 - 1) + 1); + posicionAzarColumna = (int) Math.round(Math.random() * (matriz[0].length - 2 - 1) + 1); + // Coloco el cero + matriz[posicionAzarFila][posicionAzarColumna] = '0'; + + // Imprimo en consola + for (int i = 0; i < matriz.length; i++) { + for (int j = 0; j < matriz[i].length; j++) { + System.out.print(matriz[i][j]); + } + System.out.println(); + } + + + } + +} diff --git a/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio05_MatrizConBordeYCerosYUnosAlAzar.java b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio05_MatrizConBordeYCerosYUnosAlAzar.java new file mode 100644 index 0000000..ce88feb --- /dev/null +++ b/src/tutorialJava/capitulo4_Arrays/ejercicios/bloque10/Ejercicio05_MatrizConBordeYCerosYUnosAlAzar.java @@ -0,0 +1,40 @@ +package tutorialJava.capitulo4_Arrays.ejercicios.bloque10; + +public class Ejercicio05_MatrizConBordeYCerosYUnosAlAzar { + + public static void main(String[] args) { + // Declaración de variables + char matriz[][] = new char[20][10]; + int probabilidad50; + + // La relleno de espacios en blanco + for (int i = 0; i < matriz.length; i++) { + for (int j = 0; j < matriz[i].length; j++) { + if (i == 0 || i == (matriz.length - 1) || j == 0 || j == (matriz[0].length - 1)) { + matriz[i][j] = '*'; + } + else { + // Relleno de 0 y 1 al 50% de probabilidad + probabilidad50 = (int) Math.round(Math.random()); + if (probabilidad50 == 0) { + matriz[i][j] = '0'; + } + else { + matriz[i][j] = '1'; + } + } + } + } + + // Imprimo en consola + for (int i = 0; i < matriz.length; i++) { + for (int j = 0; j < matriz[i].length; j++) { + System.out.print(matriz[i][j]); + } + System.out.println(); + } + + + } + +}