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