Toxotsist

T3.2

Sep 7th, 2021 (edited)
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. public interface Movable {
  2.     public void moveUp();
  3.     public void moveDown();
  4.     public void moveLeft();
  5.     public void moveRight();
  6. }
  7. ////////////////////////////////////////
  8. public class MovablePoint implements Movable {
  9.  
  10.     int x;
  11.     int y;
  12.     int xSpeed;
  13.     int ySpeed;
  14.  
  15.     MovablePoint(int a, int b, int xSp, int ySp){
  16.         x = a;
  17.         y = b;
  18.         xSpeed = xSp;
  19.         ySpeed = ySp;
  20.     }
  21.  
  22.     @Override
  23.     public void moveUp() {
  24.         this.y += ySpeed;
  25.     }
  26.  
  27.     @Override
  28.     public void moveDown() {
  29.         this.y -= ySpeed;
  30.     }
  31.  
  32.     @Override
  33.     public void moveLeft() {
  34.         this.x -= xSpeed;
  35.     }
  36.  
  37.     @Override
  38.     public void moveRight() {
  39.         this.x += xSpeed;
  40.     }
  41.  
  42.     @Override
  43.     public String toString() {
  44.         return "MovablePoint{" +
  45.                 "x=" + x +
  46.                 ", y=" + y +
  47.                 ", xSpeed=" + xSpeed +
  48.                 ", ySpeed=" + ySpeed +
  49.                 '}';
  50.     }
  51. }
  52.  
  53. ///////////////////////////////////////////
  54. public class MovableCircle implements Movable {
  55.     protected int radius;
  56.     protected MovablePoint center;
  57.     public MovableCircle(int x, int y, int xSpeed, int ySpeed, int
  58.             radius) {
  59.         this.radius = radius;
  60.         this.center = new MovablePoint(x, y, xSpeed, ySpeed);
  61.     }
  62.     @Override
  63.     public String toString() {return "Circle (" + center.x + ", " +
  64.             center.y + ") with radius=" + radius;}
  65.     @Override
  66.     public void moveUp() {center.moveUp();}
  67.     @Override
  68.     public void moveDown() {center.moveDown();}
  69.     @Override
  70.     public void moveRight() {center.moveRight();}
  71.     @Override
  72.     public void moveLeft() {center.moveLeft();}
  73. }
  74. ////////////////////////////////////////////////////
  75. public class MovableRectangle implements Movable {
  76.     private MovablePoint topLeft;
  77.     private MovablePoint bottomRight;
  78.     public MovableRectangle (int x1, int y1, int x2, int y2, int xSpeed,
  79.                              int ySpeed) {
  80.         this.topLeft = new MovablePoint(x1, y1, xSpeed, ySpeed);
  81.         this.bottomRight = new MovablePoint(x2, y2, xSpeed, ySpeed);
  82.     }
  83.     @Override
  84.     public String toString() {
  85.         return "Rectangle (" + topLeft.x + ", " + topLeft.y + ", " +
  86.                 bottomRight.x + ", " + bottomRight.y + ")";
  87.     }
  88.     @Override
  89.     public void moveUp() {topLeft.moveUp(); bottomRight.moveUp();}
  90.     @Override
  91.     public void moveDown() {topLeft.moveDown(); bottomRight.moveDown();}
  92.     @Override
  93.     public void moveRight() {topLeft.moveRight();
  94.         bottomRight.moveRight();}
  95.     @Override
  96.     public void moveLeft() {topLeft.moveLeft(); bottomRight.moveLeft();}
  97. }
  98. ///////////////////////////////////////////////////////////////////////////
  99. public class Main {
  100.     public static void main(String[] args){
  101.         MovablePoint point = new MovablePoint(0, 0, 1, 1);
  102.         System.out.println(point);
  103.         point.moveUp();
  104.         point.moveLeft();
  105.         System.out.println(point);
  106.         MovableCircle circle = new MovableCircle(0, 0, 1, 1, 2);
  107.         System.out.println(circle);
  108.         circle.moveDown();
  109.         circle.moveRight();
  110.         System.out.println(circle);
  111.         MovableRectangle rectangle = new MovableRectangle(0, 0, 1, 1, 1, 1);
  112.         System.out.println(rectangle);
  113.         rectangle.moveUp();
  114.         rectangle.moveLeft();
  115.         System.out.println(rectangle);
  116.     }
  117. }
Add Comment
Please, Sign In to add comment