Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. public abstract class Shape {
  2. protected double area;
  3.  
  4. public double getArea() {
  5. return area;
  6. }
  7.  
  8. protected abstract void onAreaChange();
  9. }
  10.  
  11. public class Triangle extends Shape {
  12. protected double base;
  13. protected double height;
  14.  
  15. public Triangle setBase(double base) {
  16. this.base = base;
  17. onAreaChange();
  18. return this;
  19. }
  20.  
  21. public Triangle setHeight(double height) {
  22. this.height = height;
  23. onAreaChange();
  24. return this;
  25. }
  26.  
  27. @Override
  28. public void onAreaChange() {
  29. this.area = 0.5 * this.base * this.height;
  30. }
  31. }
  32.  
  33. public class Rectangle extends Shape {
  34. protected double height;
  35. protected double width;
  36.  
  37. public Rectangle setHeight(double height) {
  38. this.height = height;
  39. onAreaChange();
  40. return this;
  41. }
  42.  
  43. public Rectangle setWidth(double width) {
  44. this.width = width;
  45. onAreaChange();
  46. return this;
  47. }
  48.  
  49. @Override
  50. public void onAreaChange() {
  51. this.area = this.height * this.width;
  52. }
  53. }
  54.  
  55. public class Circle extends Shape {
  56. protected double radius;
  57. protected final double PI = 3.14159265;
  58.  
  59. public Circle setRadius(double radius) {
  60. this.radius = radius;
  61. onAreaChange();
  62. return this;
  63. }
  64.  
  65. @Override
  66. public void onAreaChange() {
  67. this.area = this.radius * this.radius * PI;
  68. }
  69. }
  70.  
  71. import java.util.Scanner;
  72.  
  73. public class Main {
  74. public static Scanner scan = new Scanner(System.in);
  75. public static void main(String[] args) {
  76. while (true) {
  77. System.out.println("Enter the number which you want to compute the area");
  78. System.out.print("(1) Triangle (2) Rectangle (3) Circle ? ");
  79. switch (scan.nextInt()) {
  80. case 1:
  81. Triangle triangle = new Triangle();
  82. System.out.print("Base: ");
  83. triangle.setBase(scan.nextDouble());
  84. System.out.print("Height: ");
  85. triangle.setHeight(scan.nextDouble());
  86. System.out.println("Area of triangle: " + triangle.getArea());
  87. break;
  88. case 2:
  89. Rectangle rectangle = new Rectangle();
  90. System.out.print("Width: ");
  91. rectangle.setWidth(scan.nextDouble());
  92. System.out.print("Height: ");
  93. rectangle.setHeight(scan.nextDouble());
  94. System.out.println("Area of rectangle: " + rectangle.getArea());
  95. break;
  96. case 3:
  97. Circle circle = new Circle();
  98. System.out.print("Radius: ");
  99. circle.setRadius(scan.nextDouble());
  100. System.out.println("Area of circle: " + circle.getArea());
  101. break;
  102. default:
  103. System.out.println("What do u mean?");
  104. }
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement