Advertisement
pagolmonamer

Untitled

Apr 8th, 2022
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. interface Figure {
  4. public double getArea();
  5. public double getPerimeter();
  6. }
  7.  
  8. class Triangle implements Figure {
  9. private double a;
  10. private double b;
  11. private double c;
  12.  
  13. public Triangle(double a, double b, double c){
  14. this.a = a;
  15. this.b = b;
  16. this.c = c;
  17. }
  18.  
  19. public double getArea(){
  20. double s = getPerimeter() / 2;
  21. return Math.sqrt(s * (s - a) * (s - b) * (s - c));
  22. }
  23. public double getPerimeter(){
  24. return a + b + c;
  25. }
  26. }
  27.  
  28. class SmallTriangle extends Triangle {
  29. public SmallTriangle(){
  30. super(1,1,1);
  31. }
  32. }
  33. class BigTriangle extends Triangle {
  34. public BigTriangle(){
  35. super(10,10,10);
  36. }
  37. }
  38.  
  39. class Circle implements Figure {
  40. private double radius;
  41.  
  42. public Circle(double radius){
  43. this.radius = radius;
  44. }
  45. public double getArea(){
  46. return Math.PI * radius * radius;
  47. }
  48. public double getPerimeter(){
  49. return Math.PI * radius * 2;
  50. }
  51. }
  52.  
  53. public class Application {
  54. public static void main(String[] args){
  55. ArrayList<Figure> figures = new ArrayList<>();
  56. figures.add(new Triangle(1, 1, 1));
  57. figures.add(new SmallTriangle());
  58. figures.add(new BigTriangle());
  59. figures.add(new Circle(10));
  60.  
  61. for(int i = 0; i < figures.size(); ++i){
  62. System.out.println(figures.get(i).getArea() + " " + figures.get(i).getPerimeter());
  63. }
  64. }
  65. };
  66.  
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement