Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ShapesContainer{
- //state
- public static final int INIT_SIZE=10;
- public static final int RESIZE=10;
- private int numOfShapes=0;
- private Shape[] shapes; //array of shapes
- //constructours
- /**
- * constructor #1
- * creates an empty set of shapes
- */
- public ShapesContainer(){
- Shape[] shapes=new Shape[INIT_SIZE];
- for(int i=0;i<INIT_SIZE; i++){
- shapes[i]=null;
- }
- }
- /**
- * constructor #2
- * copy the shapes(shallow copy)
- * @param other
- */
- public ShapesContainer(ShapesContainer other){
- if(other!=null){
- Shape[] shapes=new Shape[other.shapes.length];
- for(int i=0;i<shapes.length;i++){
- shapes[i]=other.shapes[i];
- }
- }
- else
- throw new RuntimeException("other is null");
- }
- //behavior
- /**
- *
- * @return number of shapes
- */
- public int getShapesNum(){
- return numOfShapes;
- }
- /**
- *
- * @param newShape
- * @return true if succeed to set new shape
- */
- public boolean add(Shape newShape){
- for(int i=0;i<numOfShapes;i++){
- int j=0;
- if(newShape.getArea()>shapes[i].getArea()){
- if(numOfShapes==shapes.length){
- Shape[] temp=new Shape[shapes.length+RESIZE];
- for(j=0;j<shapes.length;j++){
- temp[j]=shapes[j];
- }
- for(j=shapes.length-1;j<temp.length;j++){
- temp[j]=null;
- }
- shapes=temp;
- }
- for(j=numOfShapes;j>=i;j--){
- shapes[j+1]=shapes[j];
- }
- shapes[j]=newShape;
- numOfShapes=numOfShapes+1;
- return true;
- }
- if(newShape.getArea()==shapes[i].getArea() && newShape==shapes[i]){
- return false;
- }
- }
- shapes[numOfShapes]=newShape;
- numOfShapes=numOfShapes+1;
- return true;
- }
- /**
- *
- * @param toRemove
- * @return true if succeed to remove the shape
- */
- public boolean remove(Shape toRemove){
- for(int i=0; i<numOfShapes; i++){
- if(toRemove.getArea()==shapes[i].getArea() && toRemove==shapes[i]){
- for(int j=i;j<numOfShapes; j++){
- shapes[j]=shapes[j+1];
- }
- shapes[numOfShapes]=null;
- numOfShapes=numOfShapes-1;
- return true;
- }
- }
- return false;
- }
- /**
- *
- * @param i
- * @return true if succeed to remove the shape
- */
- public boolean remove(int i){
- if(i>=0 && i<=numOfShapes){
- remove(shapes[i]);
- return true;
- }
- else
- return false;
- }
- /**
- *
- * @param i
- * @return shape(i)
- */
- public Shape getShape(int i){
- if(i>=0 && i<=numOfShapes){
- return shapes[i];
- }
- else
- throw new RuntimeException("i is out of bounds");
- }
- /**
- *
- * @return the sum of the areas of the shapes
- */
- public double sumArea(){
- double sumArea=0;
- for(int i=0;i<=numOfShapes;i++){
- sumArea=sumArea+shapes[i].getArea();
- }
- return sumArea;
- }
- /**
- *
- * @return the sum of the perimeters of the shapes
- */
- public double sumPerimeters(){
- double sumPer=0;
- for(int i=0;i<=numOfShapes;i++){
- sumPer=sumPer+shapes[i].getPerimeter();
- }
- return sumPer;
- }
- /**
- * move the shapes(p)
- * @param p
- */
- public void move(Point p){
- for(int i=0;i<=numOfShapes;i++){
- shapes[i].move(p);
- }
- }
- }
Add Comment
Please, Sign In to add comment