Computer Science 132
Introduction To Computing II

Dickinson College
Spring Semester 2001
Grant Braught

```
import java.awt.*;

/**
* Box3D.java -
*     Creates an object that represents a 3d box by extending
*     the java.awt.Rectangle class.
*
* @author Grant William Braught
* @author Dickinson College
* @version 4/18/2001
*/
public class Box3D extends Rectangle {

private int z;       // z coordinate of front, upper, left corner.
private int depth;

/**
* Constructs a new 3dBox whose front-upper-left corner is at (x,y,z),
* with width, height and depth specified.
*
* @param x x coordinate of front-upper-left corner.
* @param y y coordinate of front-upper-left corner.
* @param z z coordinate of front-upper-left corner.
* @param width width of the box.
* @param height height of the box.
* @param depth depth of the box.
*/
public Box3D(int x, int y, int z, int width, int height, int depth) {
super(x,y,width,height);
this.z = z;
this.depth = depth;
}

/**
* Returns the depth of the 3d box.
*
* @return the depth of this box.
*/
public int getDepth() {
return depth;
}

/**
* Returns the z coordinate of the 3d box.
*
* @return the z coordinate of this box.
*/
public int getZ() {
return z;
}

/**
* Compute the volume of the 3D box.
*
* @return the volume of this box.
*/
public int getVolume() {
return (int)getWidth() * (int)getHeight() * depth;
}

/**
* Set the location of the box.
*
* @param x x coordinate of front-upper-left corner.
* @param y y coordinate of front-upper-left corner.
* @param z z coordinate of front-upper-left corner.
*/
public void setLocation(int x, int y, int z) {
this.z = z;
// Set the location of the inherited Rectangle.
super.setLocation(x,y);
}

/**
* Set the size of the box.
*
* @param width width of the box.
* @param height height of the box.
* @param depth depth of the box.
*/
public void setSize(int width, int height, int depth) {
this.depth = depth;

// Set the size of the underlying Rectangle
super.setSize(width,height);
}

/**
* Determines if the point (x,y,z) is within the box.
*
* @return true if (x,y,z) is contained within this box;
*         false otherwise.
*/
public boolean contains(int x, int y, int z) {

// See if the x,y coordinates are inside the rectangle
// then check to see if the z coordinate is inside the
// box.
if (super.contains(x,y) &&
z > this.z && z < (this.z + depth)) {
return true;
}
else {
return false;
}
}

/**
* Determine if the 3dBox is within this 3dBox.
*
* @return true if the specified box is inside this box;
*         false otherwise.
*/
public boolean contains(Box3D box) {
// Check if the rectangle face of box is contained
// within the rectangle face of this box. Then check
// to see if the depths of the boxes are such that box
// is contained in this box.
if (super.contains(box) &&
z < box.z && z+depth > box.z + box.depth) {
return true;
}
else {
return false;
}
}

/**
* Determines if this box the specified box are equal.
*
* @return true if this box and box are equal;
*         false otherwise.
*/
public boolean equals(Box3D box) {
return super.equals(box) &&
z == box.z &&
depth == box.depth;
}

/**
* Returns a Sring representing this 3 dimensional box.
*
* @return a String representation of this box.
*/
public String toString() {
return "(" + (int)getX() + "," +
(int)getY() + "," +
(int)getZ() + ")" +
" width: " + (int)getWidth() +
" height: " + (int)getHeight() +
" depth: " + depth + ".";
}
}

```