lab8 finished

This commit is contained in:
2022-04-17 11:41:41 +08:00
parent fdb4e8296d
commit cea6585033
43 changed files with 2645 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Circle class inherit from Shape class
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor.
*
* @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;
}
/**
* Calculates the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Resizing a circle cahnges its radius to be newRadius.
*
* @param newRadius
*/
@Override
public void resize(double newRadius) {
radius = newRadius;
}
/**
* Test.
*/
public static void testCircle() {
Circle c1 = new Circle(1.0, 2.0, 3.0);
System.out.println(c1.area() == Math.PI * 9.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
c1.resize(4.0);
System.out.println(c1.area() == Math.PI * 16.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
c1.move(5.0, 6.0);
System.out.println(c1.area() == Math.PI * 16.0);
System.out.println(c1.getX() == 6.0);
System.out.println(c1.getY() == 8.0);
}
}

View File

@@ -0,0 +1,88 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-15
* Description: This is the abstract Shape class.
*/
public abstract class Shape {
/*
* Store the position of the central point of the shape.
*/
private double x;
private double y;
/**
* Constructor of the Shape class.
*
* @param x the x-coordinate of the central point of the shape.
* @param y the y-coordinate of the central point of the shape.
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of the x coordinate of the central point of the shape.
*
* @return the x coordinate of the central point of the shape.
*/
public double getX() {
return x;
}
/**
* Getter of the y coordinate of the central point of the shape.
*
* @return the y coordinate of the central point of the shape.
*/
public double getY() {
return y;
}
/**
* Move the central point of the shape by amounts dx and dy.
*
* @param dx the amount of movement in x direction.
* @param dy the amount of movement in y direction.
*/
public void move(double dx, double dy) {
x += dx;
y += dy;
}
/**
* Abstract method to calculate the area of the shape.
*
* @return the area of the shape.
*/
public abstract double area();
/**
* Abstract method to resize the shape.
*
* @param newSize the new size of the shape.
*/
public abstract void resize(double newSize);
/**
* Test.
*/
public static void testShape() {
Shape s = new Shape(3.9, 4.2) {
@Override
public double area() {
return 0;
}
@Override
public void resize(double newSize) {
}
};
System.out.println(s.getX() == 3.9);
System.out.println(s.getY() == 4.2);
s.move(1.0, 2.0);
System.out.println(s.getX() == 4.9);
System.out.println(s.getY() == 6.2);
}
}

View File

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

View File

@@ -0,0 +1,59 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Circle class inherit from Shape class
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor.
*
* @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;
}
/**
* Calculates the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Resizing a circle cahnges its radius to be newRadius.
*
* @param newRadius
*/
@Override
public void resize(double newRadius) {
radius = newRadius;
}
/**
* Test.
*/
public static void testCircle() {
Circle c1 = new Circle(1.0, 2.0, 3.0);
System.out.println(c1.area() == Math.PI * 9.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
c1.resize(4.0);
System.out.println(c1.area() == Math.PI * 16.0);
System.out.println(c1.getX() == 1.0);
System.out.println(c1.getY() == 2.0);
c1.move(5.0, 6.0);
System.out.println(c1.area() == Math.PI * 16.0);
System.out.println(c1.getX() == 6.0);
System.out.println(c1.getY() == 8.0);
}
}

View File

@@ -0,0 +1,64 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: This is the Dot class inherited from Shape class.
*/
public class Dot extends Shape {
/**
* Constructor.
*
* @param x the x coordinate of the dot
* @param y the y coordinate of the dot
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Calculates the dot's area.
*
* @return the area of the dot
*/
public double area() {
return 0;
}
/**
* Resize method of Dot throw an Exception with message "Cannot resize a dot!"
*
* @param newSize the new size of the dot
* @throws Exception the exception
*/
@Override
public void resize(double newSize) throws Exception {
throw new Exception("Cannot resize a dot!");
}
/**
* Test.
*/
public static void testDot() {
Dot d = new Dot(1.2, 3.4);
// getX, getY, and move are inherited from Shape.
// area and resize come from Dot itself.
System.out.println(d.getX() == 1.2);
System.out.println(d.getY() == 3.4);
System.out.println(d.area() == 0.0);
// Move the dot. The area does not change.
d.move(7.8, 9.0);
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
// Resize the dot. An exception is thrown, caught, and tested.
try {
d.resize(12.3);
} catch (Exception ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
// The area and position do not change.
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
}
}

View File

@@ -0,0 +1,89 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-15
* Description: This is the abstract Shape class.
*/
public abstract class Shape {
/*
* Store the position of the central point of the shape.
*/
private double x;
private double y;
/**
* Constructor of the Shape class.
*
* @param x the x-coordinate of the central point of the shape.
* @param y the y-coordinate of the central point of the shape.
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of the x coordinate of the central point of the shape.
*
* @return the x coordinate of the central point of the shape.
*/
public double getX() {
return x;
}
/**
* Getter of the y coordinate of the central point of the shape.
*
* @return the y coordinate of the central point of the shape.
*/
public double getY() {
return y;
}
/**
* Move the central point of the shape by amounts dx and dy.
*
* @param dx the amount of movement in x direction.
* @param dy the amount of movement in y direction.
*/
public void move(double dx, double dy) {
x += dx;
y += dy;
}
/**
* Abstract method to calculate the area of the shape.
*
* @return the area of the shape.
*/
public abstract double area();
/**
* Abstract method to resize the shape.
*
* @param newSize the new size of the shape.
* @throws Exception
*/
public abstract void resize(double newSize) throws Exception;
/**
* Test.
*/
public static void testShape() {
Shape s = new Shape(3.9, 4.2) {
@Override
public double area() {
return 0;
}
@Override
public void resize(double newSize) {
}
};
System.out.println(s.getX() == 3.9);
System.out.println(s.getY() == 4.2);
s.move(1.0, 2.0);
System.out.println(s.getX() == 4.9);
System.out.println(s.getY() == 6.2);
}
}

View File

@@ -0,0 +1,24 @@
/**
* Answers the question.
*
* Question3:
*
* - Q: What is the problem with the resize method of the Shape calss? How do
* you solve this problem?
*
* - A: The resize method of the Dot class is inherited from the Shape class.
* which does not have a throw Exception declared. We can solve this problem by
* declaring the resize method in the Shape class as a throw Exception.
*
* - Q: Do you need to change the resize method of the Circle class then?
*
* - A: No, we do not need to change the resize method of the Circle class.
*/
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
}
}

View File

@@ -0,0 +1,19 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description:
* CannotResizeException is a subclass of Exception.
*/
public class CannotResizeException extends Exception {
// Generate serialVersionUID by class name
public static final long serialVersionUID = CannotResizeException.class.getName().hashCode();
public CannotResizeException() {
super();
}
public CannotResizeException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,63 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Circle class inherit from Shape class
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor.
*
* @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;
}
/**
* Calculates the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Resizing a circle cahnges its radius to be newRadius.
*
* @param newRadius
*/
@Override
public void resize(double newRadius) {
radius = newRadius;
}
/**
* Test.
*/
public static void testCircle() {
Circle c = new Circle(1.2, 3.4, 4.0);
// getX, getY, and move are inherited from Shape.
// area and resize come from Circle itself.
System.out.println(c.getX() == 1.2);
System.out.println(c.getY() == 3.4);
System.out.println(c.area() == Math.PI * 16.0);
// Move the circle. The area does not change.
c.move(7.8, 9.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 16.0);
// Resize the circle. The area changes but not the position.
c.resize(8.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 64.0);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: This is the Dot class inherited from Shape class.
*/
public class Dot extends Shape {
/**
* Constructor.
*
* @param x the x coordinate of the dot
* @param y the y coordinate of the dot
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Calculates the dot's area.
*
* @return the area of the dot
*/
public double area() {
return 0;
}
/**
* Resize method of Dot throw an CannotResizeException with message "Cannot
* resize a dot!"
*
* @param newSize the new size of the dot
* @throws CannotResizeException the exception
*/
@Override
public void resize(double newSize) throws CannotResizeException {
throw new CannotResizeException("Cannot resize a dot!");
}
/**
* Test.
*/
public static void testDot() {
Dot d = new Dot(1.2, 3.4);
// getX, getY, and move are inherited from Shape.
// area and resize come from Dot itself.
System.out.println(d.getX() == 1.2);
System.out.println(d.getY() == 3.4);
System.out.println(d.area() == 0.0);
// Move the dot. The area does not change.
d.move(7.8, 9.0);
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
// Resize the dot. An exception is thrown, caught, and tested.
try {
d.resize(12.3);
} catch (Exception ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
// The area and position do not change.
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
try {
d.resize(12.3);
} catch (CannotResizeException ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
}
}

View File

@@ -0,0 +1,89 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-15
* Description: This is the abstract Shape class.
*/
public abstract class Shape {
/*
* Store the position of the central point of the shape.
*/
private double x;
private double y;
/**
* Constructor of the Shape class.
*
* @param x the x-coordinate of the central point of the shape.
* @param y the y-coordinate of the central point of the shape.
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of the x coordinate of the central point of the shape.
*
* @return the x coordinate of the central point of the shape.
*/
public double getX() {
return x;
}
/**
* Getter of the y coordinate of the central point of the shape.
*
* @return the y coordinate of the central point of the shape.
*/
public double getY() {
return y;
}
/**
* Move the central point of the shape by amounts dx and dy.
*
* @param dx the amount of movement in x direction.
* @param dy the amount of movement in y direction.
*/
public void move(double dx, double dy) {
x += dx;
y += dy;
}
/**
* Abstract method to calculate the area of the shape.
*
* @return the area of the shape.
*/
public abstract double area();
/**
* Abstract method to resize the shape.
*
* @param newSize the new size of the shape.
* @throws CannotResizeException
*/
public abstract void resize(double newSize) throws CannotResizeException;
/**
* Test.
*/
public static void testShape() {
Shape s = new Shape(3.9, 4.2) {
@Override
public double area() {
return 0;
}
@Override
public void resize(double newSize) {
}
};
System.out.println(s.getX() == 3.9);
System.out.println(s.getY() == 4.2);
s.move(1.0, 2.0);
System.out.println(s.getX() == 4.9);
System.out.println(s.getY() == 6.2);
}
}

View File

@@ -0,0 +1,24 @@
/**
* Answers the question.
*
* Question3:
*
* - Q: What is the problem with the resize method of the Shape calss? How do
* you solve this problem?
*
* - A: The resize method of the Dot class is inherited from the Shape class.
* which does not have a throw Exception declared. We can solve this problem by
* declaring the resize method in the Shape class as a throw Exception.
*
* - Q: Do you need to change the resize method of the Circle class then?
*
* - A: No, we do not need to change the resize method of the Circle class.
*/
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
}
}

View File

@@ -0,0 +1,19 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description:
* CannotResizeException is a subclass of Exception.
*/
public class CannotResizeException extends Exception {
// Generate serialVersionUID by class name
public static final long serialVersionUID = CannotResizeException.class.getName().hashCode();
public CannotResizeException() {
super();
}
public CannotResizeException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,63 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Circle class inherit from Shape class
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor.
*
* @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;
}
/**
* Calculates the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Resizing a circle cahnges its radius to be newRadius.
*
* @param newRadius
*/
@Override
public void resize(double newRadius) {
radius = newRadius;
}
/**
* Test.
*/
public static void testCircle() {
Circle c = new Circle(1.2, 3.4, 4.0);
// getX, getY, and move are inherited from Shape.
// area and resize come from Circle itself.
System.out.println(c.getX() == 1.2);
System.out.println(c.getY() == 3.4);
System.out.println(c.area() == Math.PI * 16.0);
// Move the circle. The area does not change.
c.move(7.8, 9.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 16.0);
// Resize the circle. The area changes but not the position.
c.resize(8.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 64.0);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: This is the Dot class inherited from Shape class.
*/
public class Dot extends Shape {
/**
* Constructor.
*
* @param x the x coordinate of the dot
* @param y the y coordinate of the dot
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Calculates the dot's area.
*
* @return the area of the dot
*/
public double area() {
return 0;
}
/**
* Resize method of Dot throw an CannotResizeException with message "Cannot
* resize a dot!"
*
* @param newSize the new size of the dot
* @throws CannotResizeException the exception
*/
@Override
public void resize(double newSize) throws CannotResizeException {
throw new CannotResizeException("Cannot resize a dot!");
}
/**
* Test.
*/
public static void testDot() {
Dot d = new Dot(1.2, 3.4);
// getX, getY, and move are inherited from Shape.
// area and resize come from Dot itself.
System.out.println(d.getX() == 1.2);
System.out.println(d.getY() == 3.4);
System.out.println(d.area() == 0.0);
// Move the dot. The area does not change.
d.move(7.8, 9.0);
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
// Resize the dot. An exception is thrown, caught, and tested.
try {
d.resize(12.3);
} catch (Exception ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
// The area and position do not change.
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
try {
d.resize(12.3);
} catch (CannotResizeException ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
}
}

View File

@@ -0,0 +1,67 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Rectangle class inherited from Shape class
*/
public class Rectangle extends Shape {
private double width;
private double length;
/**
* Constructor
*
* @param x x coordinate of the center of the rectangle
* @param y y coordinate of the center of the rectangle
* @param width width of the rectangle
* @param length length of the rectangle
*/
public Rectangle(double x, double y, double width, double length) {
super(x, y);
this.width = width;
this.length = length;
}
/**
* Calculates the area of the rectangle.
*
* @return the area of the rectangle
*/
@Override
public double area() {
return width * length;
}
/**
* Resizing a rectangle changes its width and length to both be newSize.
*
* @param newSize the new size of the rectangle
*/
@Override
public void resize(double newSize) {
width = newSize;
length = newSize;
}
/**
* Test.
*/
public static void testRectangle() {
Rectangle r = new Rectangle(1.2, 3.4, 4.0, 5.0);
// getX, getY, and move are inherited from Shape.
// area and resize come from Rectangle itself.
System.out.println(r.getX() == 1.2);
System.out.println(r.getY() == 3.4);
System.out.println(r.area() == 20.0);
// Move the rectangle. The area does not change.
r.move(7.8, 9.0);
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 20.0);
// Resize the rectangle. The area changes but not the position.
r.resize(12.0);
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 144.0);
}
}

View File

@@ -0,0 +1,89 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-15
* Description: This is the abstract Shape class.
*/
public abstract class Shape {
/*
* Store the position of the central point of the shape.
*/
private double x;
private double y;
/**
* Constructor of the Shape class.
*
* @param x the x-coordinate of the central point of the shape.
* @param y the y-coordinate of the central point of the shape.
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of the x coordinate of the central point of the shape.
*
* @return the x coordinate of the central point of the shape.
*/
public double getX() {
return x;
}
/**
* Getter of the y coordinate of the central point of the shape.
*
* @return the y coordinate of the central point of the shape.
*/
public double getY() {
return y;
}
/**
* Move the central point of the shape by amounts dx and dy.
*
* @param dx the amount of movement in x direction.
* @param dy the amount of movement in y direction.
*/
public void move(double dx, double dy) {
x += dx;
y += dy;
}
/**
* Abstract method to calculate the area of the shape.
*
* @return the area of the shape.
*/
public abstract double area();
/**
* Abstract method to resize the shape.
*
* @param newSize the new size of the shape.
* @throws CannotResizeException
*/
public abstract void resize(double newSize) throws CannotResizeException;
/**
* Test.
*/
public static void testShape() {
Shape s = new Shape(3.9, 4.2) {
@Override
public double area() {
return 0;
}
@Override
public void resize(double newSize) {
}
};
System.out.println(s.getX() == 3.9);
System.out.println(s.getY() == 4.2);
s.move(1.0, 2.0);
System.out.println(s.getX() == 4.9);
System.out.println(s.getY() == 6.2);
}
}

View File

@@ -0,0 +1,40 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Square class inherited from Rectangle class
*/
public class Square extends Rectangle {
/**
* Constructor.
*
* @param x the x coordinate center of the square
* @param y the y coordinate center of the square
* @param size the size of the square
*/
public Square(double x, double y, double size) {
super(x, y, size, size);
}
/**
* Test.
*/
public static void testSquare() {
Square s = new Square(1.2, 3.4, 5.0);
// getX, getY, and move are inherited from Shape.
// area and resize are inherited from Rectangle.
System.out.println(s.getX() == 1.2);
System.out.println(s.getY() == 3.4);
System.out.println(s.area() == 25.0);
// Move the square. The area does not change.
s.move(7.8, 9.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 25.0);
// Resize the square. The area changes but not the position.
s.resize(12.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 144.0);
}
}

View File

@@ -0,0 +1,33 @@
/**
* Answers the question.
*
* Question3:
*
* - Q: What is the problem with the resize method of the Shape calss? How do
* you solve this problem?
*
* - A: The resize method of the Dot class is inherited from the Shape class.
* which does not have a throw Exception declared. We can solve this problem by
* declaring the resize method in the Shape class as a throw Exception.
*
* - Q: Do you need to change the resize method of the Circle class then?
*
* - A: No, we do not need to change the resize method of the Circle class.
*
* - Q: Does the Square class need its own area and resize method or not?
*
* - A: No, but it depends. Currently the Square.area and Square.resize methods
* return the correct values. But in the future, if we want to change the
* Rectangle.area and Rectangle.resize methods, the Square class may give the
* wrong values. So it is better to have its own area and resize methods.
*/
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
Rectangle.testRectangle();
Square.testSquare();
}
}

View File

@@ -0,0 +1,19 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description:
* CannotResizeException is a subclass of Exception.
*/
public class CannotResizeException extends Exception {
// Generate serialVersionUID by class name
public static final long serialVersionUID = CannotResizeException.class.getName().hashCode();
public CannotResizeException() {
super();
}
public CannotResizeException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,63 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Circle class inherit from Shape class
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor.
*
* @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;
}
/**
* Calculates the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Resizing a circle cahnges its radius to be newRadius.
*
* @param newRadius
*/
@Override
public void resize(double newRadius) {
radius = newRadius;
}
/**
* Test.
*/
public static void testCircle() {
Circle c = new Circle(1.2, 3.4, 4.0);
// getX, getY, and move are inherited from Shape.
// area and resize come from Circle itself.
System.out.println(c.getX() == 1.2);
System.out.println(c.getY() == 3.4);
System.out.println(c.area() == Math.PI * 16.0);
// Move the circle. The area does not change.
c.move(7.8, 9.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 16.0);
// Resize the circle. The area changes but not the position.
c.resize(8.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 64.0);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: This is the Dot class inherited from Shape class.
*/
public class Dot extends Shape {
/**
* Constructor.
*
* @param x the x coordinate of the dot
* @param y the y coordinate of the dot
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Calculates the dot's area.
*
* @return the area of the dot
*/
public double area() {
return 0;
}
/**
* Resize method of Dot throw an CannotResizeException with message "Cannot
* resize a dot!"
*
* @param newSize the new size of the dot
* @throws CannotResizeException the exception
*/
@Override
public void resize(double newSize) throws CannotResizeException {
throw new CannotResizeException("Cannot resize a dot!");
}
/**
* Test.
*/
public static void testDot() {
Dot d = new Dot(1.2, 3.4);
// getX, getY, and move are inherited from Shape.
// area and resize come from Dot itself.
System.out.println(d.getX() == 1.2);
System.out.println(d.getY() == 3.4);
System.out.println(d.area() == 0.0);
// Move the dot. The area does not change.
d.move(7.8, 9.0);
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
// Resize the dot. An exception is thrown, caught, and tested.
try {
d.resize(12.3);
} catch (Exception ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
// The area and position do not change.
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
try {
d.resize(12.3);
} catch (CannotResizeException ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
}
}

View File

@@ -0,0 +1,86 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Rectangle class inherited from Shape class
*/
public class Rectangle extends Shape {
private double width;
private double length;
/**
* Constructor
*
* @param x x coordinate of the center of the rectangle
* @param y y coordinate of the center of the rectangle
* @param width width of the rectangle
* @param length length of the rectangle
*/
public Rectangle(double x, double y, double width, double length) {
super(x, y);
this.width = width;
this.length = length;
}
/**
* Calculates the area of the rectangle.
*
* @return the area of the rectangle
*/
@Override
public double area() {
return width * length;
}
/**
* Resizing a rectangle changes its width and length to both be newSize.
*
* @param newSize the new size of the rectangle
*/
@Override
public void resize(double newSize) {
width = newSize;
length = newSize;
}
/**
* Overloading resizeing a nectangle changes its width and length to both be
* newWidth and newLength.
*
* @param newWidth the new width of the rectangle
* @param newLength the new length of the rectangle
*/
public void resize(double newWidth, double newLength) {
width = newWidth;
length = newLength;
}
/**
* Test.
*/
public static void testRectangle() {
Rectangle r = new Rectangle(1.2, 3.4, 4.0, 5.0);
// getX, getY, and move are inherited from Shape.
// area and resize come from Rectangle itself.
System.out.println(r.getX() == 1.2);
System.out.println(r.getY() == 3.4);
System.out.println(r.area() == 20.0);
// Move the rectangle. The area does not change.
r.move(7.8, 9.0);
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 20.0);
// Resize the rectangle. The area changes but not the position.
r.resize(12.0);
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 144.0);
// Resize the rectangle again with different width and length.
// The area changes but not the position.
r.resize(10.0, 15.0);
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 150.0);
}
}

View File

@@ -0,0 +1,89 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-15
* Description: This is the abstract Shape class.
*/
public abstract class Shape {
/*
* Store the position of the central point of the shape.
*/
private double x;
private double y;
/**
* Constructor of the Shape class.
*
* @param x the x-coordinate of the central point of the shape.
* @param y the y-coordinate of the central point of the shape.
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of the x coordinate of the central point of the shape.
*
* @return the x coordinate of the central point of the shape.
*/
public double getX() {
return x;
}
/**
* Getter of the y coordinate of the central point of the shape.
*
* @return the y coordinate of the central point of the shape.
*/
public double getY() {
return y;
}
/**
* Move the central point of the shape by amounts dx and dy.
*
* @param dx the amount of movement in x direction.
* @param dy the amount of movement in y direction.
*/
public void move(double dx, double dy) {
x += dx;
y += dy;
}
/**
* Abstract method to calculate the area of the shape.
*
* @return the area of the shape.
*/
public abstract double area();
/**
* Abstract method to resize the shape.
*
* @param newSize the new size of the shape.
* @throws CannotResizeException
*/
public abstract void resize(double newSize) throws CannotResizeException;
/**
* Test.
*/
public static void testShape() {
Shape s = new Shape(3.9, 4.2) {
@Override
public double area() {
return 0;
}
@Override
public void resize(double newSize) {
}
};
System.out.println(s.getX() == 3.9);
System.out.println(s.getY() == 4.2);
s.move(1.0, 2.0);
System.out.println(s.getX() == 4.9);
System.out.println(s.getY() == 6.2);
}
}

View File

@@ -0,0 +1,49 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Square class inherited from Rectangle class
*/
public class Square extends Rectangle {
/**
* Constructor.
*
* @param x the x coordinate center of the square
* @param y the y coordinate center of the square
* @param size the size of the square
*/
public Square(double x, double y, double size) {
super(x, y, size, size);
}
/**
* Test.
*/
public static void testSquare() {
Square s = new Square(1.2, 3.4, 5.0);
// getX, getY, and move are inherited from Shape.
// area and resize are inherited from Rectangle.
System.out.println(s.getX() == 1.2);
System.out.println(s.getY() == 3.4);
System.out.println(s.area() == 25.0);
// Move the square. The area does not change.
s.move(7.8, 9.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 25.0);
// Resize the square. The area changes but not the position.
s.resize(12.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 144.0);
// Resize the square again with different width and length!
// Now the square is not square anymore!
// The area changes but not the position.
s.resize(10.0, 15.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 150.0);
}
}

View File

@@ -0,0 +1,39 @@
/**
* Answers the question.
*
* Question3:
*
* - Q: What is the problem with the resize method of the Shape calss? How do
* you solve this problem?
*
* - A: The resize method of the Dot class is inherited from the Shape class.
* which does not have a throw Exception declared. We can solve this problem by
* declaring the resize method in the Shape class as a throw Exception.
*
* - Q: Do you need to change the resize method of the Circle class then?
*
* - A: No, we do not need to change the resize method of the Circle class.
*
* - Q: Does the Square class need its own area and resize method or not?
*
* - A: No, but it depends. Currently the Square.area and Square.resize methods
* return the correct values. But in the future, if we want to change the
* Rectangle.area and Rectangle.resize methods, the Square class may give the
* wrong values. So it is better to have its own area and resize methods.
*
* - Q: What is then the problem for the Square class?
*
* - A: No problem. The area method is overloaded. But the resize method in
* Square can set different values for the width and height. So it is better to
* have its own resize methods.
*/
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
Rectangle.testRectangle();
Square.testSquare();
}
}

View File

@@ -0,0 +1,19 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description:
* CannotResizeException is a subclass of Exception.
*/
public class CannotResizeException extends Exception {
// Generate serialVersionUID by class name
public static final long serialVersionUID = CannotResizeException.class.getName().hashCode();
public CannotResizeException() {
super();
}
public CannotResizeException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,63 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Circle class inherit from Shape class
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor.
*
* @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;
}
/**
* Calculates the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Resizing a circle cahnges its radius to be newRadius.
*
* @param newRadius
*/
@Override
public void resize(double newRadius) {
radius = newRadius;
}
/**
* Test.
*/
public static void testCircle() {
Circle c = new Circle(1.2, 3.4, 4.0);
// getX, getY, and move are inherited from Shape.
// area and resize come from Circle itself.
System.out.println(c.getX() == 1.2);
System.out.println(c.getY() == 3.4);
System.out.println(c.area() == Math.PI * 16.0);
// Move the circle. The area does not change.
c.move(7.8, 9.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 16.0);
// Resize the circle. The area changes but not the position.
c.resize(8.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 64.0);
}
}

View File

@@ -0,0 +1,72 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: This is the Dot class inherited from Shape class.
*/
public class Dot extends Shape {
/**
* Constructor.
*
* @param x the x coordinate of the dot
* @param y the y coordinate of the dot
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Calculates the dot's area.
*
* @return the area of the dot
*/
public double area() {
return 0;
}
/**
* Resize method of Dot throw an CannotResizeException with message "Cannot
* resize a dot!"
*
* @param newSize the new size of the dot
* @throws CannotResizeException the exception
*/
@Override
public void resize(double newSize) throws CannotResizeException {
throw new CannotResizeException("Cannot resize a dot!");
}
/**
* Test.
*/
public static void testDot() {
Dot d = new Dot(1.2, 3.4);
// getX, getY, and move are inherited from Shape.
// area and resize come from Dot itself.
System.out.println(d.getX() == 1.2);
System.out.println(d.getY() == 3.4);
System.out.println(d.area() == 0.0);
// Move the dot. The area does not change.
d.move(7.8, 9.0);
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
// Resize the dot. An exception is thrown, caught, and tested.
try {
d.resize(12.3);
} catch (Exception ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
// The area and position do not change.
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
try {
d.resize(12.3);
} catch (CannotResizeException ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
}
}

View File

@@ -0,0 +1,93 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Rectangle class inherited from Shape class
*/
public class Rectangle extends Shape {
private double width;
private double length;
/**
* Constructor
*
* @param x x coordinate of the center of the rectangle
* @param y y coordinate of the center of the rectangle
* @param width width of the rectangle
* @param length length of the rectangle
*/
public Rectangle(double x, double y, double width, double length) {
super(x, y);
this.width = width;
this.length = length;
}
/**
* Calculates the area of the rectangle.
*
* @return the area of the rectangle
*/
@Override
public double area() {
return width * length;
}
/**
* Resizing a rectangle changes its width and length to both be newSize.
*
* @param newSize the new size of the rectangle
* @throws CannotResizeException
*/
@Override
public void resize(double newSize) {
width = newSize;
length = newSize;
}
/**
* Overloading resizeing a nectangle changes its width and length to both be
* newWidth and newLength.
*
* @param newWidth the new width of the rectangle
* @param newLength the new length of the rectangle
* @throws CannotResizeException
*/
public void resize(double newWidth, double newLength) throws CannotResizeException {
width = newWidth;
length = newLength;
}
/**
* Test.
*/
public static void testRectangle() {
Rectangle r = new Rectangle(1.2, 3.4, 4.0, 5.0);
// getX, getY, and move are inherited from Shape.
// area and resize come from Rectangle itself.
System.out.println(r.getX() == 1.2);
System.out.println(r.getY() == 3.4);
System.out.println(r.area() == 20.0);
// Move the rectangle. The area does not change.
r.move(7.8, 9.0);
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 20.0);
// Resize the rectangle. The area changes but not the position.
r.resize(12.0);
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 144.0);
// Resize the rectangle again with different width and length.
// The area changes but not the position.
try {
r.resize(10.0, 15.0);
} catch (CannotResizeException ex) {
System.out.println("BUG! This must never happen!");
}
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 150.0);
}
}

View File

@@ -0,0 +1,89 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-15
* Description: This is the abstract Shape class.
*/
public abstract class Shape {
/*
* Store the position of the central point of the shape.
*/
private double x;
private double y;
/**
* Constructor of the Shape class.
*
* @param x the x-coordinate of the central point of the shape.
* @param y the y-coordinate of the central point of the shape.
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of the x coordinate of the central point of the shape.
*
* @return the x coordinate of the central point of the shape.
*/
public double getX() {
return x;
}
/**
* Getter of the y coordinate of the central point of the shape.
*
* @return the y coordinate of the central point of the shape.
*/
public double getY() {
return y;
}
/**
* Move the central point of the shape by amounts dx and dy.
*
* @param dx the amount of movement in x direction.
* @param dy the amount of movement in y direction.
*/
public void move(double dx, double dy) {
x += dx;
y += dy;
}
/**
* Abstract method to calculate the area of the shape.
*
* @return the area of the shape.
*/
public abstract double area();
/**
* Abstract method to resize the shape.
*
* @param newSize the new size of the shape.
* @throws CannotResizeException
*/
public abstract void resize(double newSize) throws CannotResizeException;
/**
* Test.
*/
public static void testShape() {
Shape s = new Shape(3.9, 4.2) {
@Override
public double area() {
return 0;
}
@Override
public void resize(double newSize) {
}
};
System.out.println(s.getX() == 3.9);
System.out.println(s.getY() == 4.2);
s.move(1.0, 2.0);
System.out.println(s.getX() == 4.9);
System.out.println(s.getY() == 6.2);
}
}

View File

@@ -0,0 +1,80 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Square class inherited from Rectangle class
*/
public class Square extends Rectangle {
/**
* Constructor.
*
* @param x the x coordinate center of the square
* @param y the y coordinate center of the square
* @param size the size of the square
*/
public Square(double x, double y, double size) {
super(x, y, size, size);
}
/**
* Override the resize(double double) method check width == height.
*
* @param width the new width of the square
* @param length the new length of the square
*/
@Override
public void resize(double width, double length) throws CannotResizeException {
if (width != length) {
throw new CannotResizeException("Cannot resize a square into a rectangle!");
}
super.resize(width, length);
}
/**
* Test.
*/
public static void testSquare() {
Square s = new Square(1.2, 3.4, 5.0);
// getX, getY, and move are inherited from Shape.
// area and resize are inherited from Rectangle.
System.out.println(s.getX() == 1.2);
System.out.println(s.getY() == 3.4);
System.out.println(s.area() == 25.0);
// Move the square. The area does not change.
s.move(7.8, 9.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 25.0);
// Resize the square. The area changes but not the position.
s.resize(12.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 144.0);
// Resize the square again with different width and length!
try {
s.resize(10.0, 15.0);
System.out.println(s.getX() == 9.0); // Unreachable.
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 150.0);
} catch (CannotResizeException ex) {
System.out.println(ex.getMessage() == "Cannot resize a square into a rectangle!");
}
// The area and position do not change.
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 144.0);
// Resize the square again with equal width and length.
// The area changes but not the position.
try {
s.resize(10.0, 10.0);
} catch (CannotResizeException ex) {
System.out.println("BUG! This must never happen!");
}
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 100.0);
}
}

View File

@@ -0,0 +1,45 @@
/**
* Answers the question.
*
* Question3:
*
* - Q: What is the problem with the resize method of the Shape calss? How do
* you solve this problem?
*
* - A: The resize method of the Dot class is inherited from the Shape class.
* which does not have a throw Exception declared. We can solve this problem by
* declaring the resize method in the Shape class as a throw Exception.
*
* - Q: Do you need to change the resize method of the Circle class then?
*
* - A: No, we do not need to change the resize method of the Circle class.
*
* - Q: Does the Square class need its own area and resize method or not?
*
* - A: No, but it depends. Currently the Square.area and Square.resize methods
* return the correct values. But in the future, if we want to change the
* Rectangle.area and Rectangle.resize methods, the Square class may give the
* wrong values. So it is better to have its own area and resize methods.
*
* - Q: What is then the problem for the Square class?
*
* - A: No problem. The area method is overloaded. But the resize method in
* Square can set different values for the width and height. So it is better to
* have its own resize methods.
*
* - Q: What happends with the resize method of the Rectangle class?
*
* - A: The resize method of the Square class is inherited from the Rectangle
* class. So we need to add the throws Exception to the resize method of the
* Square class.
*/
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
Rectangle.testRectangle();
Square.testSquare();
}
}

View File

@@ -0,0 +1,19 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description:
* This is the BadRadiusException class.
*/
public class BadRadiusException extends Exception {
// Generate serialVersionUID by class name
private static final long serialVersionUID = BadRadiusException.class.getName().hashCode();
public BadRadiusException() {
super();
}
public BadRadiusException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,19 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description:
* CannotResizeException is a subclass of Exception.
*/
public class CannotResizeException extends Exception {
// Generate serialVersionUID by class name
public static final long serialVersionUID = CannotResizeException.class.getName().hashCode();
public CannotResizeException() {
super();
}
public CannotResizeException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,115 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Circle class inherit from Shape class
*/
public class Circle extends Shape {
private double radius;
/**
* Constructor.
*
* @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, must be positive
* @throws BadRadiusException if the radius is negative
*/
public Circle(double x, double y, double radius) throws BadRadiusException {
super(x, y);
this.radius = radius;
if (radius < 0) {
throw new BadRadiusException("Radius must be positive!");
}
}
/**
* Calculates the area of the circle.
*
* @return the area of the circle
*/
@Override
public double area() {
return Math.PI * radius * radius;
}
/**
* Resizing a circle cahnges its radius to be newRadius.
*
* @param newRadius
*/
@Override
public void resize(double newRadius) throws BadRadiusException {
if (newRadius < 0) {
throw new BadRadiusException("Radius must be positive!");
}
radius = newRadius;
}
/**
* Test.
*/
public static void testCircle() {
// Create a circle with a positive radius.
// No exception is thrown until we resize with a
// negative radius.
try {
Circle c = new Circle(1.2, 3.4, 4.0);
// getX, getY, and move are inherited from Shape.
// area and resize come from Circle itself.
System.out.println(c.getX() == 1.2);
System.out.println(c.getY() == 3.4);
System.out.println(c.area() == Math.PI * 16.0);
// Move the circle. The area does not change.
c.move(7.8, 9.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 16.0);
// Resize the circle. The area changes but not the position.
c.resize(8.0);
System.out.println(c.getX() == 9.0);
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 64.0);
// Resize the circle with a negative radius.
// An exception is thrown, caught, and tested.
c.resize(-12.3);
System.out.println(c.getX() == 9.0); // Unreachable.
System.out.println(c.getY() == 12.4);
System.out.println(c.area() == Math.PI * 64.0);
} catch (BadRadiusException ex) {
System.out.println(ex.getMessage() == "Radius must be positive!");
}
// Create a circle with a positive radius.
// Resize the circle with a zero radius.
// No exception is thrown.
try {
Circle c = new Circle(1.2, 3.4, 4.0);
c.resize(0.0);
// The area is now zero, the position does not change.
System.out.println(c.getX() == 1.2);
System.out.println(c.getY() == 3.4);
System.out.println(c.area() == 0.0);
} catch (BadRadiusException ex) {
System.out.println("BUG! This must never happen!");
}
// Try to create a circle with a negative radius.
// An exception is thrown, caught, and tested.
try {
Circle c2 = new Circle(1.2, 3.4, -5.6);
} catch (BadRadiusException ex) {
System.out.println(ex.getMessage() == "Radius must be positive!");
}
// Create a circle with a zero radius.
// No exception is thrown.
try {
Circle c3 = new Circle(1.2, 3.4, 0.0);
System.out.println(c3.getX() == 1.2);
System.out.println(c3.getY() == 3.4);
System.out.println(c3.area() == 0.0);
} catch (BadRadiusException ex) {
System.out.println("BUG! This must never happen!");
}
}
}

View File

@@ -0,0 +1,72 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: This is the Dot class inherited from Shape class.
*/
public class Dot extends Shape {
/**
* Constructor.
*
* @param x the x coordinate of the dot
* @param y the y coordinate of the dot
*/
public Dot(double x, double y) {
super(x, y);
}
/**
* Calculates the dot's area.
*
* @return the area of the dot
*/
public double area() {
return 0;
}
/**
* Resize method of Dot throw an CannotResizeException with message "Cannot
* resize a dot!"
*
* @param newSize the new size of the dot
* @throws CannotResizeException the exception
*/
@Override
public void resize(double newSize) throws CannotResizeException {
throw new CannotResizeException("Cannot resize a dot!");
}
/**
* Test.
*/
public static void testDot() {
Dot d = new Dot(1.2, 3.4);
// getX, getY, and move are inherited from Shape.
// area and resize come from Dot itself.
System.out.println(d.getX() == 1.2);
System.out.println(d.getY() == 3.4);
System.out.println(d.area() == 0.0);
// Move the dot. The area does not change.
d.move(7.8, 9.0);
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
// Resize the dot. An exception is thrown, caught, and tested.
try {
d.resize(12.3);
} catch (Exception ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
// The area and position do not change.
System.out.println(d.getX() == 9.0);
System.out.println(d.getY() == 12.4);
System.out.println(d.area() == 0.0);
try {
d.resize(12.3);
} catch (CannotResizeException ex) {
System.out.println(ex.getMessage() == "Cannot resize a dot!");
}
}
}

View File

@@ -0,0 +1,95 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Rectangle class inherited from Shape class
*/
public class Rectangle extends Shape {
private double width;
private double length;
/**
* Constructor
*
* @param x x coordinate of the center of the rectangle
* @param y y coordinate of the center of the rectangle
* @param width width of the rectangle
* @param length length of the rectangle
*/
public Rectangle(double x, double y, double width, double length) {
super(x, y);
this.width = width;
this.length = length;
}
/**
* Calculates the area of the rectangle.
*
* @return the area of the rectangle
*/
@Override
public double area() {
return width * length;
}
/**
* Resizing a rectangle changes its width and length to both be newSize.
*
* @param newSize the new size of the rectangle
* @throws CannotResizeException
*/
@Override
public void resize(double newSize) {
width = newSize;
length = newSize;
}
/**
* Overloading resizeing a nectangle changes its width and length to both be
* newWidth and newLength.
*
* @param newWidth the new width of the rectangle
* @param newLength the new length of the rectangle
* @throws CannotResizeException
*/
public void resize(double newWidth, double newLength) throws CannotResizeException {
width = newWidth;
length = newLength;
}
/**
* Test.
*/
public static void testRectangle() {
Rectangle r = new Rectangle(1.2, 3.4, 4.0, 5.0);
// getX, getY, and move are inherited from Shape.
// area and resize come from Rectangle itself.
System.out.println(r.getX() == 1.2);
System.out.println(r.getY() == 3.4);
System.out.println(r.area() == 20.0);
// Move the rectangle. The area does not change.
r.move(7.8, 9.0);
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 20.0);
// Resize the rectangle. The area changes but not the position.
r.resize(12.0);
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 144.0);
// Resize the rectangle again with different width and length.
// The area changes but not the position.
// Resize the rectangle again with different width and length.
// The area changes but not the position.
try {
r.resize(10.0, 15.0);
} catch (CannotResizeException ex) {
System.out.println("BUG! This must never happen!");
}
System.out.println(r.getX() == 9.0);
System.out.println(r.getY() == 12.4);
System.out.println(r.area() == 150.0);
}
}

View File

@@ -0,0 +1,89 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-15
* Description: This is the abstract Shape class.
*/
public abstract class Shape {
/*
* Store the position of the central point of the shape.
*/
private double x;
private double y;
/**
* Constructor of the Shape class.
*
* @param x the x-coordinate of the central point of the shape.
* @param y the y-coordinate of the central point of the shape.
*/
public Shape(double x, double y) {
this.x = x;
this.y = y;
}
/**
* Getter of the x coordinate of the central point of the shape.
*
* @return the x coordinate of the central point of the shape.
*/
public double getX() {
return x;
}
/**
* Getter of the y coordinate of the central point of the shape.
*
* @return the y coordinate of the central point of the shape.
*/
public double getY() {
return y;
}
/**
* Move the central point of the shape by amounts dx and dy.
*
* @param dx the amount of movement in x direction.
* @param dy the amount of movement in y direction.
*/
public void move(double dx, double dy) {
x += dx;
y += dy;
}
/**
* Abstract method to calculate the area of the shape.
*
* @return the area of the shape.
*/
public abstract double area();
/**
* Abstract method to resize the shape.
*
* @param newSize the new size of the shape.
* @throws CannotResizeException
*/
public abstract void resize(double newSize) throws Exception;
/**
* Test.
*/
public static void testShape() {
Shape s = new Shape(3.9, 4.2) {
@Override
public double area() {
return 0;
}
@Override
public void resize(double newSize) {
}
};
System.out.println(s.getX() == 3.9);
System.out.println(s.getY() == 4.2);
s.move(1.0, 2.0);
System.out.println(s.getX() == 4.9);
System.out.println(s.getY() == 6.2);
}
}

View File

@@ -0,0 +1,81 @@
/*
* Author: CHEN Yongyuan (Walter) 1930006025 from OOP(1007)
* Date: 2022-04-14
* Description: Square class inherited from Rectangle class
*/
public class Square extends Rectangle {
/**
* Constructor.
*
* @param x the x coordinate center of the square
* @param y the y coordinate center of the square
* @param size the size of the square
*/
public Square(double x, double y, double size) {
super(x, y, size, size);
}
/**
* Override the resize(double double) method check width == height.
*
* @param width the new width of the square
* @param length the new length of the square
*/
@Override
public void resize(double width, double length) throws CannotResizeException {
if (width != length) {
throw new CannotResizeException("Cannot resize a square into a rectangle!");
}
super.resize(width, length);
}
/**
* Test.
*/
public static void testSquare() {
Square s = new Square(1.2, 3.4, 5.0);
// getX, getY, and move are inherited from Shape.
// area and resize are inherited from Rectangle.
System.out.println(s.getX() == 1.2);
System.out.println(s.getY() == 3.4);
System.out.println(s.area() == 25.0);
// Move the square. The area does not change.
s.move(7.8, 9.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 25.0);
// Resize the square. The area changes but not the position.
s.resize(12.0);
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 144.0);
// Resize the square again with different width and length!
// Now the square is not square anymore!
try {
s.resize(10.0, 15.0);
System.out.println(s.getX() == 9.0); // Unreachable.
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 150.0);
} catch (CannotResizeException ex) {
System.out.println(ex.getMessage() == "Cannot resize a square into a rectangle!");
}
// The area and position do not change.
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 144.0);
// Resize the square again with equal width and length.
// The area changes but not the position.
try {
s.resize(10.0, 10.0);
} catch (CannotResizeException ex) {
System.out.println("BUG! This must never happen!");
}
System.out.println(s.getX() == 9.0);
System.out.println(s.getY() == 12.4);
System.out.println(s.area() == 100.0);
}
}

View File

@@ -0,0 +1,55 @@
/**
* Answers the question.
*
* Question3:
*
* - Q: What is the problem with the resize method of the Shape calss? How do
* you solve this problem?
*
* - A: The resize method of the Dot class is inherited from the Shape class.
* which does not have a throw Exception declared. We can solve this problem by
* declaring the resize method in the Shape class as a throw Exception.
*
* - Q: Do you need to change the resize method of the Circle class then?
*
* - A: No, we do not need to change the resize method of the Circle class.
*
* - Q: Does the Square class need its own area and resize method or not?
*
* - A: No, but it depends. Currently the Square.area and Square.resize methods
* return the correct values. But in the future, if we want to change the
* Rectangle.area and Rectangle.resize methods, the Square class may give the
* wrong values. So it is better to have its own area and resize methods.
*
* - Q: What is then the problem for the Square class?
*
* - A: No problem. The area method is overloaded. But the resize method in
* Square can set different values for the width and height. So it is better to
* have its own resize methods.
*
* - Q: What happends with the resize method of the Rectangle class?
*
* - A: The resize method of the Square class is inherited from the Rectangle
* class. So we need to add the throws Exception to the resize method of the
* Square class.
*
* - Q: What is then the problem for the Shape class?
*
* - A: The resize method of the Circle class is inherited from the Shape class.
* Circle.resize() method does throw a BadRadiusException, but its parent class
* Shape.resize() method does not throw a BadRadiusException, instead it throws
* a CannotResizeException. So we can solve this problem by declaring the parent
* class of BadRadiusException and CannotResizeException as a throw Exception.
* Or we can solve this problem by declaring both BadRadiusException and
* CannotResizeException in the Shape class.
*/
public class Start {
public static void main(String[] args) {
Shape.testShape();
Circle.testCircle();
Dot.testDot();
Rectangle.testRectangle();
Square.testSquare();
}
}