lab8 q1
This commit is contained in:
88
lab/lab8/Question1/Shape.java
Normal file
88
lab/lab8/Question1/Shape.java
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
5
lab/lab8/Question1/Start.java
Normal file
5
lab/lab8/Question1/Start.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class Start {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Shape.testShape();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user