-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBox.java
More file actions
178 lines (161 loc) · 3.97 KB
/
Box.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
package packalg;
/**
* PackAlg
* By Arwid Bancewicz, October 19, 2009
*/
// Imports
import java.util.Arrays;
import java.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
*/
public class Box extends Shape {
// Fields
protected int iiWidth, iiLength, iiHeight; // dimensions
/**
* Class constructor with specified dimensions.
*/
public Box(int aiWidth, int aiLength, int aiHeight) {
super();
this.iiWidth = aiWidth;
this.iiLength = aiLength;
this.iiHeight = aiHeight;
}
/**
* Class constructor with specified dimensions and position.
*/
public Box(int aiWidth, int aiLength, int aiHeight, int aiX, int aiY,int aiZ) {
super.setPosition(aiX, aiY, aiZ);
this.iiWidth = aiWidth;
this.iiLength = aiLength;
this.iiHeight = aiHeight;
}
/**
* @see Shape#getArea()
*/
@Override
public int getArea() {
return this.iiWidth * this.iiLength;
}
/**
* @see Shape#getVolume()
*/
@Override
public int getVolume() {
return this.iiWidth * this.iiLength * this.iiHeight;
}
/**
* @see Shape#rotateXY()
*/
@Override
public void rotateXY() {
// swap width and length
int loWidth = this.iiWidth;
this.iiWidth = this.iiLength;
this.iiLength = loWidth;
}
/**
* @see Shape#canContain(Shape)
*/
@Override
public boolean canContain(Shape aoShape) {
if (aoShape instanceof Box)
return canContain((Box) aoShape);
return false;
}
/**
* Box implementation.
* @see Shape#canContain(Shape)
*/
public boolean canContain(Box aoBox) {
return (aoBox.iiWidth <= this.iiWidth
&& aoBox.iiLength <= this.iiLength && aoBox.iiHeight <= this.iiHeight);
}
/**
* @see Shape#attemptToContain(Shape)
*/
@Override
public boolean attemptToContain(Shape aoShape) {
if (aoShape instanceof Box)
return attemptToContain((Box) aoShape);
return false;
}
/**
* Box implementation.
* @see Shape#attemptToContain(Shape)
*/
public boolean attemptToContain(Box aoBox) {
if (canContain(aoBox))
return true;
rotateXY();
if (canContain(aoBox))
return true;
rotateXY(); // back to default
return false;
}
/**
* @see Shape#breakUp(Shape)
*/
@Override
public List<Shape> breakUp(Shape aoShape) {
if (aoShape instanceof Box)
return breakUp((Box) aoShape);
return null;
}
/**
* Box implementation (Assumes this shape can fit into specified shape).
* @see Shape#breakUp(Shape)
*/
public List<Shape> breakUp(Box aoBox) {
Shape loSubBoxX = new Box(aoBox.iiWidth - this.iiWidth, aoBox.iiLength,
aoBox.iiHeight, this.iiX + this.iiWidth, aoBox.iiY, aoBox.iiZ);
Shape loSubBoxY = new Box(this.iiWidth, aoBox.iiLength - this.iiLength,
aoBox.iiHeight, this.iiX, aoBox.iiY + this.iiLength, aoBox.iiZ);
Shape loSubBoxZ = new Box(this.iiWidth, this.iiLength, aoBox.iiHeight
- this.iiHeight, this.iiX, aoBox.iiY, aoBox.iiZ + this.iiHeight);
return Arrays.asList(loSubBoxX, loSubBoxY, loSubBoxZ);
}
/**
* Returns a string formated representation of box's dimensions and position.
* @see Shape#toFullString()
*/
@Override
public String toFullString() {
return "Box [Width=" + iiWidth + ", Length=" + iiLength + ", Height="
+ iiHeight + ", Position=" + super.toString() + "]";
}
/**
* Returns a string formated representation of box's dimensions
* @see Shape#toString()
*/
@Override
public String toString() {
return "Box [Width=" + iiWidth + ", Length=" + iiLength + ", Height="
+ iiHeight + "]";
}
/**
* Compares this box to the specified object.
* @see Shape#equals(Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
if (!super.equals(obj))
return false;
Box other = (Box) obj;
if (iiWidth != other.iiWidth || iiLength != other.iiLength
|| iiHeight != other.iiHeight)
return false;
return true;
}
}