veronikaaa86

06. Area of Figures

May 15th, 2021
751
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package ConditionalStatements;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P06AreaOfFigures {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         String type = scanner.nextLine();
  10.  
  11.         double area = 0;
  12.         if (type.equals("square")) {
  13.             double side = Double.parseDouble(scanner.nextLine());
  14.  
  15.             area = side * side;
  16.         } else if (type.equals("rectangle")) {
  17.             double len = Double.parseDouble(scanner.nextLine());
  18.             double width = Double.parseDouble(scanner.nextLine());
  19.  
  20.             area = len * width;
  21.         } else if (type.equals("circle")) {
  22.             double r = Double.parseDouble(scanner.nextLine());
  23.  
  24.             area = Math.PI * r * r;
  25.  
  26.         } else if (type.equals("triangle")) {
  27.             double base = Double.parseDouble(scanner.nextLine());
  28.             double height = Double.parseDouble(scanner.nextLine());
  29.  
  30.             area = (base * height) / 2;
  31.         }
  32.  
  33.         System.out.printf("%.3f", area);
  34.     }
  35. }
  36.  
RAW Paste Data