This commit is contained in:
2022-03-27 18:46:18 +08:00
parent 1c8f95c4b3
commit 85b623f65f
36 changed files with 1472 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
/**
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) Date: 2022/03/27
* Description: This is the Shape class.
*/
public class Shape {
private double x;
private double y;
/**
* Constructor
*
* @param x the position of the center point of the shape
* @param y the position of the center point of the shape
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of x
*
* @return the position of the center point of the shape
*/
public double getX() {
return x;
}
/**
* Getter of y
*
* @return the position of the center point of the shape
*/
public double getY() {
return y;
}
/**
* The are of the shape
*
* @return the area of the shape
*/
public double area() {
System.out.println("An unknown shape has an unknown area!");
return -1.0;
}
/**
* Test
*/
public static void testShape() {
Shape s = new Shape(1.0, 2.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == -1.0);
}
}

View File

@@ -0,0 +1,5 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
}
}

View File

@@ -0,0 +1,51 @@
/**
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) Date: 2022/03/27
* Description: This is the class of Circle.
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor of Circle.
*
* @param x the x coordinate of the center of the circle
* @param y the y coordinate of the center of the circle
* @param radius the radius of the circle
*/
public Circle(double x, double y, double radius) {
super(x, y);
this.radius = radius;
}
/**
* Get the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Test
*/
public static void testCircle() {
Circle c1 = new Circle(1.0, 2.0, 3.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
System.out.println(compare(c1.area(), 28.274333882308138));
}
/**
* Compare two double numbers.
*
* @param a
* @param b
* @return true if a and b are equal, false otherwise
*/
public static boolean compare(double a, double b) {
return Math.abs(a - b) < 0.000001;
}
}

View File

@@ -0,0 +1,59 @@
/**
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Shape class.
*/
public class Shape {
private double x;
private double y;
/**
* Constructor
*
* @param x the position of the center point of the shape
* @param y the position of the center point of the shape
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of x
*
* @return the position of the center point of the shape
*/
public double getX() {
return x;
}
/**
* Getter of y
*
* @return the position of the center point of the shape
*/
public double getY() {
return y;
}
/**
* The are of the shape
*
* @return the area of the shape
*/
public double area() {
System.out.println("An unknown shape has an unknown area!");
return -1.0;
}
/**
* Test
*/
public static void testShape() {
Shape s = new Shape(1.0, 2.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == -1.0);
}
}

View File

@@ -0,0 +1,6 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
}
}

View File

@@ -0,0 +1,51 @@
/**
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007) Date: 2022/03/27
* Description: This is the class of Circle.
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor of Circle.
*
* @param x the x coordinate of the center of the circle
* @param y the y coordinate of the center of the circle
* @param radius the radius of the circle
*/
public Circle(double x, double y, double radius) {
super(x, y);
this.radius = radius;
}
/**
* Get the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Test
*/
public static void testCircle() {
Circle c1 = new Circle(1.0, 2.0, 3.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
System.out.println(compare(c1.area(), 28.274333882308138));
}
/**
* Compare two double numbers.
*
* @param a
* @param b
* @return true if a and b are equal, false otherwise
*/
public static boolean compare(double a, double b) {
return Math.abs(a - b) < 0.000001;
}
}

View File

@@ -0,0 +1,37 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007).
* Date: 2022/03/27
* Description: This is the Dot class.
*/
public class Dot extends Shape {
/**
* Constructor
*
* @param x double
* @param y double
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Override the area method. Dot has no (0.0) area.
*
* @return double
*/
@Override
public double area() {
return 0.0;
}
/**
* Test
*/
public static void testDot() {
Dot d1 = new Dot(1.0, 2.0);
System.out.println(d1.getX() == 1.0);
System.out.println(d1.getY() == 2.0);
System.out.println(d1.area() == 0.0);
}
}

View File

@@ -0,0 +1,59 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Shape class.
*/
public class Shape {
private double x;
private double y;
/**
* Constructor
*
* @param x the position of the center point of the shape
* @param y the position of the center point of the shape
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of x
*
* @return the position of the center point of the shape
*/
public double getX() {
return x;
}
/**
* Getter of y
*
* @return the position of the center point of the shape
*/
public double getY() {
return y;
}
/**
* The are of the shape
*
* @return the area of the shape
*/
public double area() {
System.out.println("An unknown shape has an unknown area!");
return -1.0;
}
/**
* Test
*/
public static void testShape() {
Shape s = new Shape(1.0, 2.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == -1.0);
}
}

View File

@@ -0,0 +1,7 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
}
}

View File

@@ -0,0 +1,52 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the class of Circle.
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor of Circle.
*
* @param x the x coordinate of the center of the circle
* @param y the y coordinate of the center of the circle
* @param radius the radius of the circle
*/
public Circle(double x, double y, double radius) {
super(x, y);
this.radius = radius;
}
/**
* Get the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Test
*/
public static void testCircle() {
Circle c1 = new Circle(1.0, 2.0, 3.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
System.out.println(compare(c1.area(), 28.274333882308138));
}
/**
* Compare two double numbers.
*
* @param a
* @param b
* @return true if a and b are equal, false otherwise
*/
public static boolean compare(double a, double b) {
return Math.abs(a - b) < 0.000001;
}
}

View File

@@ -0,0 +1,37 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007).
* Date: 2022/03/27
* Description: This is the Dot class.
*/
public class Dot extends Shape {
/**
* Constructor
*
* @param x double
* @param y double
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Override the area method. Dot has no (0.0) area.
*
* @return double
*/
@Override
public double area() {
return 0.0;
}
/**
* Test
*/
public static void testDot() {
Dot d1 = new Dot(1.0, 2.0);
System.out.println(d1.getX() == 1.0);
System.out.println(d1.getY() == 2.0);
System.out.println(d1.area() == 0.0);
}
}

View File

@@ -0,0 +1,44 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Rectangle class.
*/
public class Rectangle extends Shape {
private double width;
private double height;
/**
* Constructor
*
* @param x the x coordinate of the center of the rectangle
* @param y the y coordinate of the center of the rectangle
* @param width the width of the rectangle
* @param length the length of the rectangle
*/
public Rectangle(double x, double y, double width, double length) {
super(x, y);
this.width = width;
this.height = length;
}
/**
* Override the area method
*
* @return double
*/
@Override
public double area() {
return width * height;
}
/**
* Test
*/
public static void testRectangle() {
Rectangle r1 = new Rectangle(1.0, 2.0, 3.0, 4.0);
System.out.println(r1.getX() == 1.0);
System.out.println(r1.getY() == 2.0);
System.out.println(r1.area() == 12.0);
}
}

View File

@@ -0,0 +1,59 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Shape class.
*/
public class Shape {
private double x;
private double y;
/**
* Constructor
*
* @param x the position of the center point of the shape
* @param y the position of the center point of the shape
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of x
*
* @return the position of the center point of the shape
*/
public double getX() {
return x;
}
/**
* Getter of y
*
* @return the position of the center point of the shape
*/
public double getY() {
return y;
}
/**
* The are of the shape
*
* @return the area of the shape
*/
public double area() {
System.out.println("An unknown shape has an unknown area!");
return -1.0;
}
/**
* Test
*/
public static void testShape() {
Shape s = new Shape(1.0, 2.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == -1.0);
}
}

View File

@@ -0,0 +1,28 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: Square class
*/
public class Square extends Rectangle {
/**
* Constructor
*
* @param x the x coordinate of the center of the square
* @param y the y coordinate of the center of the square
* @param size
*/
public Square(double x, double y, double size) {
super(x, y, size, size);
}
/**
* Test
*/
public static void testSquare() {
Square s = new Square(1.0, 2.0, 3.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == 9.0);
}
}

View File

@@ -0,0 +1,9 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
Rectangle.testRectangle();
Square.testSquare();
}
}

View File

@@ -0,0 +1,52 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the class of Circle.
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor of Circle.
*
* @param x the x coordinate of the center of the circle
* @param y the y coordinate of the center of the circle
* @param radius the radius of the circle
*/
public Circle(double x, double y, double radius) {
super(x, y);
this.radius = radius;
}
/**
* Get the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Test
*/
public static void testCircle() {
Circle c1 = new Circle(1.0, 2.0, 3.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
System.out.println(compare(c1.area(), 28.274333882308138));
}
/**
* Compare two double numbers.
*
* @param a
* @param b
* @return true if a and b are equal, false otherwise
*/
public static boolean compare(double a, double b) {
return Math.abs(a - b) < 0.000001;
}
}

View File

@@ -0,0 +1,37 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007).
* Date: 2022/03/27
* Description: This is the Dot class.
*/
public class Dot extends Shape {
/**
* Constructor
*
* @param x double
* @param y double
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Override the area method. Dot has no (0.0) area.
*
* @return double
*/
@Override
public double area() {
return 0.0;
}
/**
* Test
*/
public static void testDot() {
Dot d1 = new Dot(1.0, 2.0);
System.out.println(d1.getX() == 1.0);
System.out.println(d1.getY() == 2.0);
System.out.println(d1.area() == 0.0);
}
}

View File

@@ -0,0 +1,56 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the class for the many shapes.
*/
import java.util.ArrayList;
public class ManyShapes {
private ArrayList<Shape> allShapes;
/**
* Constructor
*/
public ManyShapes() {
this.allShapes = new ArrayList<Shape>();
}
/**
* addShape method takes a shape as argument and adds it to the arraylist.
*
* @param s the shape to be added
*/
public void addShape(Shape s) {
this.allShapes.add(s);
}
/**
* listAllShapes methods prints on the screen the area of each shape in the
* arraylist, one by one, using a loop. For example, if the arraylist currently
* contains a Square object of size 5 and a Dot object the the listAllShapes
* methods should print:
*
* Shape has area 25.0
*
* Shape has area 0.0
*/
public void listAllShapes() {
for (Shape s : this.allShapes) {
System.out.println("Shape has area " + s.area());
}
}
/**
* Test.
*/
public static void testManyShapes() {
ManyShapes m = new ManyShapes();
m.addShape(new Circle(1.2, 3.4, 4.0)); // Upcast from Circle to Shape.
m.addShape(new Dot(1.2, 3.4)); // Upcast from Dot to Shape.
m.addShape(new Rectangle(1.2, 3.4, 4.0, 5.0)); // Upcast from Rectangle to Shape.
m.addShape(new Shape(1.2, 3.4));
m.addShape(new Square(1.2, 3.4, 5.0)); // Upcast from Square to Shape.
m.listAllShapes();
}
}

View File

@@ -0,0 +1,44 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Rectangle class.
*/
public class Rectangle extends Shape {
private double width;
private double height;
/**
* Constructor
*
* @param x the x coordinate of the center of the rectangle
* @param y the y coordinate of the center of the rectangle
* @param width the width of the rectangle
* @param length the length of the rectangle
*/
public Rectangle(double x, double y, double width, double length) {
super(x, y);
this.width = width;
this.height = length;
}
/**
* Override the area method
*
* @return double
*/
@Override
public double area() {
return width * height;
}
/**
* Test
*/
public static void testRectangle() {
Rectangle r1 = new Rectangle(1.0, 2.0, 3.0, 4.0);
System.out.println(r1.getX() == 1.0);
System.out.println(r1.getY() == 2.0);
System.out.println(r1.area() == 12.0);
}
}

View File

@@ -0,0 +1,59 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Shape class.
*/
public class Shape {
private double x;
private double y;
/**
* Constructor
*
* @param x the position of the center point of the shape
* @param y the position of the center point of the shape
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of x
*
* @return the position of the center point of the shape
*/
public double getX() {
return x;
}
/**
* Getter of y
*
* @return the position of the center point of the shape
*/
public double getY() {
return y;
}
/**
* The are of the shape
*
* @return the area of the shape
*/
public double area() {
System.out.println("An unknown shape has an unknown area!");
return -1.0;
}
/**
* Test
*/
public static void testShape() {
Shape s = new Shape(1.0, 2.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == -1.0);
}
}

View File

@@ -0,0 +1,28 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: Square class
*/
public class Square extends Rectangle {
/**
* Constructor
*
* @param x the x coordinate of the center of the square
* @param y the y coordinate of the center of the square
* @param size
*/
public Square(double x, double y, double size) {
super(x, y, size, size);
}
/**
* Test
*/
public static void testSquare() {
Square s = new Square(1.0, 2.0, 3.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == 9.0);
}
}

View File

@@ -0,0 +1,10 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
Rectangle.testRectangle();
Square.testSquare();
ManyShapes.testManyShapes();
}
}

View File

@@ -0,0 +1,52 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the class of Circle.
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor of Circle.
*
* @param x the x coordinate of the center of the circle
* @param y the y coordinate of the center of the circle
* @param radius the radius of the circle
*/
public Circle(double x, double y, double radius) {
super(x, y);
this.radius = radius;
}
/**
* Get the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Test
*/
public static void testCircle() {
Circle c1 = new Circle(1.0, 2.0, 3.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
System.out.println(compare(c1.area(), 28.274333882308138));
}
/**
* Compare two double numbers.
*
* @param a
* @param b
* @return true if a and b are equal, false otherwise
*/
public static boolean compare(double a, double b) {
return Math.abs(a - b) < 0.000001;
}
}

View File

@@ -0,0 +1,37 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007).
* Date: 2022/03/27
* Description: This is the Dot class.
*/
public class Dot extends Shape {
/**
* Constructor
*
* @param x double
* @param y double
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Override the area method. Dot has no (0.0) area.
*
* @return double
*/
@Override
public double area() {
return 0.0;
}
/**
* Test
*/
public static void testDot() {
Dot d1 = new Dot(1.0, 2.0);
System.out.println(d1.getX() == 1.0);
System.out.println(d1.getY() == 2.0);
System.out.println(d1.area() == 0.0);
}
}

View File

@@ -0,0 +1,70 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the class for the many shapes.
*/
import java.util.ArrayList;
public class ManyShapes {
private ArrayList<Shape> allShapes;
/**
* Constructor
*/
public ManyShapes() {
this.allShapes = new ArrayList<Shape>();
}
/**
* addShape method takes a shape as argument and adds it to the arraylist.
*
* @param s the shape to be added
*/
public void addShape(Shape s) {
this.allShapes.add(s);
}
/**
* listAllShapes methods print out both the area and the type for each shape in
* the arraylist. For example, if the arraylist currently contains a Square
* object of size 5 and a Dot object the the listAllShapes method should print:
*
* Shape has area 25.0
*
* Shape has area 0.0
*/
public void listAllShapes() {
for (Shape s : this.allShapes) {
String type = "";
if (s instanceof Square) {
type = "Square";
} else if (s instanceof Dot) {
type = "Dot";
} else if (s instanceof Circle) {
type = "Circle";
} else if (s instanceof Rectangle) {
type = "Rectangle";
} else if (s instanceof Shape) {
type = "Shape";
} else if (s instanceof Square) {
type = "Square";
}
System.out.println(String.format("%s has area %f", type, s.area()));
}
}
/**
* Test.
*/
public static void testManyShapes() {
ManyShapes m = new ManyShapes();
m.addShape(new Circle(1.2, 3.4, 4.0)); // Upcast from Circle to Shape.
m.addShape(new Dot(1.2, 3.4)); // Upcast from Dot to Shape.
m.addShape(new Rectangle(1.2, 3.4, 4.0, 5.0)); // Upcast from Rectangle to Shape.
m.addShape(new Shape(1.2, 3.4));
m.addShape(new Square(1.2, 3.4, 5.0)); // Upcast from Square to Shape.
m.listAllShapes();
}
}

View File

@@ -0,0 +1,44 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Rectangle class.
*/
public class Rectangle extends Shape {
private double width;
private double height;
/**
* Constructor
*
* @param x the x coordinate of the center of the rectangle
* @param y the y coordinate of the center of the rectangle
* @param width the width of the rectangle
* @param length the length of the rectangle
*/
public Rectangle(double x, double y, double width, double length) {
super(x, y);
this.width = width;
this.height = length;
}
/**
* Override the area method
*
* @return double
*/
@Override
public double area() {
return width * height;
}
/**
* Test
*/
public static void testRectangle() {
Rectangle r1 = new Rectangle(1.0, 2.0, 3.0, 4.0);
System.out.println(r1.getX() == 1.0);
System.out.println(r1.getY() == 2.0);
System.out.println(r1.area() == 12.0);
}
}

View File

@@ -0,0 +1,59 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Shape class.
*/
public class Shape {
private double x;
private double y;
/**
* Constructor
*
* @param x the position of the center point of the shape
* @param y the position of the center point of the shape
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of x
*
* @return the position of the center point of the shape
*/
public double getX() {
return x;
}
/**
* Getter of y
*
* @return the position of the center point of the shape
*/
public double getY() {
return y;
}
/**
* The are of the shape
*
* @return the area of the shape
*/
public double area() {
System.out.println("An unknown shape has an unknown area!");
return -1.0;
}
/**
* Test
*/
public static void testShape() {
Shape s = new Shape(1.0, 2.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == -1.0);
}
}

View File

@@ -0,0 +1,28 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: Square class
*/
public class Square extends Rectangle {
/**
* Constructor
*
* @param x the x coordinate of the center of the square
* @param y the y coordinate of the center of the square
* @param size
*/
public Square(double x, double y, double size) {
super(x, y, size, size);
}
/**
* Test
*/
public static void testSquare() {
Square s = new Square(1.0, 2.0, 3.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == 9.0);
}
}

View File

@@ -0,0 +1,10 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
Rectangle.testRectangle();
Square.testSquare();
ManyShapes.testManyShapes();
}
}

View File

@@ -0,0 +1,60 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the class of Circle.
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor of Circle.
*
* @param x the x coordinate of the center of the circle
* @param y the y coordinate of the center of the circle
* @param radius the radius of the circle
*/
public Circle(double x, double y, double radius) {
super(x, y);
this.radius = radius;
}
/**
* Get the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Test
*/
public static void testCircle() {
Circle c1 = new Circle(1.0, 2.0, 3.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
System.out.println(compare(c1.area(), 28.274333882308138));
}
/**
* Compare two double numbers.
*
* @param a
* @param b
* @return true if a and b are equal, false otherwise
*/
public static boolean compare(double a, double b) {
return Math.abs(a - b) < 0.000001;
}
/**
* toString
*/
@Override
public String toString() {
return this.getClass().getName() + " has area " + area();
}
}

View File

@@ -0,0 +1,45 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007).
* Date: 2022/03/27
* Description: This is the Dot class.
*/
public class Dot extends Shape {
/**
* Constructor
*
* @param x double
* @param y double
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Override the area method. Dot has no (0.0) area.
*
* @return double
*/
@Override
public double area() {
return 0.0;
}
/**
* toString
*/
@Override
public String toString() {
return this.getClass().getName() + " has area " + area();
}
/**
* Test
*/
public static void testDot() {
Dot d1 = new Dot(1.0, 2.0);
System.out.println(d1.getX() == 1.0);
System.out.println(d1.getY() == 2.0);
System.out.println(d1.area() == 0.0);
}
}

View File

@@ -0,0 +1,55 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the class for the many shapes.
*/
import java.util.ArrayList;
public class ManyShapes {
private ArrayList<Shape> allShapes;
/**
* Constructor
*/
public ManyShapes() {
this.allShapes = new ArrayList<Shape>();
}
/**
* addShape method takes a shape as argument and adds it to the arraylist.
*
* @param s the shape to be added
*/
public void addShape(Shape s) {
this.allShapes.add(s);
}
/**
* listAllShapes methods print out both the area and the type for each shape in
* the arraylist. For example, if the arraylist currently contains a Square
* object of size 5 and a Dot object the the listAllShapes method should print:
*
* Shape has area 25.0
*
* Shape has area 0.0
*/
public void listAllShapes() {
for (Shape s : this.allShapes) {
System.out.println(s);
}
}
/**
* Test.
*/
public static void testManyShapes() {
ManyShapes m = new ManyShapes();
m.addShape(new Circle(1.2, 3.4, 4.0)); // Upcast from Circle to Shape.
m.addShape(new Dot(1.2, 3.4)); // Upcast from Dot to Shape.
m.addShape(new Rectangle(1.2, 3.4, 4.0, 5.0)); // Upcast from Rectangle to Shape.
m.addShape(new Shape(1.2, 3.4));
m.addShape(new Square(1.2, 3.4, 5.0)); // Upcast from Square to Shape.
m.listAllShapes();
}
}

View File

@@ -0,0 +1,52 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Rectangle class.
*/
public class Rectangle extends Shape {
private double width;
private double height;
/**
* Constructor
*
* @param x the x coordinate of the center of the rectangle
* @param y the y coordinate of the center of the rectangle
* @param width the width of the rectangle
* @param length the length of the rectangle
*/
public Rectangle(double x, double y, double width, double length) {
super(x, y);
this.width = width;
this.height = length;
}
/**
* Override the area method
*
* @return double
*/
@Override
public double area() {
return width * height;
}
/**
* toString
*/
@Override
public String toString() {
return this.getClass().getName() + " has area " + area();
}
/**
* Test
*/
public static void testRectangle() {
Rectangle r1 = new Rectangle(1.0, 2.0, 3.0, 4.0);
System.out.println(r1.getX() == 1.0);
System.out.println(r1.getY() == 2.0);
System.out.println(r1.area() == 12.0);
}
}

View File

@@ -0,0 +1,66 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: This is the Shape class.
*/
public class Shape {
private double x;
private double y;
/**
* Constructor
*
* @param x the position of the center point of the shape
* @param y the position of the center point of the shape
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of x
*
* @return the position of the center point of the shape
*/
public double getX() {
return x;
}
/**
* Getter of y
*
* @return the position of the center point of the shape
*/
public double getY() {
return y;
}
/**
* The are of the shape
*
* @return the area of the shape
*/
public double area() {
System.out.println("An unknown shape has an unknown area!");
return -1.0;
}
/**
* toString method
*/
public String toString() {
return this.getClass().getName() + " has area " + area();
}
/**
* Test
*/
public static void testShape() {
Shape s = new Shape(1.0, 2.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == -1.0);
}
}

View File

@@ -0,0 +1,36 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022/03/27
* Description: Square class
*/
public class Square extends Rectangle {
/**
* Constructor
*
* @param x the x coordinate of the center of the square
* @param y the y coordinate of the center of the square
* @param size
*/
public Square(double x, double y, double size) {
super(x, y, size, size);
}
/**
* toString
*/
@Override
public String toString() {
return this.getClass().getName() + " has area " + area();
}
/**
* Test
*/
public static void testSquare() {
Square s = new Square(1.0, 2.0, 3.0);
System.out.println(s.getX() == 1.0);
System.out.println(s.getY() == 2.0);
System.out.println(s.area() == 9.0);
}
}

View File

@@ -0,0 +1,10 @@
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
Rectangle.testRectangle();
Square.testSquare();
ManyShapes.testManyShapes();
}
}