apuntes:introduccion
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
apuntes:introduccion [2023/06/04 11:23] – [Conversión de tipos] Santiago Faci | apuntes:introduccion [2023/10/22 12:28] (current) – [Sentencias prácticas] Santiago Faci | ||
---|---|---|---|
Line 302: | Line 302: | ||
public static final float IVA = 0.21; | public static final float IVA = 0.21; | ||
public static final int NUMERO_PAGINAS = 10; | public static final int NUMERO_PAGINAS = 10; | ||
+ | </ | ||
+ | |||
+ | ===== Enumeraciones ===== | ||
+ | |||
+ | * Permiten definir nuevos tipos de datos con rangos de valores muy limitados | ||
+ | * Sustituyen a las constantes para casos en los que éstas estaban relacionadas entre sí (distintos valores de un mismo rango) | ||
+ | * Al final no dejan de ser una serie de constantes pero agrupadas entre sí y con cierto contexto añadido | ||
+ | * Una vez definida una enumeración, | ||
+ | |||
+ | <code java> | ||
+ | public enum Direccion { | ||
+ | ARRIBA, ABAJO, DERECHA, IZQUIERDA | ||
+ | } | ||
+ | . . . | ||
+ | private Direccion direccion; | ||
+ | direccion = Direccion.ABAJO; | ||
+ | </ | ||
+ | |||
+ | === Definir tipos enumerados === | ||
+ | |||
+ | <code java> | ||
+ | public enum Estacion { | ||
+ | PRIMAVERA (10, 20.5f), | ||
+ | VERANO (23, 40), | ||
+ | OTONO (5.3f, 15), | ||
+ | INVIERNO (-2.1f, 10); | ||
+ | |||
+ | private final float temperaturaMinima; | ||
+ | private final float temperaturaMaxima; | ||
+ | |||
+ | Estacion(float minima, float maxima) { | ||
+ | temperaturaMinima = minima; | ||
+ | temperaturaMaxima = maxima; | ||
+ | } | ||
+ | | ||
+ | public float temperaturaMinima() { | ||
+ | return temperaturaMinima; | ||
+ | } | ||
+ | | ||
+ | public float temperaturaMaxima() { | ||
+ | return temperaturaMaxima; | ||
+ | } | ||
+ | | ||
+ | public float diferenciaTemperatura() { | ||
+ | return temperaturaMaxima - temperaturaMinima; | ||
+ | } | ||
+ | } | ||
</ | </ | ||
Line 424: | Line 471: | ||
</ | </ | ||
- | ===== Sentencias básicas | + | ===== Code snippets |
==== Mostrar un mensaje en pantalla ==== | ==== Mostrar un mensaje en pantalla ==== | ||
Line 454: | Line 501: | ||
Scanner teclado = new Scanner(System.in); | Scanner teclado = new Scanner(System.in); | ||
String entrada = teclado.nextLine(); | String entrada = teclado.nextLine(); | ||
+ | </ | ||
+ | |||
+ | ==== Reproducir un fichero de audio ==== | ||
+ | |||
+ | <code java> | ||
+ | try { | ||
+ | File f = new File(" | ||
+ | AudioInputStream audioIn = AudioSystem.getAudioInputStream(f.toURI().toURL()); | ||
+ | Clip clip = AudioSystem.getClip(); | ||
+ | clip.open(audioIn); | ||
+ | clip.start(); | ||
+ | } catch (IOException | UnsupportedAudioFileException | LineUnavailableException ioe) { | ||
+ | ioe.printStackTrace(); | ||
+ | } | ||
</ | </ | ||
Line 459: | Line 520: | ||
====== Ejercicios ====== | ====== Ejercicios ====== | ||
+ | |||
+ | {{ ejercicio.png}} | ||
- Haz un programa que solicite dos números y diga si son o no iguales | - Haz un programa que solicite dos números y diga si son o no iguales |
apuntes/introduccion.1685877825.txt.gz · Last modified: 2023/06/04 11:23 by Santiago Faci