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.
Make the classes Circle and Rectangle complete: write the missing code.
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.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);
}
}
-
1\$\begingroup\$ Area of a circle = pi * r * r (radius squared) \$\endgroup\$Rob Audenaerde– Rob Audenaerde2017年04月05日 07:12:38 +00:00Commented Apr 5, 2017 at 7:12
2 Answers 2
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()]);
}
-
\$\begingroup\$ Is it necessary to declare
lastIndex
variable ? Isn't it simpler to just haveregion.length
instead? \$\endgroup\$Adam– Adam2017年04月04日 12:59:58 +00:00Commented 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– MrSmith422017年04月05日 08:13:14 +00:00Commented Apr 5, 2017 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.
-
\$\begingroup\$ First time i see this
r ->
and::
can you explain it a little? \$\endgroup\$Adam– Adam2017年04月05日 08:05:14 +00:00Commented 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\$Rob Audenaerde– Rob Audenaerde2017年04月05日 08:19:01 +00:00Commented Apr 5, 2017 at 8:19