diff --git a/src/shapes/App.java b/src/shapes/App.java index 1ef07d367623cf847655394b6fbf0ab9764c5c86..6033eafd129252b1b0d92d92f0996a62e7f03470 100644 --- a/src/shapes/App.java +++ b/src/shapes/App.java @@ -2,31 +2,38 @@ package shapes; public class App { public static void main(String[] args) { - Circle c1 = new Circle(5.0); - Rectangle r1 = new Rectangle(2.0, 10.0); - Circle c2 = new Circle(20.0); - Rectangle r2 = new Rectangle(23.0, 100.0); - RectangleCircle rc1 = new RectangleCircle(10.0, 25.0, 30.0); + try { + Circle c1 = new Circle(5.0); + Rectangle r1 = new Rectangle(2.0, 10.0); + Circle c2 = new Circle(20.0); + Rectangle r2 = new Rectangle(23.0, 100.0); + RectangleCircle rc1 = new RectangleCircle(10.0, 25.0, 30.0); + // rc1.setHeight(5.0); + // rc1.setWidth(5.0); + // rc1.setRadius(26.0); + + System.out.println("First circle area: " + c1.area()); + System.out.println("First circle circumference: " + c1.circumference()); + System.out.println("First rectangle area: " + r1.area()); + System.out.println("First rectangle circumference: " + r1.circumference()); + System.out.println("First rectangle circle area: " + rc1.area()); + System.out.println("First rectangle circle circumference: " + rc1.circumference()); - System.out.println("First circle area: " + c1.area()); - System.out.println("First circle circumference: " + c1.circumference()); - System.out.println("First rectangle area: " + r1.area()); - System.out.println("First rectangle circumference: " + r1.circumference()); - System.out.println("First rectangle circle area: " + rc1.area()); - System.out.println("First rectangle circle circumference: " + rc1.circumference()); + Shape[] shapes = new Shape[10]; + shapes[0] = c1; + shapes[1] = c2; + shapes[2] = r1; + shapes[3] = r2; + shapes[4] = rc1; - Shape[] shapes = new Shape[10]; - shapes[0] = c1; - shapes[1] = c2; - shapes[2] = r1; - shapes[3] = r2; - shapes[4] = rc1; + System.out.println("Accumulation of areas: " + Util.accumulateArea(shapes)); + System.out.println("Accumulation of circumferences: " + Util.accumulateCircumference(shapes)); - System.out.println("Accumulation of areas: " + Util.accumulateArea(shapes)); - System.out.println("Accumulation of circumferences: " + Util.accumulateCircumference(shapes)); - - Object[] objects = shapes; - System.out.println("Accumulation of areas: " + Util.accumulateArea(objects)); - System.out.println("Accumulation of circumferences: " + Util.accumulateCircumference(objects)); + Object[] objects = shapes; + System.out.println("Accumulation of areas: " + Util.accumulateArea(objects)); + System.out.println("Accumulation of circumferences: " + Util.accumulateCircumference(objects)); + } catch(SizeException e) { + System.out.println(e.getMessage()); + } } } diff --git a/src/shapes/Rectangle.java b/src/shapes/Rectangle.java index dff0a71eb4d12dce70439f5ea8c2e2556cbd755b..57d3b06d695e7eee567accf6edb3969e34e00136 100644 --- a/src/shapes/Rectangle.java +++ b/src/shapes/Rectangle.java @@ -1,7 +1,6 @@ package shapes; public class Rectangle implements Shape { - private double width; private double height; diff --git a/src/shapes/RectangleCircle.java b/src/shapes/RectangleCircle.java index cd3450f7a5758e271407043e99adf7841e95968d..212de96bb9fffc1956214e13c60d9fad8998a221 100644 --- a/src/shapes/RectangleCircle.java +++ b/src/shapes/RectangleCircle.java @@ -4,7 +4,8 @@ public class RectangleCircle implements Shape { private Circle c; private Rectangle r; - public RectangleCircle(double radius, double width, double height) { + public RectangleCircle(double radius, double width, double height) throws SizeException { + validate(radius, width, height); c = new Circle(radius); r = new Rectangle(width, height); } @@ -19,7 +20,8 @@ public class RectangleCircle implements Shape { return r.circumference() + c.circumference(); } - public void setRadius(double radius) { + public void setRadius(double radius) throws SizeException { + validate(radius, r.getWidth(), r.getHeight()); c.setRadius(radius); } @@ -27,7 +29,8 @@ public class RectangleCircle implements Shape { return c.getRadius(); } - public void setWidth(double width) { + public void setWidth(double width) throws SizeException { + validate(c.getRadius(), width, r.getHeight()); r.setWidth(width); } @@ -35,11 +38,18 @@ public class RectangleCircle implements Shape { return r.getWidth(); } - public void setHeight(double height) { + public void setHeight(double height) throws SizeException { + validate(c.getRadius(), r.getWidth(), height); r.setHeight(height); } public double getHeight() { return r.getHeight(); } -} \ No newline at end of file + + public void validate(double radius, double width, double height) throws SizeException { + if(width < radius * 2 || height < radius * 2) { + throw new SizeException("The specified RectangleCircle is invalid"); + } + } +} diff --git a/src/shapes/SizeException.java b/src/shapes/SizeException.java new file mode 100644 index 0000000000000000000000000000000000000000..6a651ce8d9b62a3789730d35d82657bcb30f50c2 --- /dev/null +++ b/src/shapes/SizeException.java @@ -0,0 +1,7 @@ +package shapes; + +public class SizeException extends Exception { + public SizeException(String msg) { + super(msg); + } +}