Skip to content

Commit aeae394

Browse files
Polimorfismo y clase Math
1 parent d46ce40 commit aeae394

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

4.poo/polimorfismo/Main.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package poo.polimorfismo;
2+
3+
import poo.polimorfismo.domain.Empleado;
4+
import poo.polimorfismo.domain.Gerente;
5+
6+
7+
public class Main {
8+
public static void main(String[] args) {
9+
Empleado empleado = new Empleado("Pedro", 7000);
10+
// soutv
11+
// System.out.println("empleado = " + empleado.obtenerDetalles());
12+
imprimir(empleado);
13+
14+
// Empleado gerente = new Gerente("Catalina", 10000, "Contabilidad");
15+
Gerente gerente = new Gerente("Catalina", 10000, "Contabilidad");
16+
// System.out.println("gerente = " + gerente.obtenerDetalles());
17+
imprimir(gerente);
18+
}
19+
// Polimorfismo (muchas formas)
20+
public static void imprimir(Empleado empl ) {
21+
System.out.println("empleado = " + empl.obtenerDetalles());
22+
}
23+
}
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package poo.polimorfismo.domain;
2+
3+
public class Empleado {
4+
5+
protected String nombre;
6+
protected double sueldo;
7+
8+
public Empleado(String nombre, double sueldo) {
9+
this.nombre = nombre;
10+
this.sueldo = sueldo;
11+
}
12+
13+
public String getNombre() {
14+
return nombre;
15+
}
16+
17+
public void setNombre(String nombre) {
18+
this.nombre = nombre;
19+
}
20+
21+
public double getSueldo() {
22+
return sueldo;
23+
}
24+
25+
public void setSueldo(double sueldo) {
26+
this.sueldo = sueldo;
27+
}
28+
29+
public String obtenerDetalles() {
30+
return "Nombre: " + this.nombre + " , sueldo: " + this.sueldo;
31+
}
32+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package poo.polimorfismo.domain;
2+
3+
public class Gerente extends Empleado {
4+
5+
private String departamento;
6+
7+
public Gerente(String nombre, double sueldo, String departamento) {
8+
super(nombre, sueldo);
9+
this.departamento = departamento;
10+
}
11+
12+
@Override
13+
public String obtenerDetalles() {
14+
// System.out.println(super.obtenerDetalles());
15+
return super.obtenerDetalles() + ", departamento: " + this.departamento;
16+
// return "Nombre: " + this.nombre + " , sueldo: " + this.sueldo + ", departamento: " + this.departamento;
17+
}
18+
}

7.clasemath/math/ClaseMath.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package clasemath.math;
2+
3+
public class ClaseMath {
4+
public static void main(String[] args) {
5+
String [] colores = {"azul", "amarillo", "rojo", "verde", "blanco", "negro"};
6+
System.out.println(colores.length);
7+
double random = Math.random(); // genera un número aleatorio decimal del 0 al 1, sin incluir 1
8+
System.out.println("random = " + random);
9+
double nuevoRandom = random * colores.length;
10+
// System.out.println("nuevoRandom = " + nuevoRandom);
11+
random = random * colores.length;
12+
// random *= colores.length; // random = random * colores.length;
13+
System.out.println("random = " + random);
14+
15+
random = Math.floor(random);
16+
System.out.println("random = " + random);
17+
System.out.println("random = " + (int)random);
18+
19+
// para acceder al arreglo
20+
System.out.println("colores = " + colores[(int)random]);
21+
22+
}
23+
}

7.clasemath/math/ClaseRandom.java

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package clasemath.math;
2+
3+
import java.util.Random;
4+
5+
public class ClaseRandom {
6+
public static void main(String[] args) {
7+
8+
String [] colores = {"azul", "amarillo", "rojo", "verde", "blanco", "negro"}; // longitud 6 y su ultimo indice 5
9+
Random randomObj = new Random();
10+
11+
int randomInt = randomObj.nextInt();
12+
System.out.println("randomInt = " + randomInt);
13+
14+
// vamos a generar valores del 0 al 6, pero! sin incluir el 6
15+
// 0, 1, 2, 3, 4 o 5
16+
int randomIntParams = randomObj.nextInt(colores.length);
17+
System.out.println("randomIntParams = " + randomIntParams);
18+
// Obtener un color aleatorio de nuestro array colores
19+
System.out.println("random color: " + colores[randomIntParams]);
20+
21+
// Obtener valores aleatorios entre 15 y 25
22+
int randomIntRango = 15 + randomObj.nextInt(25-15);
23+
System.out.println("randomIntRango = " + randomIntRango);
24+
25+
26+
}
27+
}

0 commit comments

Comments
 (0)