|
| 1 | +/** |
| 2 | + * A class that represent a collection of Box3D. |
| 3 | + * Boxes will be add through addBox function, which will also take care of the order. |
| 4 | + * |
| 5 | + * @author Ben Brandes |
| 6 | + * @version 13.04.19 |
| 7 | + */ |
| 8 | +public class Collection { |
| 9 | + private Box3D[] _boxes; |
| 10 | + private int _noOfBoxes; |
| 11 | + final int MAX_NUM_BOXES = 100; |
| 12 | + final int INITIAL_VAL = 0; |
| 13 | + |
| 14 | + /** |
| 15 | + * Constructor will initialize the array to the default size (100) |
| 16 | + * And set the noOfBoxes to 0 |
| 17 | + */ |
| 18 | + public Collection() { |
| 19 | + _boxes = new Box3D[MAX_NUM_BOXES]; |
| 20 | + _noOfBoxes = INITIAL_VAL; |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Will add a box to the array and return true if there is place for another box and false for failure |
| 25 | + * |
| 26 | + * @param base Point3D base for the Box |
| 27 | + * @param length Box's length |
| 28 | + * @param width Box's width |
| 29 | + * @param height Box's height |
| 30 | + * @return true - if the box added successfully, false - if the array is full |
| 31 | + */ |
| 32 | + public boolean addBox(Point3D base, int length, int width, int height) { |
| 33 | + if (_noOfBoxes == MAX_NUM_BOXES) { |
| 34 | + return false; |
| 35 | + } else { |
| 36 | + Box3D newBox = new Box3D(base, length, width, height); |
| 37 | + int index = this.findSuitableIndex(newBox); // Passing the box to private method that'll check for the right location |
| 38 | + this.freeIndex(index); // After finding the right location, move all the boxes to the right |
| 39 | + _boxes[index] = new Box3D(newBox); // Assign the new box to it's right location |
| 40 | + _noOfBoxes++; // Increase the number of boxes |
| 41 | + return true; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Create an array that contain all the boxes, the array size will be the number of boxes |
| 47 | + * |
| 48 | + * @return An array with a size of the number of boxes |
| 49 | + */ |
| 50 | + public Box3D[] getBoxes() { |
| 51 | + Box3D[] all = new Box3D[_noOfBoxes]; |
| 52 | + |
| 53 | + for (int i = 0; i < _noOfBoxes; i++) { |
| 54 | + all[i] = new Box3D(_boxes[i]); |
| 55 | + } |
| 56 | + |
| 57 | + return all; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Return the total number of the boxes in the array |
| 62 | + * |
| 63 | + * @return the total number of the boxes in the array |
| 64 | + */ |
| 65 | + public int getNumOfBoxes() { |
| 66 | + return _noOfBoxes; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Checks how many boxes in the array can contain the parameter box |
| 71 | + * |
| 72 | + * @param box the box that will be checked |
| 73 | + * @return how many boxes in the array can contain the parameter box |
| 74 | + */ |
| 75 | + public int howManyContains(Box3D box) { |
| 76 | + int count = 0; |
| 77 | + |
| 78 | + for (int i = 0; i < _noOfBoxes; i++) { |
| 79 | + if (_boxes[i].contains(box)) |
| 80 | + count++; |
| 81 | + } |
| 82 | + |
| 83 | + return count; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Return the longest distance between two boxes in the array |
| 88 | + * |
| 89 | + * @return the longest distance between two boxes in the array |
| 90 | + */ |
| 91 | + public double longestDistance() { |
| 92 | + if(_noOfBoxes < 2) |
| 93 | + return 0; |
| 94 | + |
| 95 | + double distance = 0; |
| 96 | + |
| 97 | + for (int i = 0; i < _noOfBoxes; i++) { |
| 98 | + for (int j = 0; j < _noOfBoxes; j++) { |
| 99 | + double d = _boxes[i].distance(_boxes[j]); |
| 100 | + if (d > distance) |
| 101 | + distance = d; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return distance; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Return the box the highest base point |
| 110 | + * If the array is empty, will return <code>null</code> |
| 111 | + * |
| 112 | + * @return The box with the highest base point, if empty will return <code>null</code> |
| 113 | + */ |
| 114 | + public Box3D mostUpperBaseCorner() { |
| 115 | + if (_noOfBoxes == 0) |
| 116 | + return null; |
| 117 | + |
| 118 | + Box3D highest = new Box3D(); |
| 119 | + for (int i = 0; i < _noOfBoxes; i++) { |
| 120 | + if (_boxes[i].getBase().isAbove(highest.getBase())) { |
| 121 | + highest = _boxes[i]; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return new Box3D(highest); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Return a string that'll display all the boxes |
| 130 | + * |
| 131 | + * @return a string that'll display all the boxes |
| 132 | + */ |
| 133 | + public String toString() { |
| 134 | + String boxes = ""; |
| 135 | + for (int i = 0; i < _noOfBoxes; i++) { |
| 136 | + boxes += "Box no. " + (i + 1) + ": " + _boxes[i].toString() + "\n"; |
| 137 | + } |
| 138 | + |
| 139 | + return boxes; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * Return the total of the surface area of all the boxes in the array |
| 144 | + * |
| 145 | + * @return the total of the surface area of all the boxes in the array |
| 146 | + */ |
| 147 | + public double totalSurfaceArea() { |
| 148 | + double area = 0; |
| 149 | + |
| 150 | + for (int i = 0; i < _noOfBoxes; i++) { |
| 151 | + area += _boxes[i].getSurfaceArea(); |
| 152 | + } |
| 153 | + |
| 154 | + return area; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Will check all the boxes within this index range and |
| 159 | + * return the volume of the smallest box that can contain all those boxes. |
| 160 | + * |
| 161 | + * @param i index within the array |
| 162 | + * @param j index within the array |
| 163 | + * @return return the volume of the smallest box that can contain all those boxes |
| 164 | + */ |
| 165 | + public int volumeOfSmallestBox(int i, int j) { |
| 166 | + // Checks if the indexes are valid |
| 167 | + if (i >= MAX_NUM_BOXES || j >= MAX_NUM_BOXES || i < 0 || j < 0 || j >= _noOfBoxes || i >= _noOfBoxes) |
| 168 | + return 0; |
| 169 | + |
| 170 | + int min = (i > j) ? i : j; |
| 171 | + int max = (j > i) ? j : i; |
| 172 | + |
| 173 | + int maxH = 0, maxW = 0, maxL = 0; |
| 174 | + |
| 175 | + for (int l = min; l <= max; l++) { |
| 176 | + if (_boxes[l].getHeight() >= maxH) |
| 177 | + maxH = _boxes[l].getHeight() + 1; |
| 178 | + if (_boxes[l].getLength() >= maxL) |
| 179 | + maxL = _boxes[l].getLength() + 1; |
| 180 | + if (_boxes[l].getWidth() >= maxW) |
| 181 | + maxW = _boxes[l].getWidth() + 1; |
| 182 | + } |
| 183 | + |
| 184 | + return maxH * maxL * maxW; |
| 185 | + } |
| 186 | + |
| 187 | + // Return the index of the best place for the new box |
| 188 | + private int findSuitableIndex(Box3D box) { |
| 189 | + for (int i = 0; i < _noOfBoxes; i++) { |
| 190 | + if (_boxes[i].getVolume() >= box.getVolume()) |
| 191 | + return i; |
| 192 | + } |
| 193 | + return _noOfBoxes; |
| 194 | + } |
| 195 | + |
| 196 | + // Will move all the boxes next to the index parameter 1 place right |
| 197 | + private void freeIndex(int index) { |
| 198 | + for (int i = _noOfBoxes; i > index; i--) { |
| 199 | + _boxes[i] = new Box3D(_boxes[i - 1]); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments