Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions chapter6/haesa/refactor-chapter6-2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class Square {
public Point topLeft;
public double side;
}

public class Rectangle {
public Point topLeft;
public double height;
public double width;
}

public class Circle {
public Point center;
public double radius;
}

public class Geometry {
public final double PI = 3.141592653;

public double area(Object shape) throws NoSuchShapeException {
if (shape instanceof Square) {
Square square = (Square) shape;
return square.side * square.side;
}
else if (shape instanceof Rectangle) {
Rectangle rectangle = (Rectangle) shape;
return rectangle.height * rectangle.width;
}
else if (shape instanceof Circle) {
Circle circle = (Circle) shape;
return PI * circle.radius * circle.radius;
}
throw new NoSuchShapeException();
}
}