Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public abstract class Figure {
- private float x;
- private float y;
- public Figure(float x, float y) {
- this.x = x;
- this.y = y;
- }
- public void setY(float y) {
- this.y = y;
- }
- public void setX(float x) {
- this.x = x;
- }
- public float getY() {
- return y;
- }
- public float getX() {
- return x;
- }
- public abstract float getArea();
- public abstract float getPerimeter();
- }
- public class Rectangle extends Figure implements Moveable {
- private float height;
- private float width;
- public Rectangle(float x, float y, float height, float width) {
- super(x, y);
- this.height = height;
- this.width = width;
- }
- @Override
- public float getArea() {
- return this.height * this.width;
- }
- @Override
- public float getPerimeter() {
- return 2*(this.width + this.height);
- }
- @Override
- public void move(float dx, float dy) {
- this.setX(this.getX()+dx);
- this.setY(this.getY()+dy);
- }
- @Override
- public void resize(float koeff) {
- this.height *= koeff;
- this.width *= koeff;
- }
- @Override
- public String toString() {
- return "Rectangle\n" +
- "Center: (" + (this.getX() + this.width/2) + ", " + (this.getY() + this.height/2) + ")\n" +
- "Height: " + this.height + "\n" +
- "Width: " + this.width;
- }
- }
- public class Circle extends Figure implements Moveable {
- private float radius;
- private static final float pi = (float)Math.PI;
- public Circle(float x, float y, float radius) {
- super(x, y);
- this.radius = radius;
- }
- @Override
- public float getArea() {
- return pi * radius * radius;
- }
- @Override
- public float getPerimeter() {
- return 2 * pi * radius;
- }
- @Override
- public void move(float dx, float dy) {
- this.setX(this.getX()+dx);
- this.setY(this.getY()+dy);
- }
- @Override
- public void resize(float koeff) {
- this.radius *= koeff;
- }
- @Override
- public String toString() {
- return "Circle\n" +
- "Center: (" + this.getX() + ", " + this.getY() + ")\n" +
- "Radius: " + this.radius;
- }
- }
- public interface Moveable {
- void move(float dx, float dy);
- void resize(float koeff);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement