From fdb4e8296d4f1f88f87bdc0f8699e8738b18ad5f Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 14 Apr 2022 19:03:03 +0800 Subject: [PATCH] lab8 q1 --- lab/lab8/Question1/Shape.java | 88 +++++++++++++++++++++++++++++++++++ lab/lab8/Question1/Start.java | 5 ++ 2 files changed, 93 insertions(+) create mode 100644 lab/lab8/Question1/Shape.java create mode 100644 lab/lab8/Question1/Start.java diff --git a/lab/lab8/Question1/Shape.java b/lab/lab8/Question1/Shape.java new file mode 100644 index 0000000..2e0d63d --- /dev/null +++ b/lab/lab8/Question1/Shape.java @@ -0,0 +1,88 @@ +/* + * Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) + * Date: 2022-04-15 + * Description: This is the abstract Shape class. + */ + +public abstract class Shape { + /* + * Store the position of the central point of the shape. + */ + private double x; + private double y; + + /** + * Constructor of the Shape class. + * + * @param x the x-coordinate of the central point of the shape. + * @param y the y-coordinate of the central point of the shape. + */ + public Shape(double x, double y) { + this.x = x; + this.y = y; + } + + /** + * Getter of the x coordinate of the central point of the shape. + * + * @return the x coordinate of the central point of the shape. + */ + public double getX() { + return x; + } + + /** + * Getter of the y coordinate of the central point of the shape. + * + * @return the y coordinate of the central point of the shape. + */ + public double getY() { + return y; + } + + /** + * Move the central point of the shape by amounts dx and dy. + * + * @param dx the amount of movement in x direction. + * @param dy the amount of movement in y direction. + */ + public void move(double dx, double dy) { + x += dx; + y += dy; + } + + /** + * Abstract method to calculate the area of the shape. + * + * @return the area of the shape. + */ + public abstract double area(); + + /** + * Abstract method to resize the shape. + * + * @param newSize the new size of the shape. + */ + public abstract void resize(double newSize); + + /** + * Test. + */ + public static void testShape() { + Shape s = new Shape(3.9, 4.2) { + @Override + public double area() { + return 0; + } + + @Override + public void resize(double newSize) { + } + }; + System.out.println(s.getX() == 3.9); + System.out.println(s.getY() == 4.2); + s.move(1.0, 2.0); + System.out.println(s.getX() == 4.9); + System.out.println(s.getY() == 6.2); + } +} diff --git a/lab/lab8/Question1/Start.java b/lab/lab8/Question1/Start.java new file mode 100644 index 0000000..7afbdb6 --- /dev/null +++ b/lab/lab8/Question1/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Shape.testShape(); + } +}