Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface Movable {
- public void moveUp();
- public void moveDown();
- public void moveLeft();
- public void moveRight();
- }
- ////////////////////////////////////////
- public class MovablePoint implements Movable {
- int x;
- int y;
- int xSpeed;
- int ySpeed;
- MovablePoint(int a, int b, int xSp, int ySp){
- x = a;
- y = b;
- xSpeed = xSp;
- ySpeed = ySp;
- }
- @Override
- public void moveUp() {
- this.y += ySpeed;
- }
- @Override
- public void moveDown() {
- this.y -= ySpeed;
- }
- @Override
- public void moveLeft() {
- this.x -= xSpeed;
- }
- @Override
- public void moveRight() {
- this.x += xSpeed;
- }
- @Override
- public String toString() {
- return "MovablePoint{" +
- "x=" + x +
- ", y=" + y +
- ", xSpeed=" + xSpeed +
- ", ySpeed=" + ySpeed +
- '}';
- }
- }
- ///////////////////////////////////////////
- public class MovableCircle implements Movable {
- protected int radius;
- protected MovablePoint center;
- public MovableCircle(int x, int y, int xSpeed, int ySpeed, int
- radius) {
- this.radius = radius;
- this.center = new MovablePoint(x, y, xSpeed, ySpeed);
- }
- @Override
- public String toString() {return "Circle (" + center.x + ", " +
- center.y + ") with radius=" + radius;}
- @Override
- public void moveUp() {center.moveUp();}
- @Override
- public void moveDown() {center.moveDown();}
- @Override
- public void moveRight() {center.moveRight();}
- @Override
- public void moveLeft() {center.moveLeft();}
- }
- ////////////////////////////////////////////////////
- public class MovableRectangle implements Movable {
- private MovablePoint topLeft;
- private MovablePoint bottomRight;
- public MovableRectangle (int x1, int y1, int x2, int y2, int xSpeed,
- int ySpeed) {
- this.topLeft = new MovablePoint(x1, y1, xSpeed, ySpeed);
- this.bottomRight = new MovablePoint(x2, y2, xSpeed, ySpeed);
- }
- @Override
- public String toString() {
- return "Rectangle (" + topLeft.x + ", " + topLeft.y + ", " +
- bottomRight.x + ", " + bottomRight.y + ")";
- }
- @Override
- public void moveUp() {topLeft.moveUp(); bottomRight.moveUp();}
- @Override
- public void moveDown() {topLeft.moveDown(); bottomRight.moveDown();}
- @Override
- public void moveRight() {topLeft.moveRight();
- bottomRight.moveRight();}
- @Override
- public void moveLeft() {topLeft.moveLeft(); bottomRight.moveLeft();}
- }
- ///////////////////////////////////////////////////////////////////////////
- public class Main {
- public static void main(String[] args){
- MovablePoint point = new MovablePoint(0, 0, 1, 1);
- System.out.println(point);
- point.moveUp();
- point.moveLeft();
- System.out.println(point);
- MovableCircle circle = new MovableCircle(0, 0, 1, 1, 2);
- System.out.println(circle);
- circle.moveDown();
- circle.moveRight();
- System.out.println(circle);
- MovableRectangle rectangle = new MovableRectangle(0, 0, 1, 1, 1, 1);
- System.out.println(rectangle);
- rectangle.moveUp();
- rectangle.moveLeft();
- System.out.println(rectangle);
- }
- }
Add Comment
Please, Sign In to add comment