Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // An abstract class for geometric figures
- static abstract class GeometricFigure {
- abstract double getArea();
- abstract double getPerimeter();
- }
- // A class for rectangles
- static class Rectangle extends GeometricFigure {
- double length, breadth;
- Rectangle(double length, double breadth)
- {
- this.length = length;
- this.breadth = breadth;
- }
- @Override
- double getArea()
- {
- return length * breadth;
- }
- @Override
- double getPerimeter()
- {
- return 2 * (length + breadth);
- }
- }
- // A class for circles
- static class Circle extends GeometricFigure {
- double radius;
- Circle(double radius)
- {
- this.radius = radius;
- }
- @Override
- double getArea()
- {
- return 3.14 * radius * radius;
- }
- @Override
- double getPerimeter()
- {
- return 2 * 3.14 * radius;
- }
- }
- // Class for the fabric pattern
- static class GeometricFigureFactory {
- public static GeometricFigure getFigure(String type)
- {
- if (type == null) {
- return null;
- }
- if (type.equalsIgnoreCase("RECTANGLE")) {
- return new Rectangle(10, 20);
- }
- else if (type.equalsIgnoreCase("CIRCLE")) {
- return new Circle(5);
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement