import java.util.ArrayList; public class Box { private ArrayList internalBoxes; // A constructor that makes a box with no boxes in it public Box() { internalBoxes = new ArrayList(); } // Get method public ArrayList getInternalBoxes() { return internalBoxes; } // Method to add a box to the internal boxes public void addBox(Box b) { internalBoxes.add(b); } // Method to remove a box from the internal boxes public void removeBox(Box b) { internalBoxes.remove(b); } // Unwrap the box and return the number of boxes altogether (including this one) public int unwrap() { if (internalBoxes.size() == 0) return 1; // Count this box int count = 1; // Count each of the inner boxes for (Box b: internalBoxes) count = count + b.unwrap(); return count; } // Unwrap the box and return the number of boxes altogether (including this one) public int unwrap2() { if (internalBoxes.size() == 0) return 1; // Remove one internal box, if there is one Box insideBox = internalBoxes.remove(0); // Unwrap the rest of this box as well as the one just removed return this.unwrap2() + insideBox.unwrap2(); } // Unwrap the box and return the number of boxes altogether (including this one) public int unwrap3() { if (internalBoxes.size() == 0) return 1; // Remove one internal box, if there is one Box insideBox = internalBoxes.remove(0); // Unwrap the rest of this box as well as the one just removed int result = this.unwrap3() + insideBox.unwrap3(); // Put the box back in at position 0 (i.e., same order) internalBoxes.add(0,insideBox); return result; } }