From 85b623f65faca6c258e9dcecebcecdb97d01874a Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 27 Mar 2022 18:46:18 +0800 Subject: [PATCH] Add lab6 --- lab/lab6/Question1/Shape.java | 58 +++++++++++++++++++++++++ lab/lab6/Question1/Start.java | 5 +++ lab/lab6/Question2/Circle.java | 51 ++++++++++++++++++++++ lab/lab6/Question2/Shape.java | 59 +++++++++++++++++++++++++ lab/lab6/Question2/Start.java | 6 +++ lab/lab6/Question3/Circle.java | 51 ++++++++++++++++++++++ lab/lab6/Question3/Dot.java | 37 ++++++++++++++++ lab/lab6/Question3/Shape.java | 59 +++++++++++++++++++++++++ lab/lab6/Question3/Start.java | 7 +++ lab/lab6/Question4/Circle.java | 52 ++++++++++++++++++++++ lab/lab6/Question4/Dot.java | 37 ++++++++++++++++ lab/lab6/Question4/Rectangle.java | 44 +++++++++++++++++++ lab/lab6/Question4/Shape.java | 59 +++++++++++++++++++++++++ lab/lab6/Question4/Square.java | 28 ++++++++++++ lab/lab6/Question4/Start.java | 9 ++++ lab/lab6/Question5/Circle.java | 52 ++++++++++++++++++++++ lab/lab6/Question5/Dot.java | 37 ++++++++++++++++ lab/lab6/Question5/ManyShapes.java | 56 ++++++++++++++++++++++++ lab/lab6/Question5/Rectangle.java | 44 +++++++++++++++++++ lab/lab6/Question5/Shape.java | 59 +++++++++++++++++++++++++ lab/lab6/Question5/Square.java | 28 ++++++++++++ lab/lab6/Question5/Start.java | 10 +++++ lab/lab6/Question6/Circle.java | 52 ++++++++++++++++++++++ lab/lab6/Question6/Dot.java | 37 ++++++++++++++++ lab/lab6/Question6/ManyShapes.java | 70 ++++++++++++++++++++++++++++++ lab/lab6/Question6/Rectangle.java | 44 +++++++++++++++++++ lab/lab6/Question6/Shape.java | 59 +++++++++++++++++++++++++ lab/lab6/Question6/Square.java | 28 ++++++++++++ lab/lab6/Question6/Start.java | 10 +++++ lab/lab6/Question7/Circle.java | 60 +++++++++++++++++++++++++ lab/lab6/Question7/Dot.java | 45 +++++++++++++++++++ lab/lab6/Question7/ManyShapes.java | 55 +++++++++++++++++++++++ lab/lab6/Question7/Rectangle.java | 52 ++++++++++++++++++++++ lab/lab6/Question7/Shape.java | 66 ++++++++++++++++++++++++++++ lab/lab6/Question7/Square.java | 36 +++++++++++++++ lab/lab6/Question7/Start.java | 10 +++++ 36 files changed, 1472 insertions(+) create mode 100644 lab/lab6/Question1/Shape.java create mode 100644 lab/lab6/Question1/Start.java create mode 100644 lab/lab6/Question2/Circle.java create mode 100644 lab/lab6/Question2/Shape.java create mode 100644 lab/lab6/Question2/Start.java create mode 100644 lab/lab6/Question3/Circle.java create mode 100644 lab/lab6/Question3/Dot.java create mode 100644 lab/lab6/Question3/Shape.java create mode 100644 lab/lab6/Question3/Start.java create mode 100644 lab/lab6/Question4/Circle.java create mode 100644 lab/lab6/Question4/Dot.java create mode 100644 lab/lab6/Question4/Rectangle.java create mode 100644 lab/lab6/Question4/Shape.java create mode 100644 lab/lab6/Question4/Square.java create mode 100644 lab/lab6/Question4/Start.java create mode 100644 lab/lab6/Question5/Circle.java create mode 100644 lab/lab6/Question5/Dot.java create mode 100644 lab/lab6/Question5/ManyShapes.java create mode 100644 lab/lab6/Question5/Rectangle.java create mode 100644 lab/lab6/Question5/Shape.java create mode 100644 lab/lab6/Question5/Square.java create mode 100644 lab/lab6/Question5/Start.java create mode 100644 lab/lab6/Question6/Circle.java create mode 100644 lab/lab6/Question6/Dot.java create mode 100644 lab/lab6/Question6/ManyShapes.java create mode 100644 lab/lab6/Question6/Rectangle.java create mode 100644 lab/lab6/Question6/Shape.java create mode 100644 lab/lab6/Question6/Square.java create mode 100644 lab/lab6/Question6/Start.java create mode 100644 lab/lab6/Question7/Circle.java create mode 100644 lab/lab6/Question7/Dot.java create mode 100644 lab/lab6/Question7/ManyShapes.java create mode 100644 lab/lab6/Question7/Rectangle.java create mode 100644 lab/lab6/Question7/Shape.java create mode 100644 lab/lab6/Question7/Square.java create mode 100644 lab/lab6/Question7/Start.java diff --git a/lab/lab6/Question1/Shape.java b/lab/lab6/Question1/Shape.java new file mode 100644 index 0000000..04af6a0 --- /dev/null +++ b/lab/lab6/Question1/Shape.java @@ -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); + } +} diff --git a/lab/lab6/Question1/Start.java b/lab/lab6/Question1/Start.java new file mode 100644 index 0000000..7afbdb6 --- /dev/null +++ b/lab/lab6/Question1/Start.java @@ -0,0 +1,5 @@ +public class Start { + public static void main(String[] args) { + Shape.testShape(); + } +} diff --git a/lab/lab6/Question2/Circle.java b/lab/lab6/Question2/Circle.java new file mode 100644 index 0000000..f68f061 --- /dev/null +++ b/lab/lab6/Question2/Circle.java @@ -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; + } +} diff --git a/lab/lab6/Question2/Shape.java b/lab/lab6/Question2/Shape.java new file mode 100644 index 0000000..49fa2ba --- /dev/null +++ b/lab/lab6/Question2/Shape.java @@ -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); + } +} diff --git a/lab/lab6/Question2/Start.java b/lab/lab6/Question2/Start.java new file mode 100644 index 0000000..3604213 --- /dev/null +++ b/lab/lab6/Question2/Start.java @@ -0,0 +1,6 @@ +public class Start { + public static void main(String[] args) { + Shape.testShape(); + Circle.testCircle(); + } +} diff --git a/lab/lab6/Question3/Circle.java b/lab/lab6/Question3/Circle.java new file mode 100644 index 0000000..f68f061 --- /dev/null +++ b/lab/lab6/Question3/Circle.java @@ -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; + } +} diff --git a/lab/lab6/Question3/Dot.java b/lab/lab6/Question3/Dot.java new file mode 100644 index 0000000..dc7f20e --- /dev/null +++ b/lab/lab6/Question3/Dot.java @@ -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); + } +} diff --git a/lab/lab6/Question3/Shape.java b/lab/lab6/Question3/Shape.java new file mode 100644 index 0000000..150c62a --- /dev/null +++ b/lab/lab6/Question3/Shape.java @@ -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); + } +} diff --git a/lab/lab6/Question3/Start.java b/lab/lab6/Question3/Start.java new file mode 100644 index 0000000..c05abd7 --- /dev/null +++ b/lab/lab6/Question3/Start.java @@ -0,0 +1,7 @@ +public class Start { + public static void main(String[] args) { + Shape.testShape(); + Circle.testCircle(); + Dot.testDot(); + } +} diff --git a/lab/lab6/Question4/Circle.java b/lab/lab6/Question4/Circle.java new file mode 100644 index 0000000..1d5c8d7 --- /dev/null +++ b/lab/lab6/Question4/Circle.java @@ -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; + } +} diff --git a/lab/lab6/Question4/Dot.java b/lab/lab6/Question4/Dot.java new file mode 100644 index 0000000..dc7f20e --- /dev/null +++ b/lab/lab6/Question4/Dot.java @@ -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); + } +} diff --git a/lab/lab6/Question4/Rectangle.java b/lab/lab6/Question4/Rectangle.java new file mode 100644 index 0000000..f9f6c84 --- /dev/null +++ b/lab/lab6/Question4/Rectangle.java @@ -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); + } +} diff --git a/lab/lab6/Question4/Shape.java b/lab/lab6/Question4/Shape.java new file mode 100644 index 0000000..150c62a --- /dev/null +++ b/lab/lab6/Question4/Shape.java @@ -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); + } +} diff --git a/lab/lab6/Question4/Square.java b/lab/lab6/Question4/Square.java new file mode 100644 index 0000000..6d3351c --- /dev/null +++ b/lab/lab6/Question4/Square.java @@ -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); + } +} diff --git a/lab/lab6/Question4/Start.java b/lab/lab6/Question4/Start.java new file mode 100644 index 0000000..314181b --- /dev/null +++ b/lab/lab6/Question4/Start.java @@ -0,0 +1,9 @@ +public class Start { + public static void main(String[] args) { + Shape.testShape(); + Circle.testCircle(); + Dot.testDot(); + Rectangle.testRectangle(); + Square.testSquare(); + } +} diff --git a/lab/lab6/Question5/Circle.java b/lab/lab6/Question5/Circle.java new file mode 100644 index 0000000..1d5c8d7 --- /dev/null +++ b/lab/lab6/Question5/Circle.java @@ -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; + } +} diff --git a/lab/lab6/Question5/Dot.java b/lab/lab6/Question5/Dot.java new file mode 100644 index 0000000..dc7f20e --- /dev/null +++ b/lab/lab6/Question5/Dot.java @@ -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); + } +} diff --git a/lab/lab6/Question5/ManyShapes.java b/lab/lab6/Question5/ManyShapes.java new file mode 100644 index 0000000..8cc409c --- /dev/null +++ b/lab/lab6/Question5/ManyShapes.java @@ -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 allShapes; + + /** + * Constructor + */ + public ManyShapes() { + this.allShapes = new ArrayList(); + } + + /** + * 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(); + } +} diff --git a/lab/lab6/Question5/Rectangle.java b/lab/lab6/Question5/Rectangle.java new file mode 100644 index 0000000..f9f6c84 --- /dev/null +++ b/lab/lab6/Question5/Rectangle.java @@ -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); + } +} diff --git a/lab/lab6/Question5/Shape.java b/lab/lab6/Question5/Shape.java new file mode 100644 index 0000000..150c62a --- /dev/null +++ b/lab/lab6/Question5/Shape.java @@ -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); + } +} diff --git a/lab/lab6/Question5/Square.java b/lab/lab6/Question5/Square.java new file mode 100644 index 0000000..6d3351c --- /dev/null +++ b/lab/lab6/Question5/Square.java @@ -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); + } +} diff --git a/lab/lab6/Question5/Start.java b/lab/lab6/Question5/Start.java new file mode 100644 index 0000000..5764071 --- /dev/null +++ b/lab/lab6/Question5/Start.java @@ -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(); + } +} diff --git a/lab/lab6/Question6/Circle.java b/lab/lab6/Question6/Circle.java new file mode 100644 index 0000000..1d5c8d7 --- /dev/null +++ b/lab/lab6/Question6/Circle.java @@ -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; + } +} diff --git a/lab/lab6/Question6/Dot.java b/lab/lab6/Question6/Dot.java new file mode 100644 index 0000000..dc7f20e --- /dev/null +++ b/lab/lab6/Question6/Dot.java @@ -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); + } +} diff --git a/lab/lab6/Question6/ManyShapes.java b/lab/lab6/Question6/ManyShapes.java new file mode 100644 index 0000000..7aa7754 --- /dev/null +++ b/lab/lab6/Question6/ManyShapes.java @@ -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 allShapes; + + /** + * Constructor + */ + public ManyShapes() { + this.allShapes = new ArrayList(); + } + + /** + * 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(); + } +} diff --git a/lab/lab6/Question6/Rectangle.java b/lab/lab6/Question6/Rectangle.java new file mode 100644 index 0000000..f9f6c84 --- /dev/null +++ b/lab/lab6/Question6/Rectangle.java @@ -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); + } +} diff --git a/lab/lab6/Question6/Shape.java b/lab/lab6/Question6/Shape.java new file mode 100644 index 0000000..150c62a --- /dev/null +++ b/lab/lab6/Question6/Shape.java @@ -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); + } +} diff --git a/lab/lab6/Question6/Square.java b/lab/lab6/Question6/Square.java new file mode 100644 index 0000000..6d3351c --- /dev/null +++ b/lab/lab6/Question6/Square.java @@ -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); + } +} diff --git a/lab/lab6/Question6/Start.java b/lab/lab6/Question6/Start.java new file mode 100644 index 0000000..5764071 --- /dev/null +++ b/lab/lab6/Question6/Start.java @@ -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(); + } +} diff --git a/lab/lab6/Question7/Circle.java b/lab/lab6/Question7/Circle.java new file mode 100644 index 0000000..24af7f5 --- /dev/null +++ b/lab/lab6/Question7/Circle.java @@ -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(); + } +} diff --git a/lab/lab6/Question7/Dot.java b/lab/lab6/Question7/Dot.java new file mode 100644 index 0000000..0c5aa86 --- /dev/null +++ b/lab/lab6/Question7/Dot.java @@ -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); + } +} diff --git a/lab/lab6/Question7/ManyShapes.java b/lab/lab6/Question7/ManyShapes.java new file mode 100644 index 0000000..6045cd4 --- /dev/null +++ b/lab/lab6/Question7/ManyShapes.java @@ -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 allShapes; + + /** + * Constructor + */ + public ManyShapes() { + this.allShapes = new ArrayList(); + } + + /** + * 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(); + } +} diff --git a/lab/lab6/Question7/Rectangle.java b/lab/lab6/Question7/Rectangle.java new file mode 100644 index 0000000..8854e4f --- /dev/null +++ b/lab/lab6/Question7/Rectangle.java @@ -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); + } +} diff --git a/lab/lab6/Question7/Shape.java b/lab/lab6/Question7/Shape.java new file mode 100644 index 0000000..9f3ddfd --- /dev/null +++ b/lab/lab6/Question7/Shape.java @@ -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); + } +} diff --git a/lab/lab6/Question7/Square.java b/lab/lab6/Question7/Square.java new file mode 100644 index 0000000..66b5393 --- /dev/null +++ b/lab/lab6/Question7/Square.java @@ -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); + } +} diff --git a/lab/lab6/Question7/Start.java b/lab/lab6/Question7/Start.java new file mode 100644 index 0000000..5764071 --- /dev/null +++ b/lab/lab6/Question7/Start.java @@ -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(); + } +}