# Finding area and perimeter of rectangles and circles that are instance of interface region

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 {

}

@Override
public double area() {
}

@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);

}

}

• Area of a circle = pi * r * r (radius squared) – RobAu Apr 5 '17 at 7:12

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) {
}
}
return result.toArray(new Rectangle[result.size()]);
}

• Is it necessary to declare lastIndex variable ? Isn't it simpler to just have region.length instead? – Adam Apr 4 '17 at 12:59
• @Adam: No its not needed. Just a mini optimization to avoid validation of region.length during every loop cycle. – MrSmith42 Apr 5 '17 at 8:13

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.

• First time i see this r -> and :: can you explain it a little? – Adam Apr 5 '17 at 8:05
• 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. – RobAu Apr 5 '17 at 8:19