Advertisement
GeneralGDA

Example

Nov 29th, 2022 (edited)
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.24 KB | None | 0 0
  1. interface Shape
  2. {
  3.     Shape setWidth(int value);
  4.     Shape setHeight(int value);
  5.  
  6.     int getWidth();
  7.     int getHeight();
  8. }
  9.  
  10. class Rect implements Shape {
  11.     private final int width;
  12.     private final int height;
  13.     public Rect(int width, int height) {
  14.         this.width = width;
  15.         this.height = height;
  16.     }
  17.     @Override public Shape setWidth(int value) {
  18.         return new Rect(value, height);
  19.     }
  20.     @Override public Shape setHeight(int value) {
  21.         return new Rect(width, value);
  22.     }
  23.     @Override public int getWidth() {
  24.         return width;
  25.     }
  26.     @Override public int getHeight() {
  27.         return height;
  28.     }
  29. }
  30.  
  31. class Square implements Shape {
  32.     private final int size;
  33.     public Square(int size) {
  34.         this.size = size;
  35.     }
  36.     @Override public Shape setWidth(int value) {
  37.         if (value != size){
  38.             return new Rect(value, size);
  39.         }
  40.         return this;
  41.     }
  42.     @Override public Shape setHeight(int value) {
  43.         if (value != size){
  44.             return new Rect(size, value);
  45.         }
  46.         return this;
  47.     }
  48.     @Override public int getWidth() {
  49.         return size;
  50.     }
  51.     @Override public int getHeight() {
  52.         return size;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement