3
\$\begingroup\$

The missing code in task 1 were the methodsarea(), perimeter() and toString().

In task 2 I know that using built in collection such as Arraylist would be efficient and simpler,So any new different idea and implementation are appreciated.

  1. Make the classes Circle and Rectangle complete: write the missing code.

  2. A static method selectRectangle accepts an array of areas of type Region, and returns an array that only contains the areas that are of type Rectangle. Create that method.

  3. Create an array that contains both circles (objects of type Circle) and rectangles (objects of type Rectangle). Write code that determines and shows the perimeter and area of these regions. Call the method selectRectangles with the created array as argument.

public interface Region {
    double area();
    double perimeter();
}

class Circle implements Region {
    private double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    @Override
    public double area() {
        return Math.PI * radius;
    }

    @Override
    public double perimeter() {

        return Math.PI * 2 * radius;
    }
}

class Rectangle implements Region {
    private double with;
    private double length;

    public Rectangle(double with, double length) {
        this.length = length;
        this.with = with;
    }

    @Override
    public double area() {
        return length * with;
    }

    @Override
    public double perimeter() {
        return 2 * (length + with);
    }

    public String toString() {
       return "<" + this.with + this.with + this.area() + this.perimeter() + ">";
    }

    public static Rectangle[] selectRectangle(Region[] region) {
        int countRect = 0;

        for (int i = 0; i < region.length; i++) {
            if (region[i] instanceof Rectangle) {
                countRect++;
            }
        }
        Rectangle[] rect = new Rectangle[countRect];
        int companionVar = 0;
        for (int i = 0; i < region.length; i++) {
            if (region[i] instanceof Rectangle) {
                rect[companionVar++] = (Rectangle) region[i];
            }
        }
        return rect;
    }

    public static void main(String[] args) {
        Region[] region = {new Circle(5),
                new Rectangle(2, 4),
                new Circle(2),
                new Rectangle(7, 9),
                new Circle(6)

        };

        for (int i = 0; i < region.length; i++) {
            System.out.println(region[i].area() + region[i].perimeter());
        }

        Rectangle[] rectangle = selectRectangle(region);


    }

}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Area of a circle = pi * r * r (radius squared) \$\endgroup\$ Commented Apr 5, 2017 at 7:12

2 Answers 2

1
\$\begingroup\$

In my point of view you cannot implement area() and perimeter() any clearer or simpler.

In the toString() method I would somehow separate the values and also not print with two times and length not at all. Here is my suggestion:

 public String toString() {
       return "<" + with + " x "+ length + ", area:" + this.area() + " perimeter: "+ perimeter() + ">";
    }

In Rectangle[] selectRectangle(Region[] region) I would use an ArrayList to avoid the loop to count the number of Rectangles.

public static Rectangle[] selectRectangle(Region[] region) {
    ArrayList<Rectangle> result = new ArrayList<>();
    final int lastIndex = region.length;
    for (int i = 0; i < lastIndex; i++) {
        if (region[i] instanceof Rectangle) {
            result.add((Rectangle) region[i]);
        }
    }
    return result.toArray(new Rectangle[result.size()]);
}
\$\endgroup\$
2
  • \$\begingroup\$ Is it necessary to declare lastIndex variable ? Isn't it simpler to just have region.length instead? \$\endgroup\$
    – Adam
    Commented Apr 4, 2017 at 12:59
  • 1
    \$\begingroup\$ @Adam: No its not needed. Just a mini optimization to avoid validation of region.length during every loop cycle. \$\endgroup\$
    – MrSmith42
    Commented Apr 5, 2017 at 8:13
1
\$\begingroup\$

For task 2, you could use Java8 Stream api for selectRectangle

  public static Rectangle[] selectRectangle(Region[] regions) {
        return Arrays.stream( regions )
                     .filter( r -> r instanceof Rectangle )
                     .toArray( Rectangle[]::new );
  }

Btw, just a matter of taste, but I prefer variables that are a list or array of objects, to be plural. This allows you so see quickly that you are dealing with multiple object by just looking at the variable name.

\$\endgroup\$
2
  • \$\begingroup\$ First time i see this r -> and :: can you explain it a little? \$\endgroup\$
    – Adam
    Commented Apr 5, 2017 at 8:05
  • 1
    \$\begingroup\$ Please read the Stream docs, they are very good at explaining these nice Java 8 features: stackoverflow.com/documentation/java/88. The -> is a 'lambda' and the :: is a method reference. \$\endgroup\$ Commented Apr 5, 2017 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.