Examen 2025/01/31

This commit is contained in:
2025-01-31 13:53:42 +01:00
parent 9d13a90b4c
commit a4a4c4520b
13 changed files with 349 additions and 8 deletions

1
.idea/misc.xml generated
View File

@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager"> <component name="MavenProjectsManager">

38
.idea/workspace.xml generated
View File

@@ -5,11 +5,18 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="e2c4f742-461e-4c7a-a6e0-b5f4b8e8f229" name="Changes" comment="Examen 2024/12/13"> <list default="true" id="e2c4f742-461e-4c7a-a6e0-b5f4b8e8f229" name="Changes" comment="Examen 2024/12/13">
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20241213/Animal.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio01/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20241213/Ave.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio02/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20241213/Mamifero.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio02/Persona.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20241213/Zoologico.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio02/exceptions/EdadInvalidaException.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20241213/utils/Utils.java" afterDir="false" /> <change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio03/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio04/Main.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio04/Matematico.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio04/listener/CheckeadorNums.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio04/listener/NumNegativoEvent.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio04/listener/NumeroNegativoListener.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/examenes/examen20250131/utils/Utils.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@@ -20,6 +27,7 @@
<component name="FileTemplateManagerImpl"> <component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES"> <option name="RECENT_TEMPLATES">
<list> <list>
<option value="Exception" />
<option value="Class" /> <option value="Class" />
</list> </list>
</option> </option>
@@ -45,15 +53,23 @@
"Application.Ejercicio_c.executor": "Run", "Application.Ejercicio_c.executor": "Run",
"Application.Ejercicio_d.executor": "Run", "Application.Ejercicio_d.executor": "Run",
"Application.Ejercicio_e.executor": "Run", "Application.Ejercicio_e.executor": "Run",
"Application.Main.executor": "Run",
"Application.Zoologico.executor": "Run", "Application.Zoologico.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true", "RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true", "RunOnceActivity.git.unshallow": "true",
"dart.analysis.tool.window.visible": "false",
"git-widget-placeholder": "main", "git-widget-placeholder": "main",
"kotlin-language-version-configured": "true", "kotlin-language-version-configured": "true",
"onboarding.tips.debug.path": "/Users/h4ckx0r/IdeaProjects/ExamenesProgramacion/src/main/java/net/h4ckx0r/Main.java" "last_opened_file_path": "/Users/h4ckx0r/IdeaProjects/ExamenesProgramacion/src/main/java/examenes/examen20250131/ejercicio04",
"onboarding.tips.debug.path": "/Users/h4ckx0r/IdeaProjects/ExamenesProgramacion/src/main/java/net/h4ckx0r/Main.java",
"settings.editor.selected.configurable": "preferences.pluginManager",
"show.migrate.to.gradle.popup": "false"
} }
}]]></component> }]]></component>
<component name="RecentsManager"> <component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS">
<recent name="$PROJECT_DIR$/src/main/java/examenes/examen20250131/ejercicio04" />
</key>
<key name="CopyClassDialog.RECENTS_KEY"> <key name="CopyClassDialog.RECENTS_KEY">
<recent name="examenes.examen20241213" /> <recent name="examenes.examen20241213" />
<recent name="examenes.examen20241122" /> <recent name="examenes.examen20241122" />
@@ -157,7 +173,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1732277745346</updated> <updated>1732277745346</updated>
</task> </task>
<option name="localTasksCounter" value="12" /> <task id="LOCAL-00012" summary="Examen 2024/12/13">
<option name="closed" value="true" />
<created>1734091528594</created>
<option name="number" value="00012" />
<option name="presentableId" value="LOCAL-00012" />
<option name="project" value="LOCAL" />
<updated>1734091528594</updated>
</task>
<option name="localTasksCounter" value="13" />
<servers /> <servers />
</component> </component>
<component name="Vcs.Log.Tabs.Properties"> <component name="Vcs.Log.Tabs.Properties">

View File

@@ -0,0 +1,44 @@
package examenes.examen20250131.ejercicio01;
import examenes.examen20250131.utils.Utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Main {
public static void main(String[] args) {
HashMap<String, Double> productos = new HashMap<String, Double>();
int opcion;
do {
System.out.println("Seleccione lo que desea hacer:\n" +
"0. Salir\n" +
"1. Introducir productos\n" +
"2. Ver lista de productos ordenada por nombre");
opcion = Utils.solicitarIntScannerInline("> ");
switch (opcion) {
case 1:
productos.put(Utils.solicitarStringScannerInline("Introduzca el nombre del producto: "),
Utils.solicitarDoubleScannerInline("\nIntroduzca el precio del producto: "));
break;
case 2:
List<String> listaProductos = new ArrayList<String>();
for (Object producto : productos.keySet().toArray()) {
listaProductos.add((String) producto);
}
listaProductos = Utils.ordenarBurbujaListaStrings(listaProductos);
for (String p : listaProductos) {
System.out.println(p + ": " + productos.get(p));
}
break;
}
} while (opcion != 0);
}
}

View File

@@ -0,0 +1,23 @@
package examenes.examen20250131.ejercicio02;
import examenes.examen20250131.ejercicio02.exceptions.EdadInvalidaException;
public class Main {
public static void main(String[] args) {
try {
Persona p1 = new Persona(18);
System.out.println(p1);
} catch (EdadInvalidaException e) {
System.err.println(e.getMessage());
}
try {
Persona p2 = new Persona(-18);
System.out.println(p2);
} catch (EdadInvalidaException e) {
System.err.println(e.getMessage());
}
}
}

View File

@@ -0,0 +1,27 @@
package examenes.examen20250131.ejercicio02;
import examenes.examen20250131.ejercicio02.exceptions.EdadInvalidaException;
public class Persona {
private int edad;
public Persona(int edad) throws EdadInvalidaException {
if (edad < 0) throw new EdadInvalidaException("La edad de una persona no puede ser negativa: " + edad);
this.edad = edad;
}
public int getEdad() {
return edad;
}
public void setEdad(int edad) {
this.edad = edad;
}
@Override
public String toString() {
return "Persona{" +
"edad=" + edad +
'}';
}
}

View File

@@ -0,0 +1,7 @@
package examenes.examen20250131.ejercicio02.exceptions;
public class EdadInvalidaException extends Exception {
public EdadInvalidaException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,33 @@
package examenes.examen20250131.ejercicio03;
import examenes.examen20250131.utils.Utils;
import java.util.Calendar;
import java.util.Date;
public class Main {
public static void main(String[] args) {
Date fechaInicial = null;
Date fechaFinal = null;
while (true) {
fechaInicial = Utils.solicitarFechaInline("dd/MM/yyyy HH:mm:ss");
fechaFinal = Utils.solicitarFechaInline("dd/MM/yyyy HH:mm:ss");
if (fechaInicial.before(fechaFinal) && fechaInicial.after(new Date())) break;
System.out.println("Es necesario que la fecha inicial sea antes de la final y que la fecha inicial sea después de la fecha actual");
}
System.out.println("Días de diferencia (Date): " + (Math.abs(fechaInicial.getTime() - fechaFinal.getTime()) / 1000) / 60 / 60 / 24 );
Calendar calInicial = Calendar.getInstance();
calInicial.setTimeInMillis(fechaInicial.getTime());
Calendar calFinal = Calendar.getInstance();
calFinal.setTimeInMillis(fechaFinal.getTime());
System.out.println("Días de diferencia (Calendar): " + Math.abs(calInicial.get(Calendar.DAY_OF_YEAR) - calFinal.get(Calendar.DAY_OF_YEAR)));
}
}

View File

@@ -0,0 +1,25 @@
package examenes.examen20250131.ejercicio04;
import examenes.examen20250131.ejercicio04.listener.CheckeadorNums;
import examenes.examen20250131.utils.Utils;
public class Main {
public static void main(String[] args) {
Matematico m1 = new Matematico();
Matematico m2 = new Matematico();
String texto = Utils.solicitarStringScannerInline("Introduzca una lista de números separador por espacios: ");
String[] arrayTexto = texto.strip().split("[ ]+");
int[] arrayNums = new int[arrayTexto.length];
for (int i = 0; i < arrayTexto.length; i++) {
arrayNums[i] = Integer.parseInt(arrayTexto[i]);
CheckeadorNums.comprobadorNums(arrayNums[i]);
}
}
}

View File

@@ -0,0 +1,16 @@
package examenes.examen20250131.ejercicio04;
import examenes.examen20250131.ejercicio04.listener.CheckeadorNums;
import examenes.examen20250131.ejercicio04.listener.NumNegativoEvent;
import examenes.examen20250131.ejercicio04.listener.NumeroNegativoListener;
public class Matematico implements NumeroNegativoListener {
public Matematico() {
CheckeadorNums.addNumNegativoListener(this);
}
@Override
public void numeroNegativo(NumNegativoEvent event) {
System.out.println("Soy un matemático y he visto que el número " + event.getNumero() + " es negativo.");
}
}

View File

@@ -0,0 +1,28 @@
package examenes.examen20250131.ejercicio04.listener;
import java.util.ArrayList;
import java.util.List;
public class CheckeadorNums {
static List<NumeroNegativoListener> listeners = new ArrayList<NumeroNegativoListener>();
public static void addNumNegativoListener(NumeroNegativoListener l) {
listeners.add(l);
}
public static void removeNumNegativoListener(NumeroNegativoListener l) {
listeners.remove(l);
}
private static void fireNumNegativo(int resultado) {
for (NumeroNegativoListener opl : listeners) {
opl.numeroNegativo(new NumNegativoEvent(resultado));
}
}
public static void comprobadorNums(int num) {
if (num < 0) fireNumNegativo(num);
}
}

View File

@@ -0,0 +1,24 @@
package examenes.examen20250131.ejercicio04.listener;
public class NumNegativoEvent {
private int numero;
public NumNegativoEvent(int numero) {
this.numero = numero;
}
public int getNumero() {
return numero;
}
public void setNumero(int numero) {
this.numero = numero;
}
@Override
public String toString() {
return "NumNegativoEvent{" +
"numero=" + numero +
'}';
}
}

View File

@@ -0,0 +1,7 @@
package examenes.examen20250131.ejercicio04.listener;
public interface NumeroNegativoListener {
public void numeroNegativo(NumNegativoEvent event);
}

View File

@@ -0,0 +1,84 @@
package examenes.examen20250131.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;
public class Utils {
public static int solicitarIntScannerInline(String pregunta) {
System.out.print(pregunta);
Scanner sc = new Scanner(System.in);
return sc.nextInt();
}
public static String solicitarStringScannerInline(String pregunta) {
System.out.print(pregunta);
Scanner sc = new Scanner(System.in);
return sc.nextLine();
}
public static double solicitarDoubleScannerInline(String pregunta) {
System.out.print(pregunta);
Scanner sc = new Scanner(System.in);
return sc.nextDouble();
}
public static List<String> ordenarBurbujaListaStrings(List<String> lista) {
boolean ordenando;
do {
ordenando = false;
for (int i = 0; i < lista.size(); i++) {
if (i != (lista.size() - 1) && comparaCadenas(lista.get(i), lista.get(i + 1)) == 1) {
String elementoTemporal = lista.get(i);
lista.set(i, lista.get(i + 1));
lista.set(i + 1, elementoTemporal);
ordenando = true;
}
}
} while (ordenando);
return lista;
}
/**
* Este método es del profesor
* @param string1
* @param string2
* @return
*/
public static int comparaCadenas(String string1, String string2) {
String s1 = string1.toLowerCase();
String s2 = string2.toLowerCase();
int menorLongitud = Math.min(s1.length(), s2.length());
for (int i = 0; i < menorLongitud; i++) {
if (s1.charAt(i) < s2.charAt(i)) return -1;
else if (s1.charAt(i) > s2.charAt(i)) return 1;
}
if (s1.length() == s2.length()) return 0;
return (s1.length() < s2.length())? -1 : 1;
}
public static Date solicitarFechaInline(String formato) {
Scanner sc = new Scanner(System.in);
SimpleDateFormat formatoFecha = new SimpleDateFormat(formato);
while (true) {
System.out.print("Introduzca una fecha con este formato ( " + formato + " ) > ");
String fechaIntroducida = sc.nextLine();
try {
return formatoFecha.parse(fechaIntroducida);
} catch (ParseException e) {
System.out.println("\nVuelva a introducir la fecha, en el formato correcto: " + formato);
}
}
}
}