-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.java
More file actions
197 lines (181 loc) · 5.47 KB
/
Collection.java
File metadata and controls
197 lines (181 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
* Write a description of class Collection here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Collection
{
// instance variables - replace the example below with your own
private int _noOfBoxes;
private Box3D [] _boxes;
public final int MAX_NUM_BOXES = 100;
/**
* Constructor for objects of class Collection
*/
public Collection()
{
// initialise instance variables
_boxes = new Box3D[MAX_NUM_BOXES]; //Allocate memory
}
private void insertBox(Box3D box)
{
for (int i=_boxes.length-1; i>=0; i--)
{
if (_boxes[i] != null)
{
if (box.isLargerCapacity(_boxes[i]))
{
_boxes[i+1] = new Box3D(box);
break;
}
else
{
_boxes[i+1] = new Box3D(_boxes[i]);
}
}
}
}
public boolean addBox(Point3D base, int length, int width, int height)
{
Box3D newBox = new Box3D(base, length, width, height);
//Check if there is space by seeing if the last box is empty
if (_boxes[_boxes.length -1] == null)
{
insertBox(newBox);
return true;
}
return false;
}
/**
* Finds and returns the box with the highest base point
* @return The box with the highest base point
*/
public Box3D mostUpperBaseCorner()
{
int max = 0;
Point3D maxPoint = new Point3D(_boxes[max].getBase());
// loop will go until and of array or empty box
for (int i=1; i < _boxes.length -1 && _boxes[i] != null; i++)
{
Point3D tempPoint = new Point3D(_boxes[i].getBase());
if (maxPoint.isUnder(tempPoint))
{
maxPoint = new Point3D(tempPoint);
max = i;
}
}
return _boxes[max];
}
/**
* Calculates the sum of the 3D boxes in the collection
* @return The total surface area of the collection's boxes
*/
public double totalSurfaceArea()
{
double surfaceSum =0;
for (int i=0; i < _boxes.length -1 && _boxes[i] != null; i++)
{
surfaceSum += _boxes[i].getSurface();
}
return surfaceSum;
}
/**
* Calculates the longest distance between the farthest boxes in the
* collection
* @return The longest distance between the base point of farthest boxes
*/
public double longestDistance()
{
double maxDistance = 0;
for (int i=0; i < _boxes.length -1 && _boxes[i] != null; i++)
{
for (int j=i+1; j < _boxes.length -1 && _boxes[j] != null; j++)
{
double currentDistance = _boxes[i].distance(_boxes[j]);
if (currentDistance > maxDistance)
maxDistance = currentDistance;
}
}
return maxDistance;
}
// Checks if number is in array
private boolean checkIndex(int number)
{
if (number >= MAX_NUM_BOXES || number < 0)
return false;
return true;
}
/**
* Returns the volume of the smallest 3D box that can contain
* any box between the indexes
* @param i an index in the array
* @param j another index in the array
* @return the volume of the smallest 3D box that can contain
* any box between the indexes
*/
public int volumeOfSmallestBox(int i, int j)
{
int bigger;
if (!checkIndex(i) || !checkIndex(i))
return 0;
// The array is in size order therefore the further down one is the biggest
else
{
if (i > j)
bigger = i;
else
bigger = j;
Box3D bigBox = new Box3D(_boxes[bigger]);
// in order to make the box bigger than the largest well add 1 to all parameters
bigBox.setLength(bigBox.getLength() + 1);
bigBox.setWidth(bigBox.getWidth() + 1);
bigBox.setHeight(bigBox.getHeight() + 1);
int biggestSize = bigBox.getVolume();
return biggestSize;
}
}
/**
* Returns the number of 3D boxes in the collection
* @return the amount of 3D Boxes in the collection
*/
public int getNumOfBoxes()
{
int counter = 0;
while (counter < _boxes.length -1 && _boxes[counter] != null)
{
counter++;
}
//adding 1 cause we started from 0
return counter + 1;
}
/**
* Returns a collection containing only the full part of given one
* @return An array containing a the full 3D boxes
*/
public Box3D[] getBoxes()
{
Box3D [] newBoxes;
int totalBoxes = getNumOfBoxes();
newBoxes = new Box3D[totalBoxes];
for (int i = 0; i < totalBoxes - 1; i++)
{
newBoxes[i] = new Box3D(_boxes[i]);
}
return newBoxes;
}
/**
* Returns the 3D boxes in Box3D format
* @return A string listing the 3D boxes in the collection
*/
public String toString()
{
String allBoxes = "";
int totalBoxes = getNumOfBoxes();
for (int i = 0; i < totalBoxes - 1; i++)
{
allBoxes = allBoxes + "\n Box no." + i + ": " + _boxes[i].toString();
}
return allBoxes;
}
}