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 7): reorganised
This commit is contained in:
@@ -7,11 +7,11 @@ import java.io.FileReader;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
public class EjemploFicheroPropiedades {
|
public class Ejemplo01_FicheroPropiedades {
|
||||||
|
|
||||||
private static Properties propiedades = null;
|
private static Properties propiedades = null;
|
||||||
|
|
||||||
public EjemploFicheroPropiedades () {
|
public Ejemplo01_FicheroPropiedades () {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,9 +78,9 @@ public class EjemploFicheroPropiedades {
|
|||||||
* @param args
|
* @param args
|
||||||
*/
|
*/
|
||||||
public static void main (String args[]) {
|
public static void main (String args[]) {
|
||||||
String usuario = EjemploFicheroPropiedades.getProperty("USUARIO");
|
String usuario = Ejemplo01_FicheroPropiedades.getProperty("USUARIO");
|
||||||
String password = EjemploFicheroPropiedades.getProperty("PASSWORD");
|
String password = Ejemplo01_FicheroPropiedades.getProperty("PASSWORD");
|
||||||
int id = EjemploFicheroPropiedades.getIntProperty("ID_USUARIO");
|
int id = Ejemplo01_FicheroPropiedades.getIntProperty("ID_USUARIO");
|
||||||
|
|
||||||
System.out.println("Usuario leído del fichero de propiedades: " + usuario);
|
System.out.println("Usuario leído del fichero de propiedades: " + usuario);
|
||||||
System.out.println("Password leído del fichero de propiedades: " + password);
|
System.out.println("Password leído del fichero de propiedades: " + password);
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package tutorialJava.capitulo7_Recursos;
|
package tutorialJava.capitulo7_Recursos;
|
||||||
|
|
||||||
public class EjemploMath {
|
public class Ejemplo01_Math {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// La clase Math tiene una cantidad importante de métodos que podemos utilizar en cálculos muy diversos
|
// La clase Math tiene una cantidad importante de métodos que podemos utilizar en cálculos muy diversos
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package tutorialJava.capitulo7_Recursos;
|
package tutorialJava.capitulo7_Recursos;
|
||||||
|
|
||||||
public class EjemploString {
|
public class Ejemplo01_String {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String str = "Cadena de texto de tamaño mediano";
|
String str = "Cadena de texto de tamaño mediano";
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package tutorialJava.capitulo7_Recursos;
|
package tutorialJava.capitulo7_Recursos;
|
||||||
|
|
||||||
public class EjemploWrappersTiposPrimitivos {
|
public class Ejemplo01_WrappersTiposPrimitivos {
|
||||||
|
|
||||||
public static void main (String args[]) {
|
public static void main (String args[]) {
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ import java.util.GregorianCalendar;
|
|||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
public class EjemploDateCalendar {
|
public class Ejemplo02_DateCalendar {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@@ -199,7 +199,7 @@ public class EjemploDateCalendar {
|
|||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
ejemploCreacionNormalDate();
|
// ejemploCreacionNormalDate();
|
||||||
// ejemploFormateoFecha();
|
// ejemploFormateoFecha();
|
||||||
// ejemploCalendarBasico();
|
// ejemploCalendarBasico();
|
||||||
// ejemploCalendarConCamposIndividualizados();
|
// ejemploCalendarConCamposIndividualizados();
|
||||||
@@ -207,7 +207,7 @@ public class EjemploDateCalendar {
|
|||||||
// ejemploSumaRestaFechas();
|
// ejemploSumaRestaFechas();
|
||||||
// ejemploComparacionFechas();
|
// ejemploComparacionFechas();
|
||||||
// ejemploDiasEntreDosFechas();
|
// ejemploDiasEntreDosFechas();
|
||||||
// ejemploZonasHorarias();
|
ejemploZonasHorarias();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
package tutorialJava.capitulo7_Recursos;
|
package tutorialJava.capitulo7_Recursos;
|
||||||
|
|
||||||
public class EjemploRecursividad_Factorial {
|
public class Ejemplo05_Recursividad_Factorial {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Mediante no recursividad: " + factorialNoRecursivo(6));
|
System.out.println("Mediante no recursividad: " + factorialNoRecursivo(6));
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.excepciones;
|
package tutorialJava.capitulo7_Recursos.ejemplo03_excepciones;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
@@ -131,7 +131,9 @@ public class EjemploExcepcionesBasico {
|
|||||||
private static void ejemploClassNotFoundException () {
|
private static void ejemploClassNotFoundException () {
|
||||||
try {
|
try {
|
||||||
// Ejemplo de instanciaci<EFBFBD>n de un objeto
|
// Ejemplo de instanciaci<EFBFBD>n de un objeto
|
||||||
Persona persona = (Persona) Class.forName("tutorialJava.capitulo7_Recursos.excepciones.Persona").newInstance();
|
Persona persona = (Persona)
|
||||||
|
Class.forName("tutorialJava.capitulo7_Recursos.excepciones.Persona")
|
||||||
|
.newInstance();
|
||||||
System.out.println("Objeto instanciado: " + persona);
|
System.out.println("Objeto instanciado: " + persona);
|
||||||
|
|
||||||
// Ejemplo de obtenci<EFBFBD>n de las propiedades de un objeto
|
// Ejemplo de obtenci<EFBFBD>n de las propiedades de un objeto
|
||||||
@@ -197,10 +199,10 @@ public class EjemploExcepcionesBasico {
|
|||||||
*/
|
*/
|
||||||
public static void main (String args[]) {
|
public static void main (String args[]) {
|
||||||
|
|
||||||
ejemploExceptionGeneral();
|
// ejemploExceptionGeneral();
|
||||||
// ejemploNullPointerException();
|
// ejemploNullPointerException();
|
||||||
// ejemploArithmeticException();
|
// ejemploArithmeticException();
|
||||||
// ejemploClassCastException();
|
ejemploClassCastException();
|
||||||
// ejemploIndexOutOfBoundsException();
|
// ejemploIndexOutOfBoundsException();
|
||||||
// ejemploNumberFormatException();
|
// ejemploNumberFormatException();
|
||||||
// ejemploClassNotFoundException();
|
// ejemploClassNotFoundException();
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.excepciones;
|
package tutorialJava.capitulo7_Recursos.ejemplo03_excepciones;
|
||||||
|
|
||||||
public class EjemploLanzamientoExcepcionPropia {
|
public class EjemploLanzamientoExcepcionPropia {
|
||||||
|
|
||||||
@@ -9,7 +9,9 @@ public class EjemploLanzamientoExcepcionPropia {
|
|||||||
* @throws PersonaNulaException
|
* @throws PersonaNulaException
|
||||||
* @throws PiernaNulaException
|
* @throws PiernaNulaException
|
||||||
*/
|
*/
|
||||||
private static String personaToString (Persona persona) throws PersonaNulaException, PiernaNulaException {
|
private static String personaToString (Persona persona)
|
||||||
|
throws PersonaNulaException, PiernaNulaException {
|
||||||
|
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuffer sb = new StringBuffer();
|
||||||
// A partir de una persona, vamos a construir un StringBuffer que recorra sus dos piernas
|
// A partir de una persona, vamos a construir un StringBuffer que recorra sus dos piernas
|
||||||
if (persona == null) {
|
if (persona == null) {
|
||||||
@@ -46,8 +48,10 @@ public class EjemploLanzamientoExcepcionPropia {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Persona persona = new Persona(new Pierna(Pierna.DERECHA), new Pierna(Pierna.IZQUIERDA));
|
Persona persona = new Persona(
|
||||||
Persona persona = null;
|
new Pierna(Pierna.DERECHA),
|
||||||
|
new Pierna(Pierna.IZQUIERDA));
|
||||||
|
// Persona persona = null;
|
||||||
// Persona persona = new Persona(null, new Pierna(Pierna.IZQUIERDA));
|
// Persona persona = new Persona(null, new Pierna(Pierna.IZQUIERDA));
|
||||||
// Persona persona = new Persona(new Pierna(Pierna.DERECHA), null);
|
// Persona persona = new Persona(new Pierna(Pierna.DERECHA), null);
|
||||||
System.out.println(personaToString(persona));
|
System.out.println(personaToString(persona));
|
||||||
@@ -56,6 +60,8 @@ public class EjemploLanzamientoExcepcionPropia {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (PiernaNulaException e) {
|
} catch (PiernaNulaException e) {
|
||||||
System.out.println("Existen valores nulos en el objeto persona: " + e.getMessage());
|
System.out.println("Existen valores nulos en el objeto persona: " + e.getMessage());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.excepciones;
|
package tutorialJava.capitulo7_Recursos.ejemplo03_excepciones;
|
||||||
|
|
||||||
public class Persona {
|
public class Persona {
|
||||||
protected Pierna piernaDerecha;
|
protected Pierna piernaDerecha;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.excepciones;
|
package tutorialJava.capitulo7_Recursos.ejemplo03_excepciones;
|
||||||
|
|
||||||
public class PersonaNulaException extends Exception {
|
public class PersonaNulaException extends Exception {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.excepciones;
|
package tutorialJava.capitulo7_Recursos.ejemplo03_excepciones;
|
||||||
|
|
||||||
public class Pierna {
|
public class Pierna {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.excepciones;
|
package tutorialJava.capitulo7_Recursos.ejemplo03_excepciones;
|
||||||
|
|
||||||
public class PiernaNulaException extends Exception {
|
public class PiernaNulaException extends Exception {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.palabraRepetidaException;
|
package tutorialJava.capitulo7_Recursos.ejemplo03_excepciones.palabraRepetidaException;
|
||||||
|
|
||||||
public class PalabraRepetidaException extends Exception {
|
public class PalabraRepetidaException extends Exception {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.palabraRepetidaException;
|
package tutorialJava.capitulo7_Recursos.ejemplo03_excepciones.palabraRepetidaException;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploEntraPersonaListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploEntraPersonaListener;
|
||||||
|
|
||||||
public class Madre implements PersonaEntraListener {
|
public class Madre implements PersonaEntraListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploEntraPersonaListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploEntraPersonaListener;
|
||||||
|
|
||||||
public class Padre implements PersonaEntraListener {
|
public class Padre implements PersonaEntraListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploEntraPersonaListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploEntraPersonaListener;
|
||||||
|
|
||||||
public class PersonaEntraEvent {
|
public class PersonaEntraEvent {
|
||||||
private String nombrePer;
|
private String nombrePer;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploEntraPersonaListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploEntraPersonaListener;
|
||||||
|
|
||||||
public interface PersonaEntraListener {
|
public interface PersonaEntraListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploEntraPersonaListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploEntraPersonaListener;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploNumeroImparListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploNumeroImparListener;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploNumeroImparListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploNumeroImparListener;
|
||||||
|
|
||||||
public class Mesa implements NumeroImparListener {
|
public class Mesa implements NumeroImparListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploNumeroImparListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploNumeroImparListener;
|
||||||
|
|
||||||
public class NumeroImparEvent {
|
public class NumeroImparEvent {
|
||||||
private int numeroIntroducido;
|
private int numeroIntroducido;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploNumeroImparListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploNumeroImparListener;
|
||||||
|
|
||||||
public interface NumeroImparListener {
|
public interface NumeroImparListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploNumeroImparListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploNumeroImparListener;
|
||||||
|
|
||||||
public class Perro implements NumeroImparListener {
|
public class Perro implements NumeroImparListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploPalabraRepetidaEnFraseListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploPalabraRepetidaEnFraseListener;
|
||||||
|
|
||||||
public class CorrectorOrtografico implements PalabraRepetidaListener {
|
public class CorrectorOrtografico implements PalabraRepetidaListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploPalabraRepetidaEnFraseListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploPalabraRepetidaEnFraseListener;
|
||||||
|
|
||||||
public class Informatica implements PalabraRepetidaListener {
|
public class Informatica implements PalabraRepetidaListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploPalabraRepetidaEnFraseListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploPalabraRepetidaEnFraseListener;
|
||||||
|
|
||||||
public class PalabraRepetidaEvent {
|
public class PalabraRepetidaEvent {
|
||||||
String palabra;
|
String palabra;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploPalabraRepetidaEnFraseListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploPalabraRepetidaEnFraseListener;
|
||||||
|
|
||||||
public interface PalabraRepetidaListener {
|
public interface PalabraRepetidaListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.ejemploPalabraRepetidaEnFraseListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.ejemploPalabraRepetidaEnFraseListener;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.lluviaListener;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Aemet {
|
||||||
|
|
||||||
|
private static List<InteresadoLLuviaListener> lluviaListeners =
|
||||||
|
new ArrayList<InteresadoLLuviaListener>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param l
|
||||||
|
*/
|
||||||
|
public static void addInteresadoLluviaListener (
|
||||||
|
InteresadoLLuviaListener listener) {
|
||||||
|
lluviaListeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param listener
|
||||||
|
*/
|
||||||
|
public static void removeInteresadoLluviaListener (
|
||||||
|
InteresadoLLuviaListener listener) {
|
||||||
|
lluviaListeners.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static void fireEstaLloviendo(int litrosPorMetro) {
|
||||||
|
for (InteresadoLLuviaListener listener : lluviaListeners) {
|
||||||
|
InteresadoLluviaEvent event = new InteresadoLluviaEvent();
|
||||||
|
event.setLitrosPorMetro(litrosPorMetro);
|
||||||
|
listener.estaLloviendo(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param args
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
String respuestaLluvia;
|
||||||
|
|
||||||
|
new Telediario();
|
||||||
|
new ProgramaRadio();
|
||||||
|
|
||||||
|
|
||||||
|
System.out.println("Está lloviendo?");
|
||||||
|
respuestaLluvia = sc.nextLine();
|
||||||
|
|
||||||
|
if (respuestaLluvia.toLowerCase().startsWith("s")) {
|
||||||
|
System.out.println("Está lloviendo");
|
||||||
|
|
||||||
|
System.out.println("¿Cuánto está lloviendo?");
|
||||||
|
int litrosPorMetro = Integer.parseInt(sc.nextLine());
|
||||||
|
|
||||||
|
fireEstaLloviendo(litrosPorMetro);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.lluviaListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.lluviaListener;
|
||||||
|
|
||||||
public interface InteresadoLLuviaListener {
|
public interface InteresadoLLuviaListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.lluviaListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.lluviaListener;
|
||||||
|
|
||||||
public class InteresadoLluviaEvent {
|
public class InteresadoLluviaEvent {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.lluviaListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.lluviaListener;
|
||||||
|
|
||||||
public class ProgramaRadio implements InteresadoLLuviaListener {
|
public class ProgramaRadio implements InteresadoLLuviaListener {
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package tutorialJava.capitulo7_Recursos.lluviaListener;
|
package tutorialJava.capitulo7_Recursos.ejemplo04_listeners.lluviaListener;
|
||||||
|
|
||||||
public class Telediario implements InteresadoLLuviaListener {
|
public class Telediario implements InteresadoLLuviaListener {
|
||||||
|
|
||||||
Reference in New Issue
Block a user