lab8 finished
This commit is contained in:
84
assignment3/Question1/StartGrading.java
Normal file
84
assignment3/Question1/StartGrading.java
Normal file
@@ -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<EFBFBD><EFBFBD>Chasseur();
|
||||
testFrankTheRabbit();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
99
assignment3/Question2/StartGrading.java
Normal file
99
assignment3/Question2/StartGrading.java
Normal file
@@ -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<75><74> chasseur called lsc1...
|
||||
LapinSaut<EFBFBD><EFBFBD>Chasseur lsc1 = new LapinSaut<EFBFBD><EFBFBD>Chasseur();
|
||||
// then create a cast iron pot with this lapin saut<75><74> chasseur in it.
|
||||
CastIronPot cip = new CastIronPot(lsc1); // Implicit upcast from LapinSaut<75><74>Chasseur to Rabbit.
|
||||
// Then get the lapin saut<75><74> chasseur from the cast iron pot...
|
||||
Rabbit r = cip.getRabbit();
|
||||
// and store it into a local variable called lsc2 of type LapinSaut<75><74>Chasseur.
|
||||
LapinSaut<EFBFBD><EFBFBD>Chasseur lsc2 = (LapinSaut<EFBFBD><EFBFBD>Chasseur)r; // Downcast mandatory!
|
||||
// Use the == operator to check that lsc1 and lsc2 are the same lapin saut<75><74> chasseur.
|
||||
System.out.println(lsc1 == lsc2);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
testMammal();
|
||||
testHuman();
|
||||
testRabbit();
|
||||
testEuropeanRabbit();
|
||||
testLapinSaut<EFBFBD><EFBFBD>Chasseur();
|
||||
testFrankTheRabbit();
|
||||
testCastIronPot();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
59
lab/lab8/Question2/Circle.java
Normal file
59
lab/lab8/Question2/Circle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
88
lab/lab8/Question2/Shape.java
Normal file
88
lab/lab8/Question2/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
6
lab/lab8/Question2/Start.java
Normal file
6
lab/lab8/Question2/Start.java
Normal file
@@ -0,0 +1,6 @@
|
||||
public class Start {
|
||||
public static void main(String[] args) {
|
||||
Shape.testShape();
|
||||
Circle.testCircle();
|
||||
}
|
||||
}
|
||||
59
lab/lab8/Question3/Circle.java
Normal file
59
lab/lab8/Question3/Circle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
64
lab/lab8/Question3/Dot.java
Normal file
64
lab/lab8/Question3/Dot.java
Normal 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);
|
||||
}
|
||||
}
|
||||
89
lab/lab8/Question3/Shape.java
Normal file
89
lab/lab8/Question3/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
24
lab/lab8/Question3/Start.java
Normal file
24
lab/lab8/Question3/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
19
lab/lab8/Question4/CannotResizeException.java
Normal file
19
lab/lab8/Question4/CannotResizeException.java
Normal 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);
|
||||
}
|
||||
}
|
||||
63
lab/lab8/Question4/Circle.java
Normal file
63
lab/lab8/Question4/Circle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
72
lab/lab8/Question4/Dot.java
Normal file
72
lab/lab8/Question4/Dot.java
Normal 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!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
89
lab/lab8/Question4/Shape.java
Normal file
89
lab/lab8/Question4/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
24
lab/lab8/Question4/Start.java
Normal file
24
lab/lab8/Question4/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
19
lab/lab8/Question5/CannotResizeException.java
Normal file
19
lab/lab8/Question5/CannotResizeException.java
Normal 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);
|
||||
}
|
||||
}
|
||||
63
lab/lab8/Question5/Circle.java
Normal file
63
lab/lab8/Question5/Circle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
72
lab/lab8/Question5/Dot.java
Normal file
72
lab/lab8/Question5/Dot.java
Normal 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!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
67
lab/lab8/Question5/Rectangle.java
Normal file
67
lab/lab8/Question5/Rectangle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
89
lab/lab8/Question5/Shape.java
Normal file
89
lab/lab8/Question5/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
40
lab/lab8/Question5/Square.java
Normal file
40
lab/lab8/Question5/Square.java
Normal 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);
|
||||
}
|
||||
}
|
||||
33
lab/lab8/Question5/Start.java
Normal file
33
lab/lab8/Question5/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
19
lab/lab8/Question6/CannotResizeException.java
Normal file
19
lab/lab8/Question6/CannotResizeException.java
Normal 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);
|
||||
}
|
||||
}
|
||||
63
lab/lab8/Question6/Circle.java
Normal file
63
lab/lab8/Question6/Circle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
72
lab/lab8/Question6/Dot.java
Normal file
72
lab/lab8/Question6/Dot.java
Normal 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!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
86
lab/lab8/Question6/Rectangle.java
Normal file
86
lab/lab8/Question6/Rectangle.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
89
lab/lab8/Question6/Shape.java
Normal file
89
lab/lab8/Question6/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
49
lab/lab8/Question6/Square.java
Normal file
49
lab/lab8/Question6/Square.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
39
lab/lab8/Question6/Start.java
Normal file
39
lab/lab8/Question6/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
19
lab/lab8/Question7/CannotResizeException.java
Normal file
19
lab/lab8/Question7/CannotResizeException.java
Normal 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);
|
||||
}
|
||||
}
|
||||
63
lab/lab8/Question7/Circle.java
Normal file
63
lab/lab8/Question7/Circle.java
Normal 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);
|
||||
}
|
||||
}
|
||||
72
lab/lab8/Question7/Dot.java
Normal file
72
lab/lab8/Question7/Dot.java
Normal 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!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
93
lab/lab8/Question7/Rectangle.java
Normal file
93
lab/lab8/Question7/Rectangle.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
89
lab/lab8/Question7/Shape.java
Normal file
89
lab/lab8/Question7/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
80
lab/lab8/Question7/Square.java
Normal file
80
lab/lab8/Question7/Square.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
45
lab/lab8/Question7/Start.java
Normal file
45
lab/lab8/Question7/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
19
lab/lab8/Question8/BadRadiusException.java
Normal file
19
lab/lab8/Question8/BadRadiusException.java
Normal 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);
|
||||
}
|
||||
}
|
||||
19
lab/lab8/Question8/CannotResizeException.java
Normal file
19
lab/lab8/Question8/CannotResizeException.java
Normal 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);
|
||||
}
|
||||
}
|
||||
115
lab/lab8/Question8/Circle.java
Normal file
115
lab/lab8/Question8/Circle.java
Normal 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!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
72
lab/lab8/Question8/Dot.java
Normal file
72
lab/lab8/Question8/Dot.java
Normal 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!");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
95
lab/lab8/Question8/Rectangle.java
Normal file
95
lab/lab8/Question8/Rectangle.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
89
lab/lab8/Question8/Shape.java
Normal file
89
lab/lab8/Question8/Shape.java
Normal 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);
|
||||
}
|
||||
}
|
||||
81
lab/lab8/Question8/Square.java
Normal file
81
lab/lab8/Question8/Square.java
Normal 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);
|
||||
|
||||
}
|
||||
}
|
||||
55
lab/lab8/Question8/Start.java
Normal file
55
lab/lab8/Question8/Start.java
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user