Skip to content

Instantly share code, notes, and snippets.

@jnizet
Created December 14, 2014 08:30
Show Gist options
  • Save jnizet/a645b7efae4488ad8125 to your computer and use it in GitHub Desktop.
Save jnizet/a645b7efae4488ad8125 to your computer and use it in GitHub Desktop.
public final class Rectangle {
private final int width;
private final int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public int area() {
return width * height;
}
public int perimeter() {
return 2 * (width + height);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class RectangleStats {
public static int combinedArea(Collection<Rectangle> rectangles) {
int sum = 0;
for (Rectangle rectangle : rectangles) {
sum += rectangle.area();
}
return sum;
}
public static int combinedPerimeter(Collection<Rectangle> rectangles) {
int sum = 0;
for (Rectangle rectangle : rectangles) {
sum += rectangle.perimeter();
}
return sum;
}
public static void main(String[] args) {
List<Rectangle> rectangles = new ArrayList<>();
rectangles.add(new Rectangle(10, 5));
rectangles.add(new Rectangle(4, 3));
System.out.println("Combined area = " + RectangleStats.combinedArea(rectangles));
System.out.println("Combined perimeter = " + RectangleStats.combinedPerimeter(rectangles));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment