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
Compare commits
2 Commits
efa8fb3abc
...
38fe9675d3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
38fe9675d3 | ||
|
|
f8035afcb7 |
@@ -149,6 +149,19 @@ public class UtilsArrays {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* @param m
|
||||
*/
|
||||
public static void imprimeMatriz (char m[][]) {
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
System.out.print(m[i][j] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void ordenaArray (int a[], boolean asc) {
|
||||
boolean hayIntercambios;
|
||||
do {
|
||||
|
||||
178
src/tutorialJava/capitulo5/independenceDay/CampoBatalla.java
Normal file
178
src/tutorialJava/capitulo5/independenceDay/CampoBatalla.java
Normal file
@@ -0,0 +1,178 @@
|
||||
package tutorialJava.capitulo5.independenceDay;
|
||||
|
||||
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 mezclaHumanos () {
|
||||
for (int i = 0; i < humanos.length; i++) {
|
||||
int ind1 = Utils.obtenerNumeroAzar(0, humanos.length - 1);
|
||||
int ind2 = Utils.obtenerNumeroAzar(0, humanos.length - 1);
|
||||
Humano aux = humanos[ind1];
|
||||
humanos[ind1] = humanos[ind2];
|
||||
humanos[ind2] = aux;
|
||||
}
|
||||
}
|
||||
|
||||
public void mezclaMalvados () {
|
||||
for (int i = 0; i < malvados.length; i++) {
|
||||
int ind1 = Utils.obtenerNumeroAzar(0, malvados.length - 1);
|
||||
int ind2 = Utils.obtenerNumeroAzar(0, malvados.length - 1);
|
||||
Malvado aux = malvados[ind1];
|
||||
malvados[ind1] = malvados[ind2];
|
||||
malvados[ind2] = aux;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void batalla () {
|
||||
do {
|
||||
// Disparo sobre el primer malvado vivo
|
||||
Malvado m = getPrimerMalvadoVivo();
|
||||
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 (getPrimerMalvadoVivo() != null) {
|
||||
// Disparo sobre el primer humano vivo
|
||||
Humano h = getPrimerHumanoVivo();
|
||||
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(getPrimerHumanoVivo() != null &&
|
||||
getPrimerMalvadoVivo() != null);
|
||||
|
||||
// Si llego hasta aquí, un bando no tiene soldados vivos
|
||||
if (getPrimerHumanoVivo() != null) {
|
||||
System.out.println("Han ganado los humanos");
|
||||
}
|
||||
else {
|
||||
System.out.println("Han ganado los malvados");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Humano getPrimerHumanoVivo() {
|
||||
for (int i = 0; i < humanos.length; i++) {
|
||||
if (humanos[i].isVivo()) {
|
||||
return humanos[i];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Malvado getPrimerMalvadoVivo() {
|
||||
for (int i = 0; i < malvados.length; i++) {
|
||||
if (malvados[i].isVivo()) {
|
||||
return malvados[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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
62
src/tutorialJava/capitulo5/independenceDay/Humano.java
Normal file
62
src/tutorialJava/capitulo5/independenceDay/Humano.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package tutorialJava.capitulo5.independenceDay;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Humano {
|
||||
|
||||
private int puntosVida;
|
||||
private String nombre;
|
||||
private boolean vivo;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public Humano() {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param puntosVida
|
||||
* @param nombre
|
||||
* @param vivo
|
||||
*/
|
||||
public Humano(String nombre) {
|
||||
puntosVida = Utils.obtenerNumeroAzar(50, 100);
|
||||
this.nombre = nombre;
|
||||
vivo = true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Humano [puntosVida=" + puntosVida + ", nombre=" + nombre + ", vivo=" + vivo + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
53
src/tutorialJava/capitulo5/independenceDay/Malvado.java
Normal file
53
src/tutorialJava/capitulo5/independenceDay/Malvado.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package tutorialJava.capitulo5.independenceDay;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Malvado {
|
||||
|
||||
private int puntosVida;
|
||||
private String nombre;
|
||||
private boolean vivo;
|
||||
|
||||
public Malvado() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Malvado(String nombre) {
|
||||
this.puntosVida = Utils.obtenerNumeroAzar(50, 100);
|
||||
this.nombre = nombre;
|
||||
this.vivo = true;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "Malvador [puntosVida=" + puntosVida + ", nombre=" + nombre + ", vivo=" + vivo + "]";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
25
src/tutorialJava/capitulo5/independenceDay/Principal.java
Normal file
25
src/tutorialJava/capitulo5/independenceDay/Principal.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package tutorialJava.capitulo5.independenceDay;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
|
||||
public class Principal {
|
||||
|
||||
public static void main(String[] args) {
|
||||
CampoBatalla campo = new CampoBatalla("Rute");
|
||||
|
||||
campo.mezclaHumanos();
|
||||
campo.mezclaMalvados();
|
||||
System.out.println(campo.toString());
|
||||
|
||||
campo.batalla();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package tutorialJava.examenes.examen20241122;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class EjA_InvertirOrdenArray {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int a[] = new int[10];
|
||||
UtilsArrays.inicializaArray(a, -100, 100);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
invierteArray(a);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
}
|
||||
|
||||
|
||||
public static void invierteArray (int a[]) {
|
||||
for (int i = 0; i < a.length / 2; i++) {
|
||||
int aux = a[i];
|
||||
a[i] = a[a.length - 1 - i];
|
||||
a[a.length - 1 - i] = aux;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package tutorialJava.examenes.examen20241122;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class EjB_GenerarMatrizDesdeArrayString {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String p[] = new String[] {"hola", "adios", "rojo", "azul"};
|
||||
|
||||
char m[][] = new char[p.length][maximaLongitdPalabraEnArray(p)];
|
||||
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
for (int j = 0; j < m[i].length; j++) {
|
||||
if (j < p[i].length()) {
|
||||
m[i][j] = p[i].charAt(j);
|
||||
}
|
||||
else {
|
||||
m[i][j] = '_';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UtilsArrays.imprimeMatriz(m);
|
||||
}
|
||||
|
||||
|
||||
public static int maximaLongitdPalabraEnArray(String a[]) {
|
||||
int mayor = a[0].length();
|
||||
for (int i = 1; i < a.length; i++) {
|
||||
if (a[i].length() > mayor) {
|
||||
mayor = a[i].length();
|
||||
}
|
||||
}
|
||||
return mayor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package tutorialJava.examenes.examen20241122;
|
||||
|
||||
import tutorialJava.UtilsArrays;
|
||||
|
||||
public class EjC_OrdenarArrayPorCantidadDivisores {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int a[] = new int[10];
|
||||
UtilsArrays.inicializaArray(a, 0, 100);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
ordenaBurbuja(a);
|
||||
UtilsArrays.imprimeArray(a);
|
||||
}
|
||||
|
||||
|
||||
public static void ordenaBurbuja(int a[]) {
|
||||
boolean hayIntercambios = false;
|
||||
do {
|
||||
hayIntercambios = false;
|
||||
for (int i = 0; i < a.length - 1; i++) {
|
||||
if (cantidadDivisores(a[i]) > cantidadDivisores(a[i + 1])) {
|
||||
int aux = a[i];
|
||||
a[i] = a[i + 1];
|
||||
a[i + 1] = aux;
|
||||
hayIntercambios = true;
|
||||
}
|
||||
}
|
||||
} while (hayIntercambios == true);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param n
|
||||
* @return
|
||||
*/
|
||||
public static int cantidadDivisores(int n) {
|
||||
int contDivisores = 0;
|
||||
for (int i = 2; i < n; i++) {
|
||||
if (n % i == 0) {
|
||||
contDivisores++;
|
||||
}
|
||||
}
|
||||
return contDivisores;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package tutorialJava.examenes.examen20241122;
|
||||
|
||||
public class EjD_StringDeCamelCaseASnakeCase {
|
||||
|
||||
public static void main(String[] args) {
|
||||
String str = "estoEsUnEjemploDeNombreDeVariable";
|
||||
|
||||
System.out.println("snake_case: " + camelCaseASnakeCase(str));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param strCamelCase
|
||||
* @return
|
||||
*/
|
||||
public static String camelCaseASnakeCase(String strCamelCase) {
|
||||
String strSnakeCase = "";
|
||||
|
||||
for (int i = 0; i < strCamelCase.length(); i++) {
|
||||
if (strCamelCase.charAt(i) >= 'A' &&
|
||||
strCamelCase.charAt(i) <= 'Z') {
|
||||
strSnakeCase += "_" +
|
||||
((char) (strCamelCase.charAt(i) + 32));
|
||||
}
|
||||
else {
|
||||
strSnakeCase += strCamelCase.charAt(i);
|
||||
}
|
||||
}
|
||||
return strSnakeCase;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user