Advertisement
Hrispens

Untitled

Dec 13th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. public abstract class Figure {
  2.     protected float x;
  3.     protected float y;
  4.     abstract float getArea();
  5.     abstract float getPerimeter();
  6.     Figure(float x, float y){
  7.         this.x = x;
  8.         this.y = y;
  9.     }
  10. //main Для проверки
  11.     //При проверки я его удалял
  12.     public static void main(String[] args) {
  13.         Circle circle = new Circle(10, 10, 1);
  14.         Rectangle rectangle = new Rectangle(10, 10, 1, 1);
  15.         circle.move(2, 2);
  16.         circle.resize(5);
  17.         rectangle.move(5, 0);
  18.         rectangle.resize(3);
  19.         System.out.println(circle);
  20.         System.out.println();
  21.         System.out.println(rectangle);
  22.     }
  23. //
  24. }
  25.  
  26. class Rectangle extends Figure implements Moveable{
  27.     private float height;
  28.     private float width;
  29.     Rectangle(float x, float y, float height, float width) {
  30.         super(x, y);
  31.         this.height = height;
  32.         this.width = width;
  33.     }
  34.  
  35.     @Override
  36.     public String toString() {
  37.         return "Rectangle\nCenter: ("+(x+width/2)+", "+(y+height/2)+")\nHeight: "+height+"\nWidth: "+width;
  38.     }
  39.  
  40.     @Override
  41.     float getArea() {
  42.         return height*width;
  43.     }
  44.  
  45.     @Override
  46.     float getPerimeter() {
  47.         return height*2+width*2;
  48.     }
  49.  
  50.     @Override
  51.     public void move(float dx, float dy) {
  52.     x = x +dx;
  53.     y = y + dy;
  54.     }
  55.  
  56.     @Override
  57.     public void resize(float koeff) {
  58.         height = height*koeff;
  59.         width = width * koeff;
  60.     }
  61. }
  62.  
  63. class Circle extends Figure implements Moveable{
  64.  
  65.     @Override
  66.     public String toString() {
  67.         return "Circle\nCenter: ("+x+", "+y+")\nRadius: "+radius;
  68.     }
  69.  
  70.     private float radius;
  71.     Circle(float x, float y, float radius) {
  72.         super(x, y);
  73.         this.radius = radius;
  74.     }
  75.  
  76.     @Override
  77.     float getArea() {
  78.         return (float) Math.PI * radius*radius;
  79.     }
  80.  
  81.     @Override
  82.     float getPerimeter() {
  83.         return (float) (2*Math.PI*radius);
  84.     }
  85.  
  86.     @Override
  87.     public void move(float dx, float dy) {
  88.         x = x+dx;
  89.         y = y+dy;
  90.     }
  91.  
  92.     @Override
  93.     public void resize(float koeff) {
  94.         radius = radius*koeff;
  95.     }
  96. }
  97. interface Moveable {
  98.     void move(float dx, float dy);
  99.     void resize(float koeff);
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement