Add lab6
This commit is contained in:
58
lab/lab6/Question1/Shape.java
Normal file
58
lab/lab6/Question1/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
5
lab/lab6/Question1/Start.java
Normal file
5
lab/lab6/Question1/Start.java
Normal file
@@ -0,0 +1,5 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Shape.testShape();
|
||||
}
|
||||
}
|
||||
51
lab/lab6/Question2/Circle.java
Normal file
51
lab/lab6/Question2/Circle.java
Normal 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;
|
||||
}
|
||||
}
|
||||
59
lab/lab6/Question2/Shape.java
Normal file
59
lab/lab6/Question2/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
6
lab/lab6/Question2/Start.java
Normal file
6
lab/lab6/Question2/Start.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Shape.testShape();
|
||||
Circle.testCircle();
|
||||
}
|
||||
}
|
||||
51
lab/lab6/Question3/Circle.java
Normal file
51
lab/lab6/Question3/Circle.java
Normal 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;
|
||||
}
|
||||
}
|
||||
37
lab/lab6/Question3/Dot.java
Normal file
37
lab/lab6/Question3/Dot.java
Normal 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);
|
||||
}
|
||||
}
|
||||
59
lab/lab6/Question3/Shape.java
Normal file
59
lab/lab6/Question3/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
7
lab/lab6/Question3/Start.java
Normal file
7
lab/lab6/Question3/Start.java
Normal file
@@ -0,0 +1,7 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Shape.testShape();
|
||||
Circle.testCircle();
|
||||
Dot.testDot();
|
||||
}
|
||||
}
|
||||
52
lab/lab6/Question4/Circle.java
Normal file
52
lab/lab6/Question4/Circle.java
Normal 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;
|
||||
}
|
||||
}
|
||||
37
lab/lab6/Question4/Dot.java
Normal file
37
lab/lab6/Question4/Dot.java
Normal 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);
|
||||
}
|
||||
}
|
||||
44
lab/lab6/Question4/Rectangle.java
Normal file
44
lab/lab6/Question4/Rectangle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
59
lab/lab6/Question4/Shape.java
Normal file
59
lab/lab6/Question4/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
28
lab/lab6/Question4/Square.java
Normal file
28
lab/lab6/Question4/Square.java
Normal 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);
|
||||
}
|
||||
}
|
||||
9
lab/lab6/Question4/Start.java
Normal file
9
lab/lab6/Question4/Start.java
Normal file
@@ -0,0 +1,9 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Shape.testShape();
|
||||
Circle.testCircle();
|
||||
Dot.testDot();
|
||||
Rectangle.testRectangle();
|
||||
Square.testSquare();
|
||||
}
|
||||
}
|
||||
52
lab/lab6/Question5/Circle.java
Normal file
52
lab/lab6/Question5/Circle.java
Normal 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;
|
||||
}
|
||||
}
|
||||
37
lab/lab6/Question5/Dot.java
Normal file
37
lab/lab6/Question5/Dot.java
Normal 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);
|
||||
}
|
||||
}
|
||||
56
lab/lab6/Question5/ManyShapes.java
Normal file
56
lab/lab6/Question5/ManyShapes.java
Normal 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();
|
||||
}
|
||||
}
|
||||
44
lab/lab6/Question5/Rectangle.java
Normal file
44
lab/lab6/Question5/Rectangle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
59
lab/lab6/Question5/Shape.java
Normal file
59
lab/lab6/Question5/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
28
lab/lab6/Question5/Square.java
Normal file
28
lab/lab6/Question5/Square.java
Normal 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);
|
||||
}
|
||||
}
|
||||
10
lab/lab6/Question5/Start.java
Normal file
10
lab/lab6/Question5/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
52
lab/lab6/Question6/Circle.java
Normal file
52
lab/lab6/Question6/Circle.java
Normal 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;
|
||||
}
|
||||
}
|
||||
37
lab/lab6/Question6/Dot.java
Normal file
37
lab/lab6/Question6/Dot.java
Normal 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);
|
||||
}
|
||||
}
|
||||
70
lab/lab6/Question6/ManyShapes.java
Normal file
70
lab/lab6/Question6/ManyShapes.java
Normal 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();
|
||||
}
|
||||
}
|
||||
44
lab/lab6/Question6/Rectangle.java
Normal file
44
lab/lab6/Question6/Rectangle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
59
lab/lab6/Question6/Shape.java
Normal file
59
lab/lab6/Question6/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
28
lab/lab6/Question6/Square.java
Normal file
28
lab/lab6/Question6/Square.java
Normal 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);
|
||||
}
|
||||
}
|
||||
10
lab/lab6/Question6/Start.java
Normal file
10
lab/lab6/Question6/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
60
lab/lab6/Question7/Circle.java
Normal file
60
lab/lab6/Question7/Circle.java
Normal 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();
|
||||
}
|
||||
}
|
||||
45
lab/lab6/Question7/Dot.java
Normal file
45
lab/lab6/Question7/Dot.java
Normal 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);
|
||||
}
|
||||
}
|
||||
55
lab/lab6/Question7/ManyShapes.java
Normal file
55
lab/lab6/Question7/ManyShapes.java
Normal 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();
|
||||
}
|
||||
}
|
||||
52
lab/lab6/Question7/Rectangle.java
Normal file
52
lab/lab6/Question7/Rectangle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
66
lab/lab6/Question7/Shape.java
Normal file
66
lab/lab6/Question7/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
36
lab/lab6/Question7/Square.java
Normal file
36
lab/lab6/Question7/Square.java
Normal 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);
|
||||
}
|
||||
}
|
||||
10
lab/lab6/Question7/Start.java
Normal file
10
lab/lab6/Question7/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user