Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gd;
- import java.util.ArrayList;
- /**
- * A shape consisting of multiple shapes held in an arraylist (UNLIMITED POWER!)
- * @author hypesystem
- * @email [email protected]
- */
- public class Composite extends Shape {
- private ArrayList<Shape> shapes;
- /**
- * Creates a composite shape consisting of serveral other shapes (that can
- * be either composite shapes themselves or simple shapes).
- * @param shapes Array of shapes to consist of
- */
- public Composite(ArrayList<Shape> shapes) {
- super(null);
- this.shapes = shapes;
- }
- /**
- * Returns the area of this composite shape (the sum of the areas of the
- * simple shapes).
- * @return area
- */
- @Override
- public double area() {
- double area = 0;
- for(Shape shape : shapes) {
- area += shape.area();
- }
- return area;
- }
- /**
- * Notice the text before the circumference method in TwoShapes for comments
- * on this method.
- * @return sum of circumferences of simple shapes
- */
- @Override
- public double circumference() {
- double circ = 0;
- for(Shape shape : shapes) {
- circ += shape.circumference();
- }
- return circ;
- }
- /**
- * Returns the amount of simple shapes in this composite shape
- * @return amount of simple shapes
- */
- @Override
- public int countSimple() {
- int simple = 0;
- for(Shape shape : shapes) {
- simple += shape.countSimple();
- }
- return simple;
- }
- /**
- * Adds a shape to the composite
- * @param shape shape to add
- */
- public void addShape(Shape shape) {
- shapes.add(shape);
- }
- /**
- * Removes a shape from the composite by its index number
- * @param index index of shape to remove
- */
- public void removeShape(int index) {
- shapes.remove(index);
- }
- /**
- * Removes a shape from the composite
- * @param shape shape to remove
- */
- public void removeShape(Shape shape) {
- shapes.remove(shape);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment