Advertisement
MrDoyle

Methods) Area Calculator

Apr 9th, 2021
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6.         while (true) {
  7.             System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
  8.             System.out.println("1) Triangle");
  9.             System.out.println("2) Rectangle");
  10.             System.out.println("3) Square");
  11.             System.out.println("4) Circle");
  12.             System.out.println("5) Quit");
  13.  
  14.             System.out.print("Please enter your shape selection: ");
  15.             Scanner keyboard = new Scanner(System.in);
  16.             int shapeOption = keyboard.nextInt();
  17.  
  18.  
  19.             if (shapeOption == 1) {
  20.                 System.out.print("\nBase: ");
  21.                 Scanner keyboard2 = new Scanner(System.in);
  22.                 int base = keyboard2.nextInt();
  23.                 System.out.print("Height: ");
  24.                 Scanner keyboard3 = new Scanner(System.in);
  25.                 int height = keyboard3.nextInt();
  26.  
  27.                 System.out.println("\nThe area is: " + area_triangle(base, height) + " square units");
  28.  
  29.             } else if (shapeOption == 2) {
  30.                 System.out.print("\nLength: ");
  31.                 Scanner keyboard4 = new Scanner(System.in);
  32.                 int length = keyboard4.nextInt();
  33.                 System.out.print("Width: ");
  34.                 Scanner keyboard5 = new Scanner(System.in);
  35.                 int width = keyboard5.nextInt();
  36.  
  37.                 System.out.println("\nThe area is: " + area_rectangle(length, width) + " square units");
  38.  
  39.             } else if (shapeOption == 3) {
  40.                 System.out.print("\nSide length: ");
  41.                 Scanner keyboard6 = new Scanner(System.in);
  42.                 int side_length = keyboard6.nextInt();
  43.  
  44.                 System.out.println("\nThe area is: " + area_square(side_length) + " square units");
  45.  
  46.             } else if (shapeOption == 4) {
  47.                 System.out.print("\nRadius: ");
  48.                 Scanner keyboard7 = new Scanner(System.in);
  49.                 int radius = keyboard7.nextInt();
  50.  
  51.                 System.out.println("\nThe area is: " + area_circle(radius) + " square units");
  52.  
  53.             } else {
  54.                 System.out.println("\nGoodbye");
  55.                 break;
  56.             }
  57.  
  58.         }
  59.  
  60.     }
  61.  
  62.  
  63.     // returns the area of a circle
  64.     public static double area_circle( int radius ){
  65.         return Math.round((radius * radius * Math.PI)* 100.0) / 100.0;
  66.     }
  67.  
  68.  
  69.     // returns the area of a rectangle
  70.     public static int area_rectangle( int length, int width ){
  71.         return (length * width);
  72.     }
  73.  
  74.  
  75.     // returns the area of a square
  76.     public static int area_square( int side ) {
  77.         return (side * side);
  78.     }
  79.  
  80.  
  81.     // returns the area of a triangle
  82.     public static double area_triangle( int base, int height ){
  83.     return (0.5 * base * height);
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement