From cea65850335e5bb0097ea8fdc5c170433c2f6505 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 17 Apr 2022 11:41:41 +0800 Subject: [PATCH] lab8 finished --- assignment3/Question1/StartGrading.java | 84 +++++++++++++ assignment3/Question2/StartGrading.java | 99 +++++++++++++++ lab/lab8/Question2/Circle.java | 59 +++++++++ lab/lab8/Question2/Shape.java | 88 ++++++++++++++ lab/lab8/Question2/Start.java | 6 + lab/lab8/Question3/Circle.java | 59 +++++++++ lab/lab8/Question3/Dot.java | 64 ++++++++++ lab/lab8/Question3/Shape.java | 89 ++++++++++++++ lab/lab8/Question3/Start.java | 24 ++++ lab/lab8/Question4/CannotResizeException.java | 19 +++ lab/lab8/Question4/Circle.java | 63 ++++++++++ lab/lab8/Question4/Dot.java | 72 +++++++++++ lab/lab8/Question4/Shape.java | 89 ++++++++++++++ lab/lab8/Question4/Start.java | 24 ++++ lab/lab8/Question5/CannotResizeException.java | 19 +++ lab/lab8/Question5/Circle.java | 63 ++++++++++ lab/lab8/Question5/Dot.java | 72 +++++++++++ lab/lab8/Question5/Rectangle.java | 67 ++++++++++ lab/lab8/Question5/Shape.java | 89 ++++++++++++++ lab/lab8/Question5/Square.java | 40 ++++++ lab/lab8/Question5/Start.java | 33 +++++ lab/lab8/Question6/CannotResizeException.java | 19 +++ lab/lab8/Question6/Circle.java | 63 ++++++++++ lab/lab8/Question6/Dot.java | 72 +++++++++++ lab/lab8/Question6/Rectangle.java | 86 +++++++++++++ lab/lab8/Question6/Shape.java | 89 ++++++++++++++ lab/lab8/Question6/Square.java | 49 ++++++++ lab/lab8/Question6/Start.java | 39 ++++++ lab/lab8/Question7/CannotResizeException.java | 19 +++ lab/lab8/Question7/Circle.java | 63 ++++++++++ lab/lab8/Question7/Dot.java | 72 +++++++++++ lab/lab8/Question7/Rectangle.java | 93 ++++++++++++++ lab/lab8/Question7/Shape.java | 89 ++++++++++++++ lab/lab8/Question7/Square.java | 80 ++++++++++++ lab/lab8/Question7/Start.java | 45 +++++++ lab/lab8/Question8/BadRadiusException.java | 19 +++ lab/lab8/Question8/CannotResizeException.java | 19 +++ lab/lab8/Question8/Circle.java | 115 ++++++++++++++++++ lab/lab8/Question8/Dot.java | 72 +++++++++++ lab/lab8/Question8/Rectangle.java | 95 +++++++++++++++ lab/lab8/Question8/Shape.java | 89 ++++++++++++++ lab/lab8/Question8/Square.java | 81 ++++++++++++ lab/lab8/Question8/Start.java | 55 +++++++++ 43 files changed, 2645 insertions(+) create mode 100644 assignment3/Question1/StartGrading.java create mode 100644 assignment3/Question2/StartGrading.java create mode 100644 lab/lab8/Question2/Circle.java create mode 100644 lab/lab8/Question2/Shape.java create mode 100644 lab/lab8/Question2/Start.java create mode 100644 lab/lab8/Question3/Circle.java create mode 100644 lab/lab8/Question3/Dot.java create mode 100644 lab/lab8/Question3/Shape.java create mode 100644 lab/lab8/Question3/Start.java create mode 100644 lab/lab8/Question4/CannotResizeException.java create mode 100644 lab/lab8/Question4/Circle.java create mode 100644 lab/lab8/Question4/Dot.java create mode 100644 lab/lab8/Question4/Shape.java create mode 100644 lab/lab8/Question4/Start.java create mode 100644 lab/lab8/Question5/CannotResizeException.java create mode 100644 lab/lab8/Question5/Circle.java create mode 100644 lab/lab8/Question5/Dot.java create mode 100644 lab/lab8/Question5/Rectangle.java create mode 100644 lab/lab8/Question5/Shape.java create mode 100644 lab/lab8/Question5/Square.java create mode 100644 lab/lab8/Question5/Start.java create mode 100644 lab/lab8/Question6/CannotResizeException.java create mode 100644 lab/lab8/Question6/Circle.java create mode 100644 lab/lab8/Question6/Dot.java create mode 100644 lab/lab8/Question6/Rectangle.java create mode 100644 lab/lab8/Question6/Shape.java create mode 100644 lab/lab8/Question6/Square.java create mode 100644 lab/lab8/Question6/Start.java create mode 100644 lab/lab8/Question7/CannotResizeException.java create mode 100644 lab/lab8/Question7/Circle.java create mode 100644 lab/lab8/Question7/Dot.java create mode 100644 lab/lab8/Question7/Rectangle.java create mode 100644 lab/lab8/Question7/Shape.java create mode 100644 lab/lab8/Question7/Square.java create mode 100644 lab/lab8/Question7/Start.java create mode 100644 lab/lab8/Question8/BadRadiusException.java create mode 100644 lab/lab8/Question8/CannotResizeException.java create mode 100644 lab/lab8/Question8/Circle.java create mode 100644 lab/lab8/Question8/Dot.java create mode 100644 lab/lab8/Question8/Rectangle.java create mode 100644 lab/lab8/Question8/Shape.java create mode 100644 lab/lab8/Question8/Square.java create mode 100644 lab/lab8/Question8/Start.java diff --git a/assignment3/Question1/StartGrading.java b/assignment3/Question1/StartGrading.java new file mode 100644 index 0000000..cfa71b3 --- /dev/null +++ b/assignment3/Question1/StartGrading.java @@ -0,0 +1,84 @@ +public class StartGrading { + public static void testMammal() { + Mammal m = new Mammal("some name"); + System.out.println(m.getName() == "some name"); + System.out.println(m.isCookable() == false); // Message printed too. + } + + public static void testHuman() { + Human h = new Human(); + // getName is inherited from Mammal. + System.out.println(h.getName() == "Alice"); + System.out.println(h.isCookable() == false); // No message printed. + } + + + public static void testRabbit() { + Rabbit r = new Rabbit("Bugs Bunny", 20.0); + // getName is inherited from Mammal. + System.out.println(r.getName() == "Bugs Bunny"); + System.out.println(r.getWeight() == 20.0); + System.out.println(r.isCookable() == true); + } + + + public static void testEuropeanRabbit() { + // Testing the first constructor. + EuropeanRabbit er1 = new EuropeanRabbit("black bunny", 3.0); + // getName is inherited from Mammal. + System.out.println(er1.getName() == "black bunny"); + // getWeight is inherited from Rabbit. + System.out.println(er1.getWeight() == 3.0); + // isCookable is inherited from Rabbit. + System.out.println(er1.isCookable() == true); + // Testing the second constructor. + EuropeanRabbit er2 = new EuropeanRabbit("white rabbit"); + // getName is inherited from Mammal. + System.out.println(er2.getName() == "white rabbit"); + // getWeight is inherited from Rabbit. + System.out.println(er2.getWeight() == 2.0); + // isCookable is inherited from Rabbit. + System.out.println(er2.isCookable() == true); + } + + public static void testEuropeanRabbit() { + // Testing the first constructor. + EuropeanRabbit er1 = new EuropeanRabbit("black bunny", 3.0); + // getName is inherited from Mammal. + System.out.println(er1.getName() == "black bunny"); + // getWeight is inherited from Rabbit. + System.out.println(er1.getWeight() == 3.0); + // isCookable is inherited from Rabbit. + System.out.println(er1.isCookable() == true); + // Testing the second constructor. + EuropeanRabbit er2 = new EuropeanRabbit("white rabbit"); + // getName is inherited from Mammal. + System.out.println(er2.getName() == "white rabbit"); + // getWeight is inherited from Rabbit. + System.out.println(er2.getWeight() == 2.0); + // isCookable is inherited from Rabbit. + System.out.println(er2.isCookable() == true); + } + + + public static void testFrankTheRabbit() { + FrankTheRabbit ftr = new FrankTheRabbit(); + // getName is inherited from Mammal. + System.out.println(ftr.getName() == "Frank"); + // getWeight is inherited from Rabbit. + System.out.println(ftr.getWeight() == 100.0); + // isCookable is from FrankTheRabbit itself. + System.out.println(ftr.isCookable() == false); + } + + public static void main(String[] args) { + testMammal(); + testHuman(); + testRabbit(); + testEuropeanRabbit(); + testLapinSaut¨¦Chasseur(); + testFrankTheRabbit(); + } + + +} diff --git a/assignment3/Question2/StartGrading.java b/assignment3/Question2/StartGrading.java new file mode 100644 index 0000000..dd6bbe1 --- /dev/null +++ b/assignment3/Question2/StartGrading.java @@ -0,0 +1,99 @@ +public class StartGrading { + public static void testMammal() { + Mammal m = new Mammal("some name"); + System.out.println(m.getName() == "some name"); + System.out.println(m.isCookable() == false); // Message printed too. + } + + public static void testHuman() { + Human h = new Human(); + // getName is inherited from Mammal. + System.out.println(h.getName() == "Alice"); + System.out.println(h.isCookable() == false); // No message printed. + } + + + public static void testRabbit() { + Rabbit r = new Rabbit("Bugs Bunny", 20.0); + // getName is inherited from Mammal. + System.out.println(r.getName() == "Bugs Bunny"); + System.out.println(r.getWeight() == 20.0); + System.out.println(r.isCookable() == true); + } + + + public static void testEuropeanRabbit() { + // Testing the first constructor. + EuropeanRabbit er1 = new EuropeanRabbit("black bunny", 3.0); + // getName is inherited from Mammal. + System.out.println(er1.getName() == "black bunny"); + // getWeight is inherited from Rabbit. + System.out.println(er1.getWeight() == 3.0); + // isCookable is inherited from Rabbit. + System.out.println(er1.isCookable() == true); + // Testing the second constructor. + EuropeanRabbit er2 = new EuropeanRabbit("white rabbit"); + // getName is inherited from Mammal. + System.out.println(er2.getName() == "white rabbit"); + // getWeight is inherited from Rabbit. + System.out.println(er2.getWeight() == 2.0); + // isCookable is inherited from Rabbit. + System.out.println(er2.isCookable() == true); + } + + public static void testEuropeanRabbit() { + // Testing the first constructor. + EuropeanRabbit er1 = new EuropeanRabbit("black bunny", 3.0); + // getName is inherited from Mammal. + System.out.println(er1.getName() == "black bunny"); + // getWeight is inherited from Rabbit. + System.out.println(er1.getWeight() == 3.0); + // isCookable is inherited from Rabbit. + System.out.println(er1.isCookable() == true); + // Testing the second constructor. + EuropeanRabbit er2 = new EuropeanRabbit("white rabbit"); + // getName is inherited from Mammal. + System.out.println(er2.getName() == "white rabbit"); + // getWeight is inherited from Rabbit. + System.out.println(er2.getWeight() == 2.0); + // isCookable is inherited from Rabbit. + System.out.println(er2.isCookable() == true); + } + + + public static void testFrankTheRabbit() { + FrankTheRabbit ftr = new FrankTheRabbit(); + // getName is inherited from Mammal. + System.out.println(ftr.getName() == "Frank"); + // getWeight is inherited from Rabbit. + System.out.println(ftr.getWeight() == 100.0); + // isCookable is from FrankTheRabbit itself. + System.out.println(ftr.isCookable() == false); + } + + public static void testCastIronPot() { + // In the testCastIronPot method, create a lapin saut¨¦ chasseur called lsc1... + LapinSaut¨¦Chasseur lsc1 = new LapinSaut¨¦Chasseur(); + // then create a cast iron pot with this lapin saut¨¦ chasseur in it. + CastIronPot cip = new CastIronPot(lsc1); // Implicit upcast from LapinSaut¨¦Chasseur to Rabbit. + // Then get the lapin saut¨¦ chasseur from the cast iron pot... + Rabbit r = cip.getRabbit(); + // and store it into a local variable called lsc2 of type LapinSaut¨¦Chasseur. + LapinSaut¨¦Chasseur lsc2 = (LapinSaut¨¦Chasseur)r; // Downcast mandatory! + // Use the == operator to check that lsc1 and lsc2 are the same lapin saut¨¦ chasseur. + System.out.println(lsc1 == lsc2); + } + + + public static void main(String[] args) { + testMammal(); + testHuman(); + testRabbit(); + testEuropeanRabbit(); + testLapinSaut¨¦Chasseur(); + testFrankTheRabbit(); + testCastIronPot(); + } + + +} diff --git a/lab/lab8/Question2/Circle.java b/lab/lab8/Question2/Circle.java new file mode 100644 index 0000000..3b13d5d --- /dev/null +++ b/lab/lab8/Question2/Circle.java @@ -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); + } +} diff --git a/lab/lab8/Question2/Shape.java b/lab/lab8/Question2/Shape.java new file mode 100644 index 0000000..2e0d63d --- /dev/null +++ b/lab/lab8/Question2/Shape.java @@ -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); + } +} diff --git a/lab/lab8/Question2/Start.java b/lab/lab8/Question2/Start.java new file mode 100644 index 0000000..3604213 --- /dev/null +++ b/lab/lab8/Question2/Start.java @@ -0,0 +1,6 @@ +public class Start { + public static void main(String[] args) { + Shape.testShape(); + Circle.testCircle(); + } +} diff --git a/lab/lab8/Question3/Circle.java b/lab/lab8/Question3/Circle.java new file mode 100644 index 0000000..3b13d5d --- /dev/null +++ b/lab/lab8/Question3/Circle.java @@ -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); + } +} diff --git a/lab/lab8/Question3/Dot.java b/lab/lab8/Question3/Dot.java new file mode 100644 index 0000000..8e4c67e --- /dev/null +++ b/lab/lab8/Question3/Dot.java @@ -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); + } +} diff --git a/lab/lab8/Question3/Shape.java b/lab/lab8/Question3/Shape.java new file mode 100644 index 0000000..5bb0b81 --- /dev/null +++ b/lab/lab8/Question3/Shape.java @@ -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); + } +} diff --git a/lab/lab8/Question3/Start.java b/lab/lab8/Question3/Start.java new file mode 100644 index 0000000..4e427d0 --- /dev/null +++ b/lab/lab8/Question3/Start.java @@ -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(); + } +} diff --git a/lab/lab8/Question4/CannotResizeException.java b/lab/lab8/Question4/CannotResizeException.java new file mode 100644 index 0000000..ecc7d9b --- /dev/null +++ b/lab/lab8/Question4/CannotResizeException.java @@ -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); + } +} diff --git a/lab/lab8/Question4/Circle.java b/lab/lab8/Question4/Circle.java new file mode 100644 index 0000000..6d468a2 --- /dev/null +++ b/lab/lab8/Question4/Circle.java @@ -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); + } +} diff --git a/lab/lab8/Question4/Dot.java b/lab/lab8/Question4/Dot.java new file mode 100644 index 0000000..231533d --- /dev/null +++ b/lab/lab8/Question4/Dot.java @@ -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!"); + } + + } +} diff --git a/lab/lab8/Question4/Shape.java b/lab/lab8/Question4/Shape.java new file mode 100644 index 0000000..56f3838 --- /dev/null +++ b/lab/lab8/Question4/Shape.java @@ -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); + } +} diff --git a/lab/lab8/Question4/Start.java b/lab/lab8/Question4/Start.java new file mode 100644 index 0000000..4e427d0 --- /dev/null +++ b/lab/lab8/Question4/Start.java @@ -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(); + } +} diff --git a/lab/lab8/Question5/CannotResizeException.java b/lab/lab8/Question5/CannotResizeException.java new file mode 100644 index 0000000..ecc7d9b --- /dev/null +++ b/lab/lab8/Question5/CannotResizeException.java @@ -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); + } +} diff --git a/lab/lab8/Question5/Circle.java b/lab/lab8/Question5/Circle.java new file mode 100644 index 0000000..6d468a2 --- /dev/null +++ b/lab/lab8/Question5/Circle.java @@ -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); + } +} diff --git a/lab/lab8/Question5/Dot.java b/lab/lab8/Question5/Dot.java new file mode 100644 index 0000000..231533d --- /dev/null +++ b/lab/lab8/Question5/Dot.java @@ -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!"); + } + + } +} diff --git a/lab/lab8/Question5/Rectangle.java b/lab/lab8/Question5/Rectangle.java new file mode 100644 index 0000000..b240643 --- /dev/null +++ b/lab/lab8/Question5/Rectangle.java @@ -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); + } +} diff --git a/lab/lab8/Question5/Shape.java b/lab/lab8/Question5/Shape.java new file mode 100644 index 0000000..56f3838 --- /dev/null +++ b/lab/lab8/Question5/Shape.java @@ -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); + } +} diff --git a/lab/lab8/Question5/Square.java b/lab/lab8/Question5/Square.java new file mode 100644 index 0000000..d393545 --- /dev/null +++ b/lab/lab8/Question5/Square.java @@ -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); + } +} diff --git a/lab/lab8/Question5/Start.java b/lab/lab8/Question5/Start.java new file mode 100644 index 0000000..bffa3a9 --- /dev/null +++ b/lab/lab8/Question5/Start.java @@ -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(); + } +} diff --git a/lab/lab8/Question6/CannotResizeException.java b/lab/lab8/Question6/CannotResizeException.java new file mode 100644 index 0000000..ecc7d9b --- /dev/null +++ b/lab/lab8/Question6/CannotResizeException.java @@ -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); + } +} diff --git a/lab/lab8/Question6/Circle.java b/lab/lab8/Question6/Circle.java new file mode 100644 index 0000000..6d468a2 --- /dev/null +++ b/lab/lab8/Question6/Circle.java @@ -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); + } +} diff --git a/lab/lab8/Question6/Dot.java b/lab/lab8/Question6/Dot.java new file mode 100644 index 0000000..231533d --- /dev/null +++ b/lab/lab8/Question6/Dot.java @@ -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!"); + } + + } +} diff --git a/lab/lab8/Question6/Rectangle.java b/lab/lab8/Question6/Rectangle.java new file mode 100644 index 0000000..ff98e78 --- /dev/null +++ b/lab/lab8/Question6/Rectangle.java @@ -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); + + } +} diff --git a/lab/lab8/Question6/Shape.java b/lab/lab8/Question6/Shape.java new file mode 100644 index 0000000..56f3838 --- /dev/null +++ b/lab/lab8/Question6/Shape.java @@ -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); + } +} diff --git a/lab/lab8/Question6/Square.java b/lab/lab8/Question6/Square.java new file mode 100644 index 0000000..32c0787 --- /dev/null +++ b/lab/lab8/Question6/Square.java @@ -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); + + } +} diff --git a/lab/lab8/Question6/Start.java b/lab/lab8/Question6/Start.java new file mode 100644 index 0000000..a798b3e --- /dev/null +++ b/lab/lab8/Question6/Start.java @@ -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(); + } +} diff --git a/lab/lab8/Question7/CannotResizeException.java b/lab/lab8/Question7/CannotResizeException.java new file mode 100644 index 0000000..ecc7d9b --- /dev/null +++ b/lab/lab8/Question7/CannotResizeException.java @@ -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); + } +} diff --git a/lab/lab8/Question7/Circle.java b/lab/lab8/Question7/Circle.java new file mode 100644 index 0000000..6d468a2 --- /dev/null +++ b/lab/lab8/Question7/Circle.java @@ -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); + } +} diff --git a/lab/lab8/Question7/Dot.java b/lab/lab8/Question7/Dot.java new file mode 100644 index 0000000..231533d --- /dev/null +++ b/lab/lab8/Question7/Dot.java @@ -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!"); + } + + } +} diff --git a/lab/lab8/Question7/Rectangle.java b/lab/lab8/Question7/Rectangle.java new file mode 100644 index 0000000..1c5c513 --- /dev/null +++ b/lab/lab8/Question7/Rectangle.java @@ -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); + + } +} diff --git a/lab/lab8/Question7/Shape.java b/lab/lab8/Question7/Shape.java new file mode 100644 index 0000000..36dbcc2 --- /dev/null +++ b/lab/lab8/Question7/Shape.java @@ -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); + } +} diff --git a/lab/lab8/Question7/Square.java b/lab/lab8/Question7/Square.java new file mode 100644 index 0000000..5d15083 --- /dev/null +++ b/lab/lab8/Question7/Square.java @@ -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); + + } +} diff --git a/lab/lab8/Question7/Start.java b/lab/lab8/Question7/Start.java new file mode 100644 index 0000000..1d9c91e --- /dev/null +++ b/lab/lab8/Question7/Start.java @@ -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(); + } +} diff --git a/lab/lab8/Question8/BadRadiusException.java b/lab/lab8/Question8/BadRadiusException.java new file mode 100644 index 0000000..d16b877 --- /dev/null +++ b/lab/lab8/Question8/BadRadiusException.java @@ -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); + } +} diff --git a/lab/lab8/Question8/CannotResizeException.java b/lab/lab8/Question8/CannotResizeException.java new file mode 100644 index 0000000..ecc7d9b --- /dev/null +++ b/lab/lab8/Question8/CannotResizeException.java @@ -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); + } +} diff --git a/lab/lab8/Question8/Circle.java b/lab/lab8/Question8/Circle.java new file mode 100644 index 0000000..2bf8a5b --- /dev/null +++ b/lab/lab8/Question8/Circle.java @@ -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!"); + } + + } +} diff --git a/lab/lab8/Question8/Dot.java b/lab/lab8/Question8/Dot.java new file mode 100644 index 0000000..231533d --- /dev/null +++ b/lab/lab8/Question8/Dot.java @@ -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!"); + } + + } +} diff --git a/lab/lab8/Question8/Rectangle.java b/lab/lab8/Question8/Rectangle.java new file mode 100644 index 0000000..5a5350e --- /dev/null +++ b/lab/lab8/Question8/Rectangle.java @@ -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); + + } +} diff --git a/lab/lab8/Question8/Shape.java b/lab/lab8/Question8/Shape.java new file mode 100644 index 0000000..b44fabf --- /dev/null +++ b/lab/lab8/Question8/Shape.java @@ -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); + } +} diff --git a/lab/lab8/Question8/Square.java b/lab/lab8/Question8/Square.java new file mode 100644 index 0000000..d6b43f4 --- /dev/null +++ b/lab/lab8/Question8/Square.java @@ -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); + + } +} diff --git a/lab/lab8/Question8/Start.java b/lab/lab8/Question8/Start.java new file mode 100644 index 0000000..f68e6f0 --- /dev/null +++ b/lab/lab8/Question8/Start.java @@ -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(); + } +}