// package base; import java.util.*; abstract class Item { String name; public abstract int weight(); } class Container extends Item { Vector elements; int capacity; public Container(String name, int cap) { this.name = name; this.capacity = cap; this.elements = new Vector(); } public void addItem(Item i) { /*\label{line:cont-addItem}*/ elements.add(i); } public int weight() { /*\label{line:cont-weight}*/ Iterator it = elements.iterator(); int total = 0; while (it.hasNext()) { Item child = (Item)it.next(); total += child.weight(); } System.out.println( "Container " + name + " weighs " + total); return total; } public void check() { /*\label{line:cont-check}*/ int total = weight(); if (total>capacity){ System.out.println( "Container " + name + " overloaded"); } } /* Q: What is this for? A: because we hate constructors.*/ // public static Container make( // String n,int c) { // return new Container(n,c); // } }