Finding the samallest yellow rectangle I have implemented the following methods in 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 "&lt;" + this.length + ", " + this.width + "," + this.color + "&gt;";

}

public static Rectangle minRectangle(Rectangle[] rect) {

    Rectangle min = rect[0];
    for (int i = 0; i &lt; rect.length; i++) {
        if (rect[i].area() &lt; min.area()) {
            min = rect[i];
        }
    }
    return min;

}

public static Rectangle[] selectRectangles(Rectangle[] rect, String color) {
    int countRect = 0;
    for (int i = 0; i &lt; rect.length; i++) {
        if (rect[i].getColor().equals(color))
            countRect++;

    }

    Rectangle[] Rect = new Rectangle[countRect];
    int companionVar = 0;
    for (int j = 0; j &lt; 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 Rectangle(3, 5, "red"),
            new Rectangle(1, 1, "yellow"),
            new Rectangle(2, 2, "red"),
            new Rectangle(3, 4, "red"),
            new Rectangle(2, 8, "red"),
            new Rectangle(5, 2, "yellow")};

    System.out.println("Smallest yellow Rectangle:" + minRectangle(rect));
    Rectangle[] rectangles = selectRectangles(rect, "yellow");
    System.out.println(Arrays.toString(rectangles));
    }

}
The 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 &lt; rect.length; i++) {
        double currentArea = rect[i].area();
        if (currentArea &lt; minArea) {
            min = rect[i];
            minArea = currentArea; 
        }
    }
    return min;
}




The selectRectangles() method is doing too much work as well. By using an List&lt;Rectangle&gt; 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&lt;Rectangle&gt; rectangles = new ArrayList&lt;&gt;();
    int countRect = 0;
    for (int i = 0; i &lt; 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.