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(ch 4): solved ex blocks 9 & 10
This commit is contained in:
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -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] + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user