Advertisement
malixds_

23

Jan 16th, 2023
745
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. // An abstract class for geometric figures
  2.     static abstract class GeometricFigure {
  3.         abstract double getArea();
  4.         abstract double getPerimeter();
  5.     }
  6.  
  7.  
  8.     // A class for rectangles
  9.     static class Rectangle extends GeometricFigure {
  10.         double length, breadth;
  11.  
  12.  
  13.         Rectangle(double length, double breadth)
  14.         {
  15.             this.length = length;
  16.             this.breadth = breadth;
  17.         }
  18.  
  19.         @Override
  20.         double getArea()
  21.         {
  22.             return length * breadth;
  23.         }
  24.  
  25.         @Override
  26.         double getPerimeter()
  27.         {
  28.             return 2 * (length + breadth);
  29.         }
  30.  
  31.     }
  32.  
  33.  
  34.     // A class for circles
  35.     static class Circle extends GeometricFigure {
  36.         double radius;
  37.  
  38.  
  39.         Circle(double radius)
  40.         {
  41.             this.radius = radius;
  42.         }
  43.  
  44.         @Override
  45.         double getArea()
  46.         {
  47.             return 3.14 * radius * radius;
  48.         }
  49.  
  50.         @Override
  51.         double getPerimeter()
  52.         {
  53.             return 2 * 3.14 * radius;
  54.         }
  55.  
  56.     }
  57.  
  58.  
  59.     // Class for the fabric pattern
  60.     static class GeometricFigureFactory {
  61.  
  62.  
  63.         public static GeometricFigure getFigure(String type)
  64.         {
  65.             if (type == null) {
  66.                 return null;
  67.             }
  68.             if (type.equalsIgnoreCase("RECTANGLE")) {
  69.                 return new Rectangle(10, 20);
  70.             }
  71.             else if (type.equalsIgnoreCase("CIRCLE")) {
  72.                 return new Circle(5);
  73.             }
  74.  
  75.             return null;
  76.         }
  77.  
  78.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement