ArrayBag Trace -------------- 2) The ArrayBag class has an object array called items and an integer called numItems. The object array serves as a storage area for items in the bag, and the numItems integer allows us to keep track of how many items are in the bag. We make the items array of type Object[] because Object is the superclass of every other class, and thus polymorphism allows us to store any type of object in this array. 3) Because the items don't have a position, we can't ask the bag for an item by position. Rather, we can only "reach into" the bag and "grab" an item at random. 4) Before the call to add(), we have: ---------------------------------- stack | heap +------------+ | main | ArrayBag object | +----+ | +------------------+ | b | --|---+--------->| +----+ | +------+------+------+---- | +----+ | | items | --|--+----->| null | null | null | ... +------------+ | +----+ | +------+------+------+---- | +----+ | | numItems | 0 | | | +----+ | +------------------+ At the start of the call b.add("don't blink"), we have: ------------------------------------------------------- stack | heap +-------------+ | add | | +----+ | +---------------+ | item | --|-+--------------------------->| "don't blink" | | +----+ | +---------------+ | | | +----+ | | this | --|-+--+ | +----+ | | +-------------+ | | main | | ArrayBag object | +----+ | +----->+------------------+ | b | --|-+-------->| +----+ | +------+------+------+---- | +----+ | | items | --|--+----->| null | null | null | ... +-------------+ | +----+ | +------+------+------+---- | +----+ | | numItems | 0 | | | +----+ | +------------------+ After executing this line from the add() method: this.items[this.numItems] = item; we have: ------------------------------------------------ stack | heap +-------------+ | add | | +----+ | +---------------+ | item | --|-+--------------------------->| "don't blink" | | +----+ | +---------------+ | | ^ | +----+ | | | this | --|-+--+ | | +----+ | | | +-------------+ | | | main | | ArrayBag object | | +----+ | +----->+------------------+ | | b | --|-+-------->| +----+ | +--+---+------+------+---- | +----+ | | items | --|--+----->| | | null | null | ... +-------------+ | +----+ | +------+------+------+---- | +----+ | | numItems | 0 | | | +----+ | +------------------+ After executing this line from the add() method: this.numItems++; we have: ------------------------------------------------ stack | heap +-------------+ | add | | +----+ | +---------------+ | item | --|-+--------------------------->| "don't blink" | | +----+ | +---------------+ | | ^ | +----+ | | | this | --|-+--+ | | +----+ | | | +-------------+ | | | main | | ArrayBag object | | +----+ | +----->+------------------+ | | b | --|-+-------->| +----+ | +--+---+------+------+---- | +----+ | | items | --|--+----->| | | null | null | ... +-------------+ | +----+ | +------+------+------+---- | +----+ | | numItems | 1 | | | +----+ | +------------------+ Note that the next time that we call add(), this.numItems will be 1, and the item will be assigned to position 1 of the array. After the method returns, we have: ---------------------------------- stack | heap +---------------+ | "don't blink" | +---------------+ ^ | | | +-------------+ | | main | ArrayBag object | | +----+ | +------------------+ | | b | --|-+-------->| +----+ | +--+---+------+------+---- | +----+ | | items | --|--+----->| | | null | null | ... +-------------+ | +----+ | +------+------+------+---- | +----+ | | numItems | 1 | | | +----+ | +------------------+ 5) ArrayBag b = new ArrayBag(); b.add("don't blink"); b.add("baggy"); 7) You get an error message that says something like: "Bad types in assignment, from Object to String." This makes sense because the return type of grab is Object, so the compiler thinks we are trying to assign something of type Object to a variable of type String, and Object is not a subclass of String. 9) see separate solution file