Advertisement
Andrei_M

Inheritance 2

Nov 13th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. abstract class Figure
  2. {
  3.     protected String color, type;
  4.     protected int p;
  5.    
  6.    
  7.     Observer obs = Observer.getInstance(5);
  8.    
  9.    
  10.    
  11.     public Figure(String color)
  12.     {
  13.        
  14.         this.color = color;
  15.     }
  16.    
  17.     abstract public double perimeter();
  18.    
  19. //     protected void Notify()
  20. //     {
  21. //         System.out.println(this);
  22. //     }
  23.    
  24.     public void setColor(String s)
  25.     {
  26.         this.color = s;
  27.         obs.Notify();
  28.     }
  29.    
  30.     public boolean equals(Object o)
  31.     {
  32.        
  33.         return (o instanceof Figure) && ((Figure)o).type.equals(this.type) &&
  34.         ((Figure)o).perimeter() == this.perimeter();
  35.     }
  36.    
  37.     public String toString()
  38.     {
  39.         return type + " " + color + " " + perimeter();
  40.     }
  41. }
  42.  
  43. //------------------------------------------------------------------------------
  44. class Circle extends Figure
  45. {
  46.     private int radius;
  47.    
  48.     public Circle(int radius, String color)
  49.     {
  50.        
  51.         super(color);
  52.         type = "circle";
  53.         this.radius = radius;
  54.         obs.add(this);
  55.     }
  56.    
  57.     public double perimeter()
  58.     {
  59.         return 2*3.14*radius;
  60.     }
  61.    
  62.     public void setRadius(int x)
  63.     {
  64.         this.radius = x;
  65.         obs.Notify();
  66.     }
  67.    
  68.    
  69. }
  70.  
  71. //-----------------------------------------------------------------------------------------
  72. class Triangle extends Figure
  73. {
  74.     private int l1,l2,l3;
  75.    
  76.     public Triangle(int l1, int l2, int l3, String color)
  77.     {
  78.        
  79.         super(color);
  80.         type = "triangle";
  81.         this.l1 = l1;
  82.         this.l2 = l2;
  83.         this.l3 = l3;
  84.         obs.add(this);
  85.     }
  86.    
  87.     public double perimeter()
  88.     {
  89.         return l1+l2+l3;
  90.     }
  91.    
  92.     public void setLaturi(int x1, int x2, int x3)
  93.     {
  94.         this.l1 = x1;
  95.         this.l2 = x2;
  96.         this.l3 = x3;
  97.         obs.Notify();
  98.     }
  99.    
  100. }
  101.  
  102. //------------------------------------------------------------------------------
  103. class Square extends Figure
  104. {
  105.     private int side;
  106.     public Square(int side, String color)
  107.     {
  108.        
  109.         super(color);
  110.         type = "square";
  111.         this.side = side;
  112.         obs.add(this);
  113.     }
  114.     public double perimeter()
  115.     {
  116.         return 4*side;
  117.     }
  118.    
  119.     public void setSide(int x)
  120.     {
  121.         this.side = x;
  122.         obs.Notify();
  123.     }
  124.    
  125. }
  126.  
  127. //------------------------------------------------------------------------------
  128.  
  129. class Observer
  130. {
  131.     private Figure vector[];
  132.     private int capacity, i = 0,j;
  133.    
  134.     private static Observer observer = null;
  135.    
  136.     private Observer( int capacity)
  137.     {
  138.         this.capacity = capacity;
  139.         vector = new Figure[capacity];
  140.     }
  141.    
  142.    
  143.     public boolean add(Figure figure_to_add)
  144.     {
  145.         if(i < capacity)
  146.         {
  147.             vector[i] = figure_to_add;
  148.             i++;
  149.             return true;
  150.         }
  151.         return false;
  152.     }
  153.    
  154.     String buff = "";
  155.     public String toString()
  156.     {
  157.         for(j = 0; j < i; j++)
  158.         {
  159.             buff += j+1 + ". " + vector[j] + "\n";
  160.         }
  161.         return buff;
  162.     }
  163.    
  164.     public static Observer getInstance(int capacity) //o sa faca o noua instanta de observer doar daca actuala este nula, de aici o sa mearga la private constructor de sus si o sa puna capacity
  165.     {
  166.         if(observer == null)
  167.             observer = new Observer(capacity);
  168.         return observer;
  169.     }
  170.    
  171.     public void Notify()
  172.     {
  173.         System.out.println(this);
  174.     }
  175.    
  176. }
  177.  
  178. //------------------------------------------------------------------------------
  179. class Main
  180. {
  181.     public static void main(String[] args)
  182.     {
  183.         Figure f1, f2, f3, f4, f5;
  184.         f1 = new Circle(5, "yellow");
  185.         f2 = new Triangle(1,2,3,"red");
  186.         f3 = new Square(100, "blue");
  187.        
  188.        
  189.       //  System.out.println(f1);
  190.       //  System.out.println(f2);
  191.       //  System.out.println(f3);
  192.        
  193.       //  ((Circle)f1).setRadius(8);
  194.       //  ((Triangle)f2).setLaturi(4,5,6);
  195.           ((Square)f3).setSide(10);
  196.        
  197.        
  198.        // System.out.println(f1);
  199.        // System.out.println(f2);
  200.        // System.out.println(f3);
  201.        
  202.        
  203.        
  204.        
  205.        
  206.     }
  207. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement