- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBox.java
More file actions
Latest commit
178 lines (161 loc) · 3.97 KB
/
Copy pathBox.java
File metadata and controls
178 lines (161 loc) · 3.97 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
packagepackalg;
/**
* PackAlg
* By Arwid Bancewicz, October 19, 2009
*/
// Imports
importjava.util.Arrays;
importjava.util.List;
/**
* Box.java - A simple class represents a 3-dimensional rectangular object
* with position and dimensions.
*
* @author Arwid Bancewicz
* @version 1.2, (09/19/09)
* @see Shape
*/
publicclassBoxextendsShape {
// Fields
protectedintiiWidth, iiLength, iiHeight; // dimensions
/**
* Class constructor with specified dimensions.
*/
publicBox(intaiWidth, intaiLength, intaiHeight) {
super();
this.iiWidth = aiWidth;
this.iiLength = aiLength;
this.iiHeight = aiHeight;
}
/**
* Class constructor with specified dimensions and position.
*/
publicBox(intaiWidth, intaiLength, intaiHeight, intaiX, intaiY,intaiZ) {
super.setPosition(aiX, aiY, aiZ);
this.iiWidth = aiWidth;
this.iiLength = aiLength;
this.iiHeight = aiHeight;
}
/**
* @see Shape#getArea()
*/
@Override
publicintgetArea() {
returnthis.iiWidth * this.iiLength;
}
/**
* @see Shape#getVolume()
*/
@Override
publicintgetVolume() {
returnthis.iiWidth * this.iiLength * this.iiHeight;
}
/**
* @see Shape#rotateXY()
*/
@Override
publicvoidrotateXY() {
// swap width and length
intloWidth = this.iiWidth;
this.iiWidth = this.iiLength;
this.iiLength = loWidth;
}
/**
* @see Shape#canContain(Shape)
*/
@Override
publicbooleancanContain(ShapeaoShape) {
if (aoShapeinstanceofBox)
returncanContain((Box) aoShape);
returnfalse;
}
/**
* Box implementation.
* @see Shape#canContain(Shape)
*/
publicbooleancanContain(BoxaoBox) {
return (aoBox.iiWidth <= this.iiWidth
&& aoBox.iiLength <= this.iiLength && aoBox.iiHeight <= this.iiHeight);
}
/**
* @see Shape#attemptToContain(Shape)
*/
@Override
publicbooleanattemptToContain(ShapeaoShape) {
if (aoShapeinstanceofBox)
returnattemptToContain((Box) aoShape);
returnfalse;
}
/**
* Box implementation.
* @see Shape#attemptToContain(Shape)
*/
publicbooleanattemptToContain(BoxaoBox) {
if (canContain(aoBox))
returntrue;
rotateXY();
if (canContain(aoBox))
returntrue;
rotateXY(); // back to default
returnfalse;
}
/**
* @see Shape#breakUp(Shape)
*/
@Override
publicList<Shape> breakUp(ShapeaoShape) {
if (aoShapeinstanceofBox)
returnbreakUp((Box) aoShape);
returnnull;
}
/**
* Box implementation (Assumes this shape can fit into specified shape).
* @see Shape#breakUp(Shape)
*/
publicList<Shape> breakUp(BoxaoBox) {
ShapeloSubBoxX = newBox(aoBox.iiWidth - this.iiWidth, aoBox.iiLength,
aoBox.iiHeight, this.iiX + this.iiWidth, aoBox.iiY, aoBox.iiZ);
ShapeloSubBoxY = newBox(this.iiWidth, aoBox.iiLength - this.iiLength,
aoBox.iiHeight, this.iiX, aoBox.iiY + this.iiLength, aoBox.iiZ);
ShapeloSubBoxZ = newBox(this.iiWidth, this.iiLength, aoBox.iiHeight
- this.iiHeight, this.iiX, aoBox.iiY, aoBox.iiZ + this.iiHeight);
returnArrays.asList(loSubBoxX, loSubBoxY, loSubBoxZ);
}
/**
* Returns a string formated representation of box's dimensions and position.
* @see Shape#toFullString()
*/
@Override
publicStringtoFullString() {
return"Box [Width=" + iiWidth + ", Length=" + iiLength + ", Height="
+ iiHeight + ", Position=" + super.toString() + "]";
}
/**
* Returns a string formated representation of box's dimensions
* @see Shape#toString()
*/
@Override
publicStringtoString() {
return"Box [Width=" + iiWidth + ", Length=" + iiLength + ", Height="
+ iiHeight + "]";
}
/**
* Compares this box to the specified object.
* @see Shape#equals(Object)
*/
@Override
publicbooleanequals(Objectobj) {
if (this == obj)
returntrue;
if (obj == null)
returnfalse;
if (getClass() != obj.getClass())
returnfalse;
if (!super.equals(obj))
returnfalse;
Boxother = (Box) obj;
if (iiWidth != other.iiWidth || iiLength != other.iiLength
|| iiHeight != other.iiHeight)
returnfalse;
returntrue;
}
}