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 & 6): examples added
This commit is contained in:
141
src/tutorialJava/examenes/examen20241213/Principal.java
Normal file
141
src/tutorialJava/examenes/examen20241213/Principal.java
Normal file
@@ -0,0 +1,141 @@
|
||||
package tutorialJava.examenes.examen20241213;
|
||||
|
||||
import tutorialJava.Utils;
|
||||
import tutorialJava.examenes.examen20241211.Producto;
|
||||
|
||||
public class Principal {
|
||||
|
||||
private static Animal[] animales = new Animal[Utils.obtenerNumeroAzar(10, 20)];
|
||||
|
||||
/**
|
||||
*
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
|
||||
// Inicializo objetos en Array
|
||||
for (int i = 0; i < animales.length; i++) {
|
||||
if (i < (animales.length / 2)) {
|
||||
animales[i] = new Mamifero("Mamífero_" + i);
|
||||
}
|
||||
else {
|
||||
animales[i] = new Ave("Ave_" + i);
|
||||
}
|
||||
}
|
||||
|
||||
muestraAnimales();
|
||||
ordenaBurbuja(animales);
|
||||
System.out.println("\nAnimales ordenados");
|
||||
muestraAnimales();
|
||||
|
||||
System.out.println("\nEspacios necsarios");
|
||||
System.out.println("Total espacio necesario: " + calcularEspacioTotal());
|
||||
System.out.println("Total espacio mamíferos carnívoros: " +
|
||||
calcularEspacioMamiferosCarnivoros());
|
||||
|
||||
System.out.println("\nEstadísticas");
|
||||
System.out.println("Animal que más ocupa: " + animales[0].toString());
|
||||
System.out.println("Animal que menos ocupa: " +
|
||||
animales[animales.length - 1].toString());
|
||||
System.out.println("Total mamíferos herbívoros: " + contadorMamiferosHerbivoros());
|
||||
System.out.println("Total aves gran envergadura: " +
|
||||
contadorAvesGranEnvergadura());
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static void muestraAnimales() {
|
||||
for (int i = 0; i < animales.length; i++) {
|
||||
System.out.println(animales[i].toString());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static float calcularEspacioTotal() {
|
||||
float espacio = 0;
|
||||
for (int i = 0; i < animales.length; i++) {
|
||||
espacio += animales[i].calcularEspacio();
|
||||
}
|
||||
return espacio;
|
||||
}
|
||||
|
||||
|
||||
private static float calcularEspacioMamiferosCarnivoros() {
|
||||
float espacio = 0;
|
||||
for (int i = 0; i < animales.length; i++) {
|
||||
if (animales[i] instanceof Mamifero) {
|
||||
Mamifero m = (Mamifero) animales[i];
|
||||
if (m.getTipoAlimentacion().equalsIgnoreCase("Carnívoro")) {
|
||||
espacio += animales[i].calcularEspacio();
|
||||
}
|
||||
}
|
||||
}
|
||||
return espacio;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static int contadorMamiferosHerbivoros() {
|
||||
int cont = 0;
|
||||
for (int i = 0; i < animales.length; i++) {
|
||||
if (animales[i] instanceof Mamifero) {
|
||||
Mamifero m = (Mamifero) animales[i];
|
||||
if (m.getTipoAlimentacion().equalsIgnoreCase("Herbívoro")) {
|
||||
cont++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cont;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
private static int contadorAvesGranEnvergadura() {
|
||||
int cont = 0;
|
||||
for (int i = 0; i < animales.length; i++) {
|
||||
if (animales[i] instanceof Ave) {
|
||||
Ave a = (Ave) animales[i];
|
||||
if (a.getEnvergaduraAlas() > 100) {
|
||||
cont++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return cont;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param a
|
||||
*/
|
||||
public static void ordenaBurbuja(Animal a[]) {
|
||||
boolean hayIntercambios = false;
|
||||
do {
|
||||
hayIntercambios = false;
|
||||
for (int i = 0; i < a.length - 1; i++) {
|
||||
if (a[i].calcularEspacio() < a[i + 1].calcularEspacio()) {
|
||||
Animal aux = a[i];
|
||||
a[i] = a[i + 1];
|
||||
a[i + 1] = aux;
|
||||
hayIntercambios = true;
|
||||
}
|
||||
}
|
||||
} while (hayIntercambios == true);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user