lab8 finished
This commit is contained in:
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user