Ejercicio repaso métodos
This commit is contained in:
1 parent
a2e6a6c8a0
commit
e190552230
1 file changed
+137
@@ -0,0 +1,137 @@
|
|||||||
|
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;
|
||||||
|
int[] lados;
|
||||||
|
switch (figura) {
|
||||||
|
case 1: // Cuadrado
|
||||||
|
System.out.print("Longitud del lado: ");
|
||||||
|
int lado = leerEnteroPositivo();
|
||||||
|
lados = new int[4];
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
lados[i] = lado;
|
||||||
|
}
|
||||||
|
resultado =
|
||||||
|
opt == 1 ? areaCuadrado(lado) : perimetroPoligono(lados);
|
||||||
|
break;
|
||||||
|
case 2: // Triangulo
|
||||||
|
lados = new int[3];
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
System.out.printf("Longitud del lado %d: ", i + 1);
|
||||||
|
lados[i] = leerEnteroPositivo();
|
||||||
|
}
|
||||||
|
resultado =
|
||||||
|
opt == 1 ? areaTriangulo(lados) : perimetroPoligono(lados);
|
||||||
|
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 cualquier polígono
|
||||||
|
private static double perimetroPoligono(int[] lados) {
|
||||||
|
double resultado = 0.0;
|
||||||
|
for (int l : lados) resultado += l;
|
||||||
|
return resultado;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Area cuadrado
|
||||||
|
private static double areaCuadrado(int lado) {
|
||||||
|
return lado * lado;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Area del triangulo (formula de Herón)
|
||||||
|
private static double areaTriangulo(int[] lados) {
|
||||||
|
if (lados.length != 3) {
|
||||||
|
System.out.println("El triangulo tiene que tener 3 lados");
|
||||||
|
return -1.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double semiperimetro = perimetroPoligono(lados) / 2.0;
|
||||||
|
double resultado = semiperimetro;
|
||||||
|
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
resultado *= semiperimetro - lados[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
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