feat(examen entrenamiento): 20230902

This commit is contained in:
Rafa Muñoz
2025-01-30 14:58:47 +01:00
parent bc80b72094
commit acb8877882
7 changed files with 287 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
package tutorialJava.examenesEntrenamiento.examen20230902;
public class Ejercicio02_NumeroEuler {
public static void main(String[] args) {
int i = 0;
double e = 0;
while ( Math.abs(Math.E - e) > 0.0000001 ) {
e += 1f/factorialRecursivo(i);
i++;
}
System.out.println("Mi E calculado: " + e);
}
public static int factorialRecursivo(int n) {
if (n <= 1) {
return 1;
}
return n * factorialRecursivo(n - 1);
}
}

View File

@@ -0,0 +1,35 @@
package tutorialJava.examenesEntrenamiento.examen20230902;
public class Ejercicio03_Frase {
public static void main(String[] args) {
String frase = "Hola1* qué tal estás";
// División de la frase en palabras
String palabras[] = frase.split("[ ]+");
for (String palabra : palabras) {
int contMayusculas = 0, contMinusculas = 0,
contDigito = 0, contNoAlfanumerico = 0;
for (int i = 0; i < palabra.length(); i++) {
if (Character.isUpperCase(palabra.charAt(i))) contMayusculas++;
if (Character.isLowerCase(palabra.charAt(i))) contMinusculas++;
if (Character.isDigit(palabra.charAt(i))) contDigito++;
if (!Character.isLetterOrDigit(palabra.charAt(i))) contNoAlfanumerico++;
if (contMayusculas > 0 && contMinusculas > 1
&& contDigito > 0 && contNoAlfanumerico > 0) {
System.out.println("Palabra seleccionada: " + palabra);
}
}
}
}
}

View File

@@ -0,0 +1,21 @@
package tutorialJava.examenesEntrenamiento.examen20230902;
import tutorialJava.Utils;
public class Ejercicio04_ColorPorRGB {
public static void main(String[] args) {
int rojo = Utils.obtenerEnteroEntreLimites(
"Introduce el valor para rojo: ", 0, 255);
int verde = Utils.obtenerEnteroEntreLimites(
"Introduce el valor para verde: ", 0, 255);
int azul = Utils.obtenerEnteroEntreLimites(
"Introduce el valor para azul: ", 0, 255);
System.out.println("#" + Integer.toHexString(rojo)
+ Integer.toHexString(verde)
+ Integer.toHexString(azul));
}
}

View File

@@ -0,0 +1,63 @@
package tutorialJava.examenesEntrenamiento.examen20230902;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
public class Ejercicio05_Fechas {
public static void main(String[] args) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
Date date;
try {
// Leo una fecha a partir de un String
date = sdf.parse("2023-02-09 10.30.27");
// Obtengo la misma fecha menos dos semanas, a través de Calendar
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.WEEK_OF_YEAR, -2);
Date dateMenosDosSemanas = new Date(cal.getTimeInMillis());
// Vamos a crear la misma fecha con Calendar, mediante 'set'
Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.YEAR, 2023);
cal2.set(Calendar.MONTH, Calendar.JANUARY);
cal2.set(Calendar.DAY_OF_YEAR, 26);
cal2.set(Calendar.HOUR, 10);
cal2.set(Calendar.MINUTE, 30);
cal2.set(Calendar.SECOND, 27);
// Diferencia horaria entre Galapagos y Lisboa
Calendar ahoraEnGalapagos =
Calendar.getInstance(TimeZone.getTimeZone("Pacific/Galapagos"));
Calendar ahoraEnLisboa =
Calendar.getInstance(TimeZone.getTimeZone("Europe/Lisbon"));
int horaEnGalapagos = ahoraEnGalapagos.get(Calendar.HOUR_OF_DAY);
int horaEnLisboa = ahoraEnLisboa.get(Calendar.HOUR_OF_DAY);
System.out.println("Diferencia horaria entre Galapagos y Lisboa: " +
Math.abs(horaEnGalapagos - horaEnLisboa));
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@@ -0,0 +1,47 @@
package tutorialJava.examenesEntrenamiento.examen20230902.ejercicio1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
public class Ejercicio01 {
public static void main(String[] args) {
HashMap<String, Object> hm = new HashMap<String, Object>();
// Leer propiedades y almacenar en hashmap
for (int i = 1; i <= 4; i++) {
String key = "VALOR_ENTERO_" + i;
hm.put(key,
LecturaFicheroPropiedades.getIntProperty(key));
key = "VALOR_STRING_" + i;
hm.put(key,
LecturaFicheroPropiedades.getProperty(key));
}
// Leer los objetos almacenados en el hashmap
List<Integer> enteros = new ArrayList<Integer>();
Iterator<Object> it = hm.values().iterator();
while (it.hasNext()) {
Object o = it.next();
if (o instanceof String) {
System.out.println("Hay un valor String: " + o.toString());
}
else if (o instanceof Integer) {
System.out.println("Hay un valor Integer: " + o.toString());
enteros.add((Integer) o);
}
}
}
}

View File

@@ -0,0 +1,87 @@
package tutorialJava.examenesEntrenamiento.examen20230902.ejercicio1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
public class LecturaFicheroPropiedades {
private static Properties propiedades = null;
public LecturaFicheroPropiedades () {
super();
}
/**
*
* @return
*/
private static Properties getPropiedades() {
if (propiedades == null) {
propiedades = new Properties();
try {
// Una forma de leer el fichero de propiedades
// propiedades.load(propiedades.getClass().getResourceAsStream("/tutorialJava/capitulo6_Recursos/ejemplo.properties"));
// Otra forma de leer el fichero de propiedades
File file = new File("./src/tutorialJava/examenesEntrenamiento/examen20230902/ejercicio1/ejercicio01.properties");
System.out.println("Fichero encontrado: " + file.exists());
propiedades.load(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return propiedades;
}
/**
*
* @param nombrePropiedad
* @return
*/
public static String getProperty(String nombrePropiedad) {
return getPropiedades().getProperty(nombrePropiedad);
}
/**
*
* @param nombrePropiedad
* @return
*/
public static int getIntProperty (String nombrePropiedad) {
return Integer.parseInt(getPropiedades().getProperty(nombrePropiedad));
}
/**
*
* @param nombrePropiedad
* @return
*/
public static Float getFloatProperty (String nombrePropiedad) {
return Float.parseFloat(getPropiedades().getProperty(nombrePropiedad));
}
/**
*
* @param args
*/
public static void main (String args[]) {
int valorEntero1 =
LecturaFicheroPropiedades.getIntProperty("VALOR_ENTERO_1");
System.out.println(valorEntero1);
}
}

View File

@@ -0,0 +1,11 @@
# Ejemplo de comentario en fichero de propiedades
# Propiedades
VALOR_ENTERO_1=10
VALOR_ENTERO_2=20
VALOR_ENTERO_3=30
VALOR_ENTERO_4=40
VALOR_STRING_1=Cadena1
VALOR_STRING_2=Cadena2
VALOR_STRING_3=Cadena3
VALOR_STRING_4=Cadena4