Created
December 14, 2014 08:30
-
-
Save jnizet/a645b7efae4488ad8125 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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