Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. import java.util.*;
  2. public class MathProgram {
  3.  
  4. public static void main(String[] args) {
  5. Scanner input = new Scanner(System.in);
  6.  
  7. String shape;
  8. double length;
  9. double width;
  10. double base;
  11. double radius;
  12. double height;
  13.  
  14. System.out.println("Please choose a shape:\n 1. Square\n 2. Rectangle\n 3. Triangle\n 4. Circle");
  15. shape = input.nextLine();
  16.  
  17.  
  18. switch (shape) {
  19.  
  20. case "Square":
  21.  
  22. System.out.println("Please enter the length/width of your squre: ");
  23. length = input.nextDouble();
  24.  
  25. System.out.println("The area of your square is " + squareArea(length));
  26.  
  27. break;
  28.  
  29. case "Rectangle":
  30.  
  31. System.out.println("Please enter the length of your rectangle: ");
  32. length = input.nextDouble();
  33.  
  34. System.out.println("Please enter the width of your rectangle: ");
  35. width = input.nextDouble();
  36.  
  37. System.out.println("The area of your rectangle is " + rectangleArea(length, width));
  38.  
  39. break;
  40.  
  41. case "Triangle":
  42.  
  43. System.out.println("Please enter the base of your triangle: ");
  44. base = input.nextDouble();
  45.  
  46. System.out.println("Please enter the height of your triangle: ");
  47. height = input.nextDouble();
  48.  
  49. System.out.println("The area of your triangle is " + triangleArea(base, height));
  50.  
  51. break;
  52.  
  53. case "Circle":
  54.  
  55. System.out.println("Please enter the radius of your circle: ");
  56. radius = input.nextDouble();
  57.  
  58. System.out.println("The area of your circle is " + circleArea(radius));
  59.  
  60. break;
  61.  
  62.  
  63. }
  64.  
  65. }
  66.  
  67. public static double squareArea(double length) {
  68. double area;
  69.  
  70. area = Math.pow(length, 2);
  71.  
  72. return area;
  73.  
  74. }
  75.  
  76. public static double rectangleArea (double length, double width) {
  77. double area;
  78.  
  79. area = length * width;
  80.  
  81. return area;
  82.  
  83. }
  84.  
  85. public static double triangleArea(double height, double base) {
  86. double area;
  87.  
  88. area = (height * base) / 2.0;
  89.  
  90. return area;
  91.  
  92.  
  93. }
  94.  
  95. public static double circleArea(double radius) {
  96. double area;
  97.  
  98. area = (Math.PI * Math.pow(radius, 2));
  99.  
  100. return area;
  101.  
  102. }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement