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 5): example & ex added
This commit is contained in:
@@ -53,6 +53,30 @@ public class Utils {
|
|||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Pide un número entero al usuario
|
||||||
|
* @param mensaje String que va a mostrarse en consola para pedir el número
|
||||||
|
* @return Número entero introducido por el usuario
|
||||||
|
*/
|
||||||
|
public static float pideNumeroFloat (String mensaje) {
|
||||||
|
float n = 0;
|
||||||
|
boolean esNumeroCorrecto;
|
||||||
|
Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
do {
|
||||||
|
esNumeroCorrecto = false;
|
||||||
|
try {
|
||||||
|
System.out.println(mensaje);
|
||||||
|
n = Float.parseFloat(sc.nextLine());
|
||||||
|
esNumeroCorrecto = true;
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
System.out.println("No ha introducido un número");
|
||||||
|
}
|
||||||
|
} while (esNumeroCorrecto == false);
|
||||||
|
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ public class Ejercicio02_findAndReplace {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static void findAndReplace (int b[], int buscado, int reemplazo) {
|
public static void findAndReplace (int b[], int buscado, int reemplazo) {
|
||||||
StringBuffer sb = new StringBuffer();
|
|
||||||
|
|
||||||
for (int i = 0; i < b.length; i++) {
|
for (int i = 0; i < b.length; i++) {
|
||||||
if (b[i] == buscado) {
|
if (b[i] == buscado) {
|
||||||
|
|||||||
@@ -4,13 +4,14 @@ public class Ejercicio04_MatrizConBordeYCeroAlAzar {
|
|||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// Declaración de variables
|
// Declaración de variables
|
||||||
char matriz[][] = new char[20][10];
|
char matriz[][] = new char[10][20];
|
||||||
int posicionAzarFila, posicionAzarColumna;
|
int posicionAzarFila, posicionAzarColumna;
|
||||||
|
|
||||||
// La relleno de espacios en blanco
|
// La relleno de espacios en blanco
|
||||||
for (int i = 0; i < matriz.length; i++) {
|
for (int i = 0; i < matriz.length; i++) {
|
||||||
for (int j = 0; j < matriz[i].length; j++) {
|
for (int j = 0; j < matriz[i].length; j++) {
|
||||||
if (i == 0 || i == (matriz.length - 1) || j == 0 || j == (matriz[0].length - 1)) {
|
if (i == 0 || i == (matriz.length - 1) ||
|
||||||
|
j == 0 || j == (matriz[0].length - 1)) {
|
||||||
matriz[i][j] = '*';
|
matriz[i][j] = '*';
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package tutorialJava.capitulo5.ejercicios.bloque02_ColeccionAntiguedades;
|
||||||
|
|
||||||
|
public class Antiguedad {
|
||||||
|
|
||||||
|
private int anio;
|
||||||
|
private String origen;
|
||||||
|
private float precio;
|
||||||
|
|
||||||
|
public Antiguedad() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Antiguedad(int anio, String origen, float precio) {
|
||||||
|
super();
|
||||||
|
this.anio = anio;
|
||||||
|
this.origen = origen;
|
||||||
|
this.precio = precio;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAnio() {
|
||||||
|
return anio;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAnio(int anio) {
|
||||||
|
this.anio = anio;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getOrigen() {
|
||||||
|
return origen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrigen(String origen) {
|
||||||
|
this.origen = origen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getPrecio() {
|
||||||
|
return precio;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrecio(float precio) {
|
||||||
|
this.precio = precio;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Antiguedad [anio=" + anio + ", origen=" + origen + ", precio=" + precio + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package tutorialJava.capitulo5.ejercicios.bloque02_ColeccionAntiguedades;
|
||||||
|
|
||||||
|
public class Joya extends Antiguedad {
|
||||||
|
|
||||||
|
private String material;
|
||||||
|
|
||||||
|
public Joya() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Joya(int anio, String origen, float precio) {
|
||||||
|
super(anio, origen, precio);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMaterial() {
|
||||||
|
return material;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaterial(String material) {
|
||||||
|
this.material = material;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return super.toString() + " material: " + this.material;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package tutorialJava.capitulo5.ejercicios.bloque02_articulosComestibles;
|
||||||
|
|
||||||
|
public class ArtNoPerecedero extends Articulo {
|
||||||
|
|
||||||
|
public ArtNoPerecedero() {
|
||||||
|
// TODO Auto-generated constructor stub
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArtNoPerecedero(String codigo, String nombre, float precio) {
|
||||||
|
super(codigo, nombre, precio);
|
||||||
|
// TODO Auto-generated constructor stub
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package tutorialJava.capitulo5.ejercicios.bloque02_articulosComestibles;
|
||||||
|
|
||||||
|
public class ArtPerecedero extends Articulo {
|
||||||
|
|
||||||
|
private String fechaCaducidad;
|
||||||
|
|
||||||
|
public ArtPerecedero() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArtPerecedero(String codigo, String nombre, float precio) {
|
||||||
|
super(codigo, nombre, precio);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFechaCaducidad() {
|
||||||
|
return fechaCaducidad;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFechaCaducidad(String fechaCaducidad) {
|
||||||
|
this.fechaCaducidad = fechaCaducidad;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "ArtPerecedero [fechaCaducidad=" + fechaCaducidad + ", getCodigo()=" + getCodigo() + ", getNombre()="
|
||||||
|
+ getNombre() + ", getPrecio()=" + getPrecio() + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package tutorialJava.capitulo5.ejercicios.bloque02_articulosComestibles;
|
||||||
|
|
||||||
|
public class Articulo {
|
||||||
|
|
||||||
|
private String codigo;
|
||||||
|
private String nombre;
|
||||||
|
private float precio;
|
||||||
|
|
||||||
|
public Articulo() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Articulo(String codigo, String nombre, float precio) {
|
||||||
|
super();
|
||||||
|
this.codigo = codigo;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.precio = precio;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getCodigo() {
|
||||||
|
return codigo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCodigo(String codigo) {
|
||||||
|
this.codigo = codigo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getPrecio() {
|
||||||
|
return precio;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrecio(float precio) {
|
||||||
|
this.precio = precio;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Articulo [codigo=" + codigo + ", nombre=" + nombre + ", precio=" + precio + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package tutorialJava.capitulo5.ejercicios.bloque02_articulosComestibles;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
|
||||||
|
public class Tienda {
|
||||||
|
|
||||||
|
public Tienda() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
Articulo articulos[] = new Articulo[4];
|
||||||
|
|
||||||
|
for (int i = 0; i < articulos.length; i++) {
|
||||||
|
if (i < 2) {
|
||||||
|
System.out.println("Introduzca datos de Art perecedero: ");
|
||||||
|
ArtPerecedero artPer = new ArtPerecedero();
|
||||||
|
artPer.setCodigo(Utils.
|
||||||
|
obtenerCadenaConDescripcion("Dame código:"));
|
||||||
|
artPer.setNombre(Utils.
|
||||||
|
obtenerCadenaConDescripcion("Dame nombre:"));
|
||||||
|
artPer.setPrecio(Utils.
|
||||||
|
pideNumeroFloat("Dame precio"));
|
||||||
|
artPer.setFechaCaducidad(Utils.
|
||||||
|
obtenerCadenaConDescripcion("Dame fecha caducidad:"));
|
||||||
|
articulos[i] = artPer;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("Introduzca datos de Art perecedero: ");
|
||||||
|
ArtNoPerecedero artNoPer = new ArtNoPerecedero();
|
||||||
|
artNoPer.setCodigo(Utils.
|
||||||
|
obtenerCadenaConDescripcion("Dame código:"));
|
||||||
|
artNoPer.setNombre(Utils.
|
||||||
|
obtenerCadenaConDescripcion("Dame nombre:"));
|
||||||
|
artNoPer.setPrecio(Utils.
|
||||||
|
pideNumeroFloat("Dame precio"));
|
||||||
|
articulos[i] = artNoPer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Muestro los valores
|
||||||
|
for (int i = 0; i < articulos.length; i++) {
|
||||||
|
System.out.println(articulos[i].toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
package tutorialJava.capitulo5.independenceDayConHerencia;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
|
||||||
|
public class CampoBatalla {
|
||||||
|
|
||||||
|
private String nombre;
|
||||||
|
private Humano humanos[] = new Humano[20];
|
||||||
|
private Malvado malvados[] = new Malvado[20];
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param nombre
|
||||||
|
*/
|
||||||
|
public CampoBatalla (String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
|
||||||
|
for (int i = 0; i < humanos.length; i++) {
|
||||||
|
humanos[i] = new Humano("H-" + i);
|
||||||
|
}
|
||||||
|
// humanos[humanos.length - 1].setPuntosVida(
|
||||||
|
// humanos[humanos.length - 1].getPuntosVida() * 2);
|
||||||
|
|
||||||
|
Humano ultimoHumano = humanos[humanos.length - 1];
|
||||||
|
ultimoHumano.setPuntosVida(ultimoHumano.getPuntosVida() * 2);
|
||||||
|
|
||||||
|
for (int i = 0; i < malvados.length; i++) {
|
||||||
|
malvados[i] = new Malvado("M-" + i);
|
||||||
|
}
|
||||||
|
// Establezco el doble de puntos de vida al último malvado
|
||||||
|
Malvado ultimoMalvado = malvados[malvados.length - 1];
|
||||||
|
ultimoMalvado.setPuntosVida(ultimoMalvado.getPuntosVida() * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void mezclaPersonajes (Personaje a[]) {
|
||||||
|
for (int i = 0; i < a.length; i++) {
|
||||||
|
int ind1 = Utils.obtenerNumeroAzar(0, a.length - 1);
|
||||||
|
int ind2 = Utils.obtenerNumeroAzar(0, a.length - 1);
|
||||||
|
Personaje aux = a[ind1];
|
||||||
|
a[ind1] = a[ind2];
|
||||||
|
a[ind2] = aux;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void batalla () {
|
||||||
|
do {
|
||||||
|
// Disparo sobre el primer malvado vivo
|
||||||
|
Malvado m = (Malvado) getPrimerPersonajeVivo(malvados);
|
||||||
|
m.setPuntosVida(m.getPuntosVida() -
|
||||||
|
Utils.obtenerNumeroAzar(5, 25));
|
||||||
|
if (m.getPuntosVida() <= 0) {
|
||||||
|
m.setVivo(false);
|
||||||
|
m.setPuntosVida(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sólo si queda algún malvado vivo, disparo sobre humano
|
||||||
|
if (getPrimerPersonajeVivo(malvados) != null) {
|
||||||
|
// Disparo sobre el primer humano vivo
|
||||||
|
Humano h = (Humano) getPrimerPersonajeVivo(humanos);
|
||||||
|
h.setPuntosVida(h.getPuntosVida() -
|
||||||
|
Utils.obtenerNumeroAzar(5, 25));
|
||||||
|
if (h.getPuntosVida() <= 0) {
|
||||||
|
h.setVivo(false);
|
||||||
|
h.setPuntosVida(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Imprimo campo de batalla
|
||||||
|
System.out.println(toString());
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
} while(getPrimerPersonajeVivo(humanos) != null &&
|
||||||
|
getPrimerPersonajeVivo(malvados) != null);
|
||||||
|
|
||||||
|
// Si llego hasta aquí, un bando no tiene soldados vivos
|
||||||
|
if (getPrimerPersonajeVivo(humanos) != null) {
|
||||||
|
System.out.println("Han ganado los humanos");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
System.out.println("Han ganado los malvados");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Personaje getPrimerPersonajeVivo(Personaje a[]) {
|
||||||
|
for (int i = 0; i < a.length; i++) {
|
||||||
|
if (a[i].isVivo()) {
|
||||||
|
return a[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Humano[] getHumanos() {
|
||||||
|
return humanos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHumanos(Humano[] humanos) {
|
||||||
|
this.humanos = humanos;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Malvado[] getMalvados() {
|
||||||
|
return malvados;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMalvados(Malvado[] malvados) {
|
||||||
|
this.malvados = malvados;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toString() {
|
||||||
|
StringBuffer sb = new StringBuffer("Campo: " + this.nombre);
|
||||||
|
sb.append("\nHumanos: ");
|
||||||
|
for (int i = 0; i < humanos.length; i++) {
|
||||||
|
if (humanos[i].isVivo()) {
|
||||||
|
sb.append(humanos[i].getNombre() + ":" +
|
||||||
|
humanos[i].getPuntosVida() + ":" +
|
||||||
|
humanos[i].isVivo() + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sb.append("\nMalvados: ");
|
||||||
|
for (int i = 0; i < malvados.length; i++) {
|
||||||
|
if (malvados[i].isVivo()) {
|
||||||
|
sb.append(malvados[i].getNombre() + ":" +
|
||||||
|
malvados[i].getPuntosVida() + ":" +
|
||||||
|
malvados[i].isVivo() + " ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package tutorialJava.capitulo5.independenceDayConHerencia;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
|
||||||
|
public class Humano extends Personaje {
|
||||||
|
|
||||||
|
public int altura;
|
||||||
|
|
||||||
|
public Humano() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Humano(String nombre) {
|
||||||
|
super(nombre);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
package tutorialJava.capitulo5.independenceDayConHerencia;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
|
||||||
|
public class Personaje {
|
||||||
|
|
||||||
|
private int puntosVida;
|
||||||
|
private String nombre;
|
||||||
|
private boolean vivo;
|
||||||
|
|
||||||
|
public Personaje() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Personaje(String nombre) {
|
||||||
|
super();
|
||||||
|
this.puntosVida = Utils.obtenerNumeroAzar(50, 100);
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.vivo = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public Personaje(int puntosVida, String nombre, boolean vivo) {
|
||||||
|
super();
|
||||||
|
this.puntosVida = puntosVida;
|
||||||
|
this.nombre = nombre;
|
||||||
|
this.vivo = vivo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getPuntosVida() {
|
||||||
|
return puntosVida;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPuntosVida(int puntosVida) {
|
||||||
|
this.puntosVida = puntosVida;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNombre() {
|
||||||
|
return nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNombre(String nombre) {
|
||||||
|
this.nombre = nombre;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isVivo() {
|
||||||
|
return vivo;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVivo(boolean vivo) {
|
||||||
|
this.vivo = vivo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Personaje [puntosVida=" + puntosVida + ", nombre=" + nombre + ", vivo=" + vivo + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package tutorialJava.capitulo5.independenceDayConHerencia;
|
||||||
|
|
||||||
|
import tutorialJava.Utils;
|
||||||
|
|
||||||
|
public class Principal {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CampoBatalla campo = new CampoBatalla("Rute");
|
||||||
|
|
||||||
|
campo.mezclaPersonajes(campo.getHumanos());
|
||||||
|
campo.mezclaPersonajes(campo.getMalvados());
|
||||||
|
System.out.println(campo.toString());
|
||||||
|
|
||||||
|
campo.batalla();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user