Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class Figure
- {
- protected String color, type;
- protected int p;
- Observer obs = Observer.getInstance(5);
- public Figure(String color)
- {
- this.color = color;
- }
- abstract public double perimeter();
- // protected void Notify()
- // {
- // System.out.println(this);
- // }
- public void setColor(String s)
- {
- this.color = s;
- obs.Notify();
- }
- public boolean equals(Object o)
- {
- return (o instanceof Figure) && ((Figure)o).type.equals(this.type) &&
- ((Figure)o).perimeter() == this.perimeter();
- }
- public String toString()
- {
- return type + " " + color + " " + perimeter();
- }
- }
- //------------------------------------------------------------------------------
- class Circle extends Figure
- {
- private int radius;
- public Circle(int radius, String color)
- {
- super(color);
- type = "circle";
- this.radius = radius;
- obs.add(this);
- }
- public double perimeter()
- {
- return 2*3.14*radius;
- }
- public void setRadius(int x)
- {
- this.radius = x;
- obs.Notify();
- }
- }
- //-----------------------------------------------------------------------------------------
- class Triangle extends Figure
- {
- private int l1,l2,l3;
- public Triangle(int l1, int l2, int l3, String color)
- {
- super(color);
- type = "triangle";
- this.l1 = l1;
- this.l2 = l2;
- this.l3 = l3;
- obs.add(this);
- }
- public double perimeter()
- {
- return l1+l2+l3;
- }
- public void setLaturi(int x1, int x2, int x3)
- {
- this.l1 = x1;
- this.l2 = x2;
- this.l3 = x3;
- obs.Notify();
- }
- }
- //------------------------------------------------------------------------------
- class Square extends Figure
- {
- private int side;
- public Square(int side, String color)
- {
- super(color);
- type = "square";
- this.side = side;
- obs.add(this);
- }
- public double perimeter()
- {
- return 4*side;
- }
- public void setSide(int x)
- {
- this.side = x;
- obs.Notify();
- }
- }
- //------------------------------------------------------------------------------
- class Observer
- {
- private Figure vector[];
- private int capacity, i = 0,j;
- private static Observer observer = null;
- private Observer( int capacity)
- {
- this.capacity = capacity;
- vector = new Figure[capacity];
- }
- public boolean add(Figure figure_to_add)
- {
- if(i < capacity)
- {
- vector[i] = figure_to_add;
- i++;
- return true;
- }
- return false;
- }
- String buff = "";
- public String toString()
- {
- for(j = 0; j < i; j++)
- {
- buff += j+1 + ". " + vector[j] + "\n";
- }
- return buff;
- }
- 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
- {
- if(observer == null)
- observer = new Observer(capacity);
- return observer;
- }
- public void Notify()
- {
- System.out.println(this);
- }
- }
- //------------------------------------------------------------------------------
- class Main
- {
- public static void main(String[] args)
- {
- Figure f1, f2, f3, f4, f5;
- f1 = new Circle(5, "yellow");
- f2 = new Triangle(1,2,3,"red");
- f3 = new Square(100, "blue");
- // System.out.println(f1);
- // System.out.println(f2);
- // System.out.println(f3);
- // ((Circle)f1).setRadius(8);
- // ((Triangle)f2).setLaturi(4,5,6);
- ((Square)f3).setSide(10);
- // System.out.println(f1);
- // System.out.println(f2);
- // System.out.println(f3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement