Guest User

Untitled

a guest
May 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. public class ShapesContainer{
  2. //state
  3. public static final int INIT_SIZE=10;
  4. public static final int RESIZE=10;
  5. private int numOfShapes=0;
  6. private Shape[] shapes; //array of shapes
  7. //constructours
  8. /**
  9. * constructor #1
  10. * creates an empty set of shapes
  11. */
  12. public ShapesContainer(){
  13. Shape[] shapes=new Shape[INIT_SIZE];
  14. for(int i=0;i<INIT_SIZE; i++){
  15. shapes[i]=null;
  16. }
  17. }
  18. /**
  19. * constructor #2
  20. * copy the shapes(shallow copy)
  21. * @param other
  22. */
  23. public ShapesContainer(ShapesContainer other){
  24. if(other!=null){
  25. Shape[] shapes=new Shape[other.shapes.length];
  26. for(int i=0;i<shapes.length;i++){
  27. shapes[i]=other.shapes[i];
  28. }
  29. }
  30. else
  31. throw new RuntimeException("other is null");
  32. }
  33. //behavior
  34. /**
  35. *
  36. * @return number of shapes
  37. */
  38. public int getShapesNum(){
  39. return numOfShapes;
  40. }
  41. /**
  42. *
  43. * @param newShape
  44. * @return true if succeed to set new shape
  45. */
  46. public boolean add(Shape newShape){
  47. for(int i=0;i<numOfShapes;i++){
  48. int j=0;
  49. if(newShape.getArea()>shapes[i].getArea()){
  50. if(numOfShapes<shapes.length){
  51. Shape[] temp=new Shape[shapes.length+RESIZE];
  52. for(j=0;j<shapes.length;j++){
  53. temp[j]=shapes[j];
  54. }
  55. for(j=shapes.length-1;j<temp.length;j++){
  56. temp[j]=null;
  57. }
  58. shapes=temp;
  59. }
  60. for(j=numOfShapes;j>=i;j++){
  61. shapes[j+1]=shapes[j];
  62. }
  63. shapes[j]=newShape;
  64. numOfShapes=numOfShapes+1;
  65. return true;
  66. }
  67. if(newShape.getArea()==shapes[i].getArea() && newShape==shapes[i]){
  68. return false;
  69. }
  70. }
  71. shapes[numOfShapes]=newShape;
  72. numOfShapes=numOfShapes+1;
  73. return true;
  74. }
  75. /**
  76. *
  77. * @param toRemove
  78. * @return true if succeed to remove the shape
  79. */
  80. public boolean remove(Shape toRemove){
  81. for(int i=0; i<numOfShapes; i++){
  82. if(toRemove.getArea()==shapes[i].getArea() && toRemove==shapes[i]){
  83. for(int j=i;j<numOfShapes; j++){
  84. shapes[j]=shapes[j+1];
  85. }
  86. shapes[numOfShapes]=null;
  87. numOfShapes=numOfShapes-1;
  88. return true;
  89. }
  90. }
  91. return false;
  92. }
  93. /**
  94. *
  95. * @param i
  96. * @return true if succeed to remove the shape
  97. */
  98. public boolean remove(int i){
  99. if(i>=0 && i<=numOfShapes){
  100. remove(shapes[i]);
  101. return true;
  102. }
  103. else
  104. return false;
  105. }
  106. public Shape getShape(int i){
  107. if(i>=0 && i<=numOfShapes){
  108. return shapes[i];
  109. }
  110. else
  111.  
  112. }
  113. }
Add Comment
Please, Sign In to add comment