Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.28 KB | None | 0 0
  1. package GeometricFigures;
  2.  
  3. abstract public class Figure {
  4.     protected String color;
  5.     protected String type;
  6.     abstract void modifyColor(String string, Observer o);
  7.     protected abstract double getPerimeter();
  8.     abstract public double computePerimeter();
  9.    
  10.     public Figure(String color, String type) {
  11.         this.color = color;
  12.         this.type = type;
  13.     }
  14.    
  15.     public String getColor() {
  16.         return color;
  17.     }
  18.    
  19.     public String getType() {
  20.         return type;
  21.     }
  22.        
  23.     public String toString() {
  24.         return type + " " + color + " " + Double.toString(computePerimeter());
  25.     }
  26.    
  27.     public boolean equals(Object o) {
  28.         return (o instanceof Figure) && ((Figure)o).type==type && ((Figure)o).getPerimeter() == this.getPerimeter();
  29.     }
  30.  
  31. }
  32.  
  33. package GeometricFigures;
  34.  
  35. public class Circle extends Figure{
  36.  
  37.     double pCircle;
  38.     int radius;
  39.     int r;
  40.    
  41.     public Circle(String color, String type, int radius) {
  42.         super(type,color);
  43.         this.radius = radius;
  44.     }
  45.    
  46.     public void setRadius(int r) {
  47.         this.radius = r;
  48.     }
  49.    
  50.     public void setType(String t) {
  51.         this.type = t;
  52.     }
  53.    
  54.     public void setColor(String c) {
  55.         this.color = c;
  56.     }
  57.    
  58.     public double computePerimeter() {
  59.         return pCircle = 2 * Math.PI * radius;
  60.     }
  61.    
  62.     public double getPerimeter() {
  63.         return this.pCircle;
  64.     }
  65.    
  66.     public void modifyColor(String newcolor, Observer o) {
  67.         this.color = newcolor;
  68.         o.notifyObserver();
  69.     }
  70.    
  71. }
  72.  
  73.  
  74. package GeometricFigures;
  75.  
  76. public class Square extends Figure{
  77.    
  78.     int side;
  79.     int s;
  80.     public double pSquare;
  81.     String c;
  82.     String t;
  83.    
  84.    
  85.     public Square(String color, String type, int side){
  86.         super(type, color);
  87.         this.side = side;
  88.     }
  89.    
  90.     public void setSide(int s) {
  91.         this.side = s;
  92.     }
  93.    
  94.     public void setType(String t) {
  95.         type = t;
  96.     }
  97.    
  98.     public void setColor(String c) {
  99.         color = c;
  100.     }
  101.    
  102.     public double computePerimeter() {
  103.         return pSquare = 4 * side;
  104.     }
  105.    
  106.     public double getPerimeter() {
  107.         return this.pSquare;
  108.     }
  109.    
  110.     public void modifyColor(String newcolor, Observer o) {
  111.         this.color = newcolor;
  112.         o.notifyObserver();
  113.     }
  114.    
  115. }
  116.  
  117.  
  118. package GeometricFigures;
  119.  
  120. public class Triangle extends Figure{
  121.    
  122.     int side1, side2, side3;
  123.     int sd1, sd2, sd3;
  124.     int pTriangle;
  125.    
  126.     public Triangle(String color, String type, int side1, int side2, int side3) {
  127.         super(type,color);
  128.         this.side1 = side1;
  129.         this.side2 = side2;
  130.         this.side3 = side3;
  131.     }
  132.    
  133.     public void setTriangle(int sd1, int sd2, int sd3) {
  134.         this.side1 = sd1;
  135.         this.side2 = sd2;
  136.         this.side3 = sd3;
  137.     }
  138.    
  139.     public void setType(String t) {
  140.         type = t;
  141.     }
  142.    
  143.     public void setColor(String c) {
  144.         color = c;
  145.     }
  146.    
  147.     public double computePerimeter() {
  148.         return pTriangle = side1 + side2 + side3;
  149.     }
  150.    
  151.     public double getPerimeter() {
  152.         return this.pTriangle;
  153.     }
  154.    
  155.     public void modifyColor(String newcolor, Observer o) {
  156.         this.color = newcolor;
  157.         o.notifyObserver();
  158.     }
  159.  
  160. }
  161.  
  162.  
  163. package GeometricFigures;
  164.  
  165. public class Observer{
  166.    
  167.     private Figure[] figures;
  168.     private int i = 0;
  169.     private int j = 0;
  170.     private int noOfFigures;
  171.    
  172.     public Observer(int noOfFigures) {
  173.         this.noOfFigures = noOfFigures;
  174.         figures = new Figure[noOfFigures];
  175.     }
  176.    
  177.     public boolean addFigure(Figure f) {
  178.         if(i==0) {
  179.             figures[i] = f;
  180.             i++;
  181.             return true;
  182.         }
  183.        
  184.         if(i==figures.length) {
  185.             return false;
  186.         }
  187.        
  188.         for(j=0; j<figures.length; j++) {
  189.             if((f.equals(figures[j])==true)){
  190.                 return false;
  191.             }
  192.         }
  193.        
  194.         if(i<noOfFigures) {
  195.             figures[i] = f;
  196.             i++;
  197.         }
  198.        
  199.         return true;
  200.     }
  201.    
  202.     public void notifyObserver() {
  203.         System.out.println("MODIFIED");
  204.     }
  205.    
  206.     public void printAllFigure() {
  207.         for(i=0 ; i<figures.length ; i++) {
  208.             System.out.println(figures[i]);
  209.         }
  210.     }
  211.    
  212. }
  213.  
  214.  
  215. package GeometricFigures;
  216.  
  217. public class Main {
  218.     public static void main(String [] args) {
  219.            
  220.         Figure c1 = new Circle("Circle","Blue",5);
  221.         ((Circle)c1).computePerimeter();
  222.         System.out.println(c1);
  223.         ((Circle)c1).setRadius(4);
  224.         ((Circle)c1).computePerimeter();
  225.         System.out.println(c1);
  226.         ((Circle)c1).setType("Rectangle");
  227.         System.out.println(c1);
  228.         ((Circle)c1).setColor("Pink");
  229.         System.out.println(c1);
  230.        
  231.         Figure s1 = new Square("Square","Black",4);
  232.         ((Square)s1).computePerimeter();
  233.         System.out.println(s1);
  234.         ((Square)s1).setSide(3);
  235.         ((Square)s1).computePerimeter();
  236.         System.out.println(s1);
  237.         ((Square)s1).setType("Hexagon");
  238.         System.out.println(s1);
  239.         ((Square)s1).setColor("Brown");
  240.         System.out.println(s1);
  241.        
  242.         Figure t1 = new Triangle("Triangle","Green",1,2,3);
  243.         ((Triangle)t1).computePerimeter();
  244.         System.out.println(t1);
  245.         ((Triangle)t1).setTriangle(4,5,6);
  246.         ((Triangle)t1).computePerimeter();
  247.         System.out.println(t1);
  248.         ((Triangle)t1).setType("Cube");
  249.         System.out.println(t1);
  250.         ((Triangle)t1).setColor("White");
  251.         System.out.println(t1);
  252.        
  253.         Figure t2 = new Triangle("Triangle","Green",1,2,3);
  254.         ((Triangle)t2).computePerimeter();
  255.         System.out.println(t2);
  256.         ((Triangle)t2).setTriangle(4,5,6);
  257.         ((Triangle)t2).computePerimeter();
  258.         System.out.println(t2);
  259.         ((Triangle)t2).setType("Cube");
  260.         System.out.println(t2);
  261.         ((Triangle)t2).setColor("White");
  262.         System.out.println(t2);
  263.        
  264.         Observer o1= new Observer(4);
  265.        
  266.         o1.addFigure(c1);
  267.         o1.addFigure(s1);
  268.         o1.addFigure(t1);
  269.         o1.addFigure(t2);
  270.        
  271.         o1.printAllFigure();
  272.        
  273.         c1.modifyColor("Yellow",o1);
  274.        
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement