Compare commits
4
Commits
bf91a20bee
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f7e450a26 | ||
|
|
dc0a3aacb3 | ||
|
|
81eac7a894 | ||
|
|
e190552230 |
No files matched your search
@@ -0,0 +1,30 @@
|
|||||||
|
public class Constructores1 {
|
||||||
|
public static class Semaforo {
|
||||||
|
public static enum Estados{
|
||||||
|
Rojo,
|
||||||
|
Ambar,
|
||||||
|
Verde,
|
||||||
|
}
|
||||||
|
private Estados estado;
|
||||||
|
Semaforo(Estados estado){
|
||||||
|
this.estado = estado;
|
||||||
|
}
|
||||||
|
public void imprimirEstado(){
|
||||||
|
System.out.printf("El semaforo está en %s\n", this.getEstado());
|
||||||
|
}
|
||||||
|
public Estados getEstado(){
|
||||||
|
return this.estado;
|
||||||
|
}
|
||||||
|
public void setEstado(Estados estado){
|
||||||
|
this.estado = estado;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Semaforo semaforo = new Semaforo(Semaforo.Estados.Rojo);
|
||||||
|
semaforo.imprimirEstado();
|
||||||
|
semaforo.setEstado(Semaforo.Estados.Ambar);
|
||||||
|
semaforo.imprimirEstado();
|
||||||
|
semaforo.setEstado(Semaforo.Estados.Verde);
|
||||||
|
semaforo.imprimirEstado();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class ParqueAtracciones {
|
||||||
|
|
||||||
|
private static int totalEntradas = 0;
|
||||||
|
private static int totalAdultos = 0;
|
||||||
|
private static int totalNinos = 0;
|
||||||
|
private static int recaudacion = 0;
|
||||||
|
private static int totalDescuentos = 0;
|
||||||
|
|
||||||
|
private static Scanner sc = new Scanner(System.in);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
boolean salir = false;
|
||||||
|
while (!salir) {
|
||||||
|
switch (menuPrincipal()) {
|
||||||
|
case vender:
|
||||||
|
break;
|
||||||
|
case recaudacion:
|
||||||
|
break;
|
||||||
|
case estadisticas:
|
||||||
|
break;
|
||||||
|
case resumen:
|
||||||
|
break;
|
||||||
|
case salir:
|
||||||
|
salir = true;
|
||||||
|
break;
|
||||||
|
case invalida:
|
||||||
|
System.out.println("Opción inválida, intentalo de nuevo");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static enum OpcionesMenu {
|
||||||
|
vender,
|
||||||
|
recaudacion,
|
||||||
|
estadisticas,
|
||||||
|
resumen,
|
||||||
|
salir,
|
||||||
|
invalida,
|
||||||
|
}
|
||||||
|
|
||||||
|
private static OpcionesMenu menuPrincipal() {
|
||||||
|
System.out.print(
|
||||||
|
"========== PARQUE DE ATRACCIONES ==========\n" +
|
||||||
|
"1. Vender entrada\n" +
|
||||||
|
"2. Consultar recaudación\n" +
|
||||||
|
"3. Consultar estadísticas\n" +
|
||||||
|
"4. Mostrar resumen del día\n" +
|
||||||
|
"5. Salir\n" +
|
||||||
|
"Elige una opción: "
|
||||||
|
);
|
||||||
|
int opt = leerEnteroPositivo();
|
||||||
|
return switch (opt) {
|
||||||
|
case 1 -> OpcionesMenu.vender;
|
||||||
|
case 2 -> OpcionesMenu.recaudacion;
|
||||||
|
case 3 -> OpcionesMenu.estadisticas;
|
||||||
|
case 4 -> OpcionesMenu.resumen;
|
||||||
|
case 5 -> OpcionesMenu.salir;
|
||||||
|
default -> OpcionesMenu.invalida;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int leerEnteroPositivo() {
|
||||||
|
int resultado = 0;
|
||||||
|
|
||||||
|
do {
|
||||||
|
try {
|
||||||
|
resultado = sc.nextInt();
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.printf(
|
||||||
|
"Valor incorrecto, debe ser un número natural (%s)\n",
|
||||||
|
e
|
||||||
|
);
|
||||||
|
sc.next();
|
||||||
|
}
|
||||||
|
if (resultado < 0) System.out.println(
|
||||||
|
"Debe ser un numero positivo"
|
||||||
|
);
|
||||||
|
} while (resultado < 0);
|
||||||
|
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class RepasoMetodos {
|
||||||
|
|
||||||
|
static Scanner sc;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
sc = new Scanner(System.in);
|
||||||
|
int opt = mostrarMenu();
|
||||||
|
int figura = seleccionarFigura();
|
||||||
|
double resultado = -1.0;
|
||||||
|
switch (figura) {
|
||||||
|
case 1: // Cuadrado
|
||||||
|
System.out.print("Longitud del lado: ");
|
||||||
|
int lado = leerEnteroPositivo();
|
||||||
|
resultado =
|
||||||
|
opt == 1 ? areaCuadrado(lado) : perimetroCuadrado(lado);
|
||||||
|
break;
|
||||||
|
case 2: // Triangulo
|
||||||
|
int l1, l2, l3;
|
||||||
|
System.out.printf("Longitud del lado 1: ");
|
||||||
|
l1 = leerEnteroPositivo();
|
||||||
|
System.out.printf("Longitud del lado 2: ");
|
||||||
|
l2 = leerEnteroPositivo();
|
||||||
|
System.out.printf("Longitud del lado 3: ");
|
||||||
|
l3 = leerEnteroPositivo();
|
||||||
|
resultado =
|
||||||
|
opt == 1
|
||||||
|
? areaTriangulo(l1, l2, l3)
|
||||||
|
: perimetroTriangulo(l1, l2, l3);
|
||||||
|
break;
|
||||||
|
case 3: // Circulo
|
||||||
|
System.out.print("Radio del círculo: ");
|
||||||
|
int radio = leerEnteroPositivo();
|
||||||
|
resultado =
|
||||||
|
opt == 1 ? areaCirculo(radio) : perimetroCirculo(radio);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
System.out.printf("El resultado es %.2f\n", resultado);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Área del círculo (PI * r^2)
|
||||||
|
private static double areaCirculo(int radio) {
|
||||||
|
return Math.PI * (radio * radio);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perímetro del círculo (2 * PI * r)
|
||||||
|
private static double perimetroCirculo(int radio) {
|
||||||
|
return 2 * Math.PI * radio;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perímetro de triangulo
|
||||||
|
private static double perimetroTriangulo(double l1, double l2, double l3) {
|
||||||
|
return l1 + l2 + l3;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Perímetro de cuadrado
|
||||||
|
private static double perimetroCuadrado(double lado) {
|
||||||
|
return lado * 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Area cuadrado
|
||||||
|
private static double areaCuadrado(int lado) {
|
||||||
|
return lado * lado;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Area del triangulo (formula de Herón)
|
||||||
|
private static double areaTriangulo(double l1, double l2, double l3) {
|
||||||
|
double semiperimetro = perimetroTriangulo(l1, l2, l3) / 2.0;
|
||||||
|
double resultado = semiperimetro;
|
||||||
|
|
||||||
|
resultado *= semiperimetro - l1;
|
||||||
|
resultado *= semiperimetro - l2;
|
||||||
|
resultado *= semiperimetro - l3;
|
||||||
|
|
||||||
|
resultado = Math.sqrt(resultado);
|
||||||
|
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int seleccionarFigura() {
|
||||||
|
System.out.println(
|
||||||
|
"Selecciona una figura:\n" +
|
||||||
|
"1. Cuadrado\n" +
|
||||||
|
"2. Triángulo\n" +
|
||||||
|
"3. Círculo"
|
||||||
|
);
|
||||||
|
int resultado = -1;
|
||||||
|
boolean parar = false;
|
||||||
|
while (!parar) {
|
||||||
|
System.out.print("Elige una opción: ");
|
||||||
|
resultado = leerEnteroPositivo();
|
||||||
|
if (resultado <= 3) parar = true;
|
||||||
|
else System.out.printf("%d no es una figura valida\n", resultado);
|
||||||
|
}
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int mostrarMenu() {
|
||||||
|
System.out.print(
|
||||||
|
"¿Qué desea calcular?\n" + "1. Área\n" + "2. Perímetro\n"
|
||||||
|
);
|
||||||
|
boolean salir = false;
|
||||||
|
int resultado = -1;
|
||||||
|
while (!salir) {
|
||||||
|
System.out.print("Elige una opción: ");
|
||||||
|
resultado = leerEnteroPositivo();
|
||||||
|
if (resultado <= 2) salir = true;
|
||||||
|
else System.out.println("Debe ser 1 o 2");
|
||||||
|
}
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int leerEnteroPositivo() {
|
||||||
|
int resultado = -1;
|
||||||
|
boolean parar = false;
|
||||||
|
while (!parar) {
|
||||||
|
try {
|
||||||
|
resultado = sc.nextInt();
|
||||||
|
if (resultado > 0) {
|
||||||
|
parar = true;
|
||||||
|
} else {
|
||||||
|
System.out.println("Debe ser un número entero positivo");
|
||||||
|
}
|
||||||
|
} catch (InputMismatchException e) {
|
||||||
|
System.out.println("No es un número entero válido");
|
||||||
|
sc.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user