Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class Figure {
- protected float x;
- protected float y;
- abstract float getArea();
- abstract float getPerimeter();
- Figure(float x, float y){
- this.x = x;
- this.y = y;
- }
- //main Для проверки
- //При проверки я его удалял
- public static void main(String[] args) {
- Circle circle = new Circle(10, 10, 1);
- Rectangle rectangle = new Rectangle(10, 10, 1, 1);
- circle.move(2, 2);
- circle.resize(5);
- rectangle.move(5, 0);
- rectangle.resize(3);
- System.out.println(circle);
- System.out.println();
- System.out.println(rectangle);
- }
- //
- }
- class Rectangle extends Figure implements Moveable{
- private float height;
- private float width;
- Rectangle(float x, float y, float height, float width) {
- super(x, y);
- this.height = height;
- this.width = width;
- }
- @Override
- public String toString() {
- return "Rectangle\nCenter: ("+(x+width/2)+", "+(y+height/2)+")\nHeight: "+height+"\nWidth: "+width;
- }
- @Override
- float getArea() {
- return height*width;
- }
- @Override
- float getPerimeter() {
- return height*2+width*2;
- }
- @Override
- public void move(float dx, float dy) {
- x = x +dx;
- y = y + dy;
- }
- @Override
- public void resize(float koeff) {
- height = height*koeff;
- width = width * koeff;
- }
- }
- class Circle extends Figure implements Moveable{
- @Override
- public String toString() {
- return "Circle\nCenter: ("+x+", "+y+")\nRadius: "+radius;
- }
- private float radius;
- Circle(float x, float y, float radius) {
- super(x, y);
- this.radius = radius;
- }
- @Override
- float getArea() {
- return (float) Math.PI * radius*radius;
- }
- @Override
- float getPerimeter() {
- return (float) (2*Math.PI*radius);
- }
- @Override
- public void move(float dx, float dy) {
- x = x+dx;
- y = y+dy;
- }
- @Override
- public void resize(float koeff) {
- radius = radius*koeff;
- }
- }
- interface Moveable {
- void move(float dx, float dy);
- void resize(float koeff);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement