viernes, 11 de noviembre de 2022

Sacar areas de triangulo, cuadrado y círculo en java con ciclos

 El programas siguiente utilizan esta clase Leer para que funcione, esta clase debe estar en la misma carpeta donde este cada programa. El programas a continuación deben tener esta clase.

Crear este archivo java con el siguiente código:

import java.io.*;


public class Leer{

  public static String dato() {

    String sdato = "";

    try

    {

      // Definir un flujo de caracteres de entrada: flujoE

      InputStreamReader isr = new InputStreamReader(System.in);

      BufferedReader flujoE = new BufferedReader(isr);

      // Leer. La entrada finaliza al pulsar la tecla Entrar

      sdato = flujoE.readLine();

    }

    catch(IOException e) {

      System.err.println("Error: " + e.getMessage());

    }

    return sdato; // devolver el dato tecleado

  }

  

  public static short datoShort() {

    try

    {

      return Short.parseShort(dato());

    }

    catch(NumberFormatException e) {

      return Short.MIN_VALUE; // valor más pequeño

    }

  }

  

  public static int datoInt() {

    try {

      return Integer.parseInt(dato());

    }

    catch(NumberFormatException e) {

      return Integer.MIN_VALUE; // valor más pequeño

    }

  }

  

  public static long datoLong() {

    try

    {

      return Long.parseLong(dato());

    }

    catch(NumberFormatException e) {

      return Long.MIN_VALUE; // valor más pequeño

    }

  }

  

  public static float datoFloat()

  {

    try

    {

      return Float.parseFloat(dato());

    }

    catch(NumberFormatException e)

    {

      return Float.NaN; // No es un Número; valor float.

    }

  }

  

  public static double datoDouble() {

    try {

      return Double.parseDouble(dato());

    }

    catch(NumberFormatException e) {

      return Double.NaN; // No es un Número; valor double.

    }

  }

}

_________Creamos el segundo archivo java llamado Areas_______

import java.io.PrintStream;

public class Areas {

    public static void main(String[] args)

  {

    int opcion;

    double base, altura, radio, area;

    do

    {         

     System.out.printf("Calculo de areas de figuras geometricas");

     System.out.printf("\n\t 1)AREA DEL CIRCULO");

     System.out.printf("\n\t 2)AREA DEL RECTANGULO");  

     System.out.printf("\n\t 3)AREA DEL TRIANGULO"); 

     System.out.printf("\n\n\t ¿ESCOGE EL AREA A CALCULAR? <0>Salir ");         

     opcion = Leer.datoInt();               

     if (opcion > 0 && opcion<4) {

                if (opcion==1)

        {

            System.out.printf("\n\n\t ¿VALOR DEL RADIO? ");         

            radio = Leer.datoFloat();       

                area=radio*3.1416;

                System.out.printf("\n El Area es = %.2f ",area);

                System.out.printf("\n\n"); }

                if (opcion==2) {

            System.out.printf("\n\n\t ¿VALOR DE LA BASE? ");       

            base = Leer.datoFloat();        

                System.out.printf("\n\n\t ¿VALOR DE LA ALTURA? ");              

            altura = Leer.datoFloat();

                area=base * altura;

                System.out.printf("\n El Area es = %.2f ",area);

                System.out.printf("\n\n"); }

        if (opcion==3) {

            System.out.printf("\n\n\t ¿VALOR DE LA BASE? ");       

            base = Leer.datoFloat();        

                System.out.printf("\n\n\t ¿VALOR DE LA ALTURA? ");              

            altura = Leer.datoFloat();

                area=(base * altura)/2;

                System.out.printf("\n El Area es = %.2f ",area);

        System.out.printf("\nHecho por jose ");

                                               System.out.printf("\n\n"); }                        

      }     

    }while(opcion!=0); }      

      }

--------------------Configuration: <Default>--------------------

Calculo de areas de figuras geometricas

     1)AREA DEL CIRCULO

     2)AREA DEL RECTANGULO

     3)AREA DEL TRIANGULO

     ¿ESCOGE EL AREA A CALCULAR? <0>Salir 2

     ¿VALOR DE LA BASE? 1

¿VALOR DE LA ALTURA? 1

 El Area es = 1.00






No hay comentarios:

Publicar un comentario