patternjavaMinor
Finding the samallest yellow rectangle
Viewed 0 times
therectangleyellowfindingsamallest
Problem
I have implemented the following methods in
```
public class Rectangle {
// the lengths of the rectangle’s sides
private double length;
private double width;
// the rectangle’s color
private String color;
public Rectangle(double length, double width, String color) {
this.length = length;
this.width = width;
this.color = color;
}
public String getColor() {
return color;
}
public double area() {
return length * width;
}
public String toString() {
return "";
}
public static Rectangle minRectangle(Rectangle[] rect) {
Rectangle min = rect[0];
for (int i = 0; i < rect.length; i++) {
if (rect[i].area() < min.area()) {
min = rect[i];
}
}
return min;
}
public static Rectangle[] selectRectangles(Rectangle[] rect, String color) {
int countRect = 0;
for (int i = 0; i < rect.length; i++) {
if (rect[i].getColor().equals(color))
countRect++;
}
Rectangle[] Rect = new Rectangle[countRect];
int companionVar = 0;
for (int j = 0; j < rect.length; j++) {
if(rect[j].getColor().equals(color))
Rect[companionVar++] = rect[j];
}
return Rect;
}
public static void main(String[] args) {
Rectangle[] rect = {new Rectangle(2, 3, "yellow"),
new
1 and 2 and need feedback to improve my implementation. Any different approach and ideas are welcome.- A static method, minRectangle, accepts an array of rectangles (objects of type Rectangle) and returns the rectangle with the smallest area. Create this method.
- A static method, selectRectangles, accepts an array of rectangles (objects of type Rectangle) and a color (an object of type String), and returns those rectangles (as an array) that are of the given color. Create this method.
- Create an array of rectangles (objects of type Rectangle). Then use the methods selectRectangles and minRectangle to determine the smallest of the rectangles in the array that are yellow.
```
public class Rectangle {
// the lengths of the rectangle’s sides
private double length;
private double width;
// the rectangle’s color
private String color;
public Rectangle(double length, double width, String color) {
this.length = length;
this.width = width;
this.color = color;
}
public String getColor() {
return color;
}
public double area() {
return length * width;
}
public String toString() {
return "";
}
public static Rectangle minRectangle(Rectangle[] rect) {
Rectangle min = rect[0];
for (int i = 0; i < rect.length; i++) {
if (rect[i].area() < min.area()) {
min = rect[i];
}
}
return min;
}
public static Rectangle[] selectRectangles(Rectangle[] rect, String color) {
int countRect = 0;
for (int i = 0; i < rect.length; i++) {
if (rect[i].getColor().equals(color))
countRect++;
}
Rectangle[] Rect = new Rectangle[countRect];
int companionVar = 0;
for (int j = 0; j < rect.length; j++) {
if(rect[j].getColor().equals(color))
Rect[companionVar++] = rect[j];
}
return Rect;
}
public static void main(String[] args) {
Rectangle[] rect = {new Rectangle(2, 3, "yellow"),
new
Solution
The
The selectRectangles() method is doing too much work as well. By using an
Please don't omit braces
The last task in question isn't solved correctly. You should first get the yellow rectangles in the array and then detect the smallest of these.
minRectangle is doing what it should but could be improved by storing the value of area() in a variable. Assume you have 1.000.000 rectangles to check then the calculation would happen 2.000.000 times.public static Rectangle minRectangle(Rectangle[] rect) {
Rectangle min = rect[0];
double minArea = min.area();
for (int i = 0; i < rect.length; i++) {
double currentArea = rect[i].area();
if (currentArea < minArea) {
min = rect[i];
minArea = currentArea;
}
}
return min;
}The selectRectangles() method is doing too much work as well. By using an
List to add the rectangles with the desired color and returning the list as array will speed this up.Please don't omit braces
{} although they might be optional. Omitting braces can lead to hidden and therfor hard to track bugs.public static Rectangle[] selectRectangles(Rectangle[] rect, String color) {
List rectangles = new ArrayList<>();
int countRect = 0;
for (int i = 0; i < rect.length; i++) {
if (rect[i].getColor().equals(color)) {
rectangles.add(rect[i]);
}
}
Rectangle[] results = new Rectangle[rectangles.size()];
return rectangles.toArray(results);
}The last task in question isn't solved correctly. You should first get the yellow rectangles in the array and then detect the smallest of these.
Code Snippets
public static Rectangle minRectangle(Rectangle[] rect) {
Rectangle min = rect[0];
double minArea = min.area();
for (int i = 0; i < rect.length; i++) {
double currentArea = rect[i].area();
if (currentArea < minArea) {
min = rect[i];
minArea = currentArea;
}
}
return min;
}public static Rectangle[] selectRectangles(Rectangle[] rect, String color) {
List<Rectangle> rectangles = new ArrayList<>();
int countRect = 0;
for (int i = 0; i < rect.length; i++) {
if (rect[i].getColor().equals(color)) {
rectangles.add(rect[i]);
}
}
Rectangle[] results = new Rectangle[rectangles.size()];
return rectangles.toArray(results);
}Context
StackExchange Code Review Q#159434, answer score: 4
Revisions (0)
No revisions yet.