Skip to content
Snippets Groups Projects
Commit 8b1d5c42 authored by Paul Rauser's avatar Paul Rauser
Browse files

chore: add clean exception handling logic

parent dec1d89a
No related tags found
No related merge requests found
......@@ -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());
}
}
}
package shapes;
public class Rectangle implements Shape {
private double width;
private double height;
......
......@@ -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");
}
}
}
package shapes;
public class SizeException extends Exception {
public SizeException(String msg) {
super(msg);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment