Advertisement
Almetov_Kamil

2.6.2

Nov 17th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. public abstract class Figure {
  2.     private float x;
  3.     private float y;
  4.  
  5.     public Figure(float x, float y) {
  6.         this.x = x;
  7.         this.y = y;
  8.     }
  9.  
  10.     public void setY(float y) {
  11.         this.y = y;
  12.     }
  13.  
  14.     public void setX(float x) {
  15.         this.x = x;
  16.     }
  17.  
  18.     public float getY() {
  19.         return y;
  20.     }
  21.  
  22.     public float getX() {
  23.         return x;
  24.     }
  25.  
  26.     public abstract float getArea();
  27.  
  28.     public abstract float getPerimeter();
  29. }
  30.  
  31. public class Rectangle extends Figure implements Moveable {
  32.     private float height;
  33.     private float width;
  34.  
  35.     public Rectangle(float x, float y, float height, float width) {
  36.         super(x, y);
  37.         this.height = height;
  38.         this.width = width;
  39.     }
  40.  
  41.     @Override
  42.     public float getArea() {
  43.         return this.height * this.width;
  44.     }
  45.  
  46.     @Override
  47.     public float getPerimeter() {
  48.         return 2*(this.width + this.height);
  49.     }
  50.  
  51.     @Override
  52.     public void move(float dx, float dy) {
  53.         this.setX(this.getX()+dx);
  54.         this.setY(this.getY()+dy);
  55.     }
  56.  
  57.     @Override
  58.     public void resize(float koeff) {
  59.         this.height *= koeff;
  60.         this.width *= koeff;
  61.     }
  62.  
  63.     @Override
  64.     public String toString() {
  65.         return "Rectangle\n" +
  66.                 "Center: (" + (this.getX() + this.width/2) + ", " + (this.getY() + this.height/2) + ")\n" +
  67.                 "Height: " + this.height + "\n" +
  68.                 "Width: " + this.width;
  69.  
  70.     }
  71. }
  72.  
  73. public class Circle extends Figure implements Moveable {
  74.     private float radius;
  75.     private static final float pi = (float)Math.PI;
  76.  
  77.     public Circle(float x, float y, float radius) {
  78.         super(x, y);
  79.         this.radius = radius;
  80.     }
  81.  
  82.     @Override
  83.     public float getArea() {
  84.         return pi * radius * radius;
  85.     }
  86.  
  87.     @Override
  88.     public float getPerimeter() {
  89.         return 2 * pi * radius;
  90.     }
  91.  
  92.     @Override
  93.     public void move(float dx, float dy) {
  94.         this.setX(this.getX()+dx);
  95.         this.setY(this.getY()+dy);
  96.     }
  97.  
  98.     @Override
  99.     public void resize(float koeff) {
  100.         this.radius *= koeff;
  101.     }
  102.  
  103.     @Override
  104.     public String toString() {
  105.         return "Circle\n" +
  106.         "Center: (" + this.getX() + ", " + this.getY() + ")\n" +
  107.         "Radius: " + this.radius;
  108.     }
  109. }
  110.  
  111. public interface Moveable {
  112.     void move(float dx, float dy);
  113.  
  114.     void resize(float koeff);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement