import javautil public interface BagADT void addObject item Object remove

Import javautil public interface bagadt void

This preview shows page 2 - 6 out of 9 pages.

A Bag is a general unordered container of items where duplicates are allowed. import java.util.*; public interface BagADT { void add(Object item); Object remove() throws NoSuchElementException; boolean isEmpty(); } Why are we using the Objectclass in our BagADTinterface? 2. Bag ADT Implementation Steps public class ArrayBag implements BagADT { private Object[] items; private int numItems; public ArrayBag() { items = new Object[100]; numItems = 0; } void add(Object item) { ... } Object remove() throws NoSuchElementException { ... } boolean isEmpty(){ return numItems == 0; } }
Background image
Copyright 2014-2016 Jim SkrentnyCS 367 (S16): L2 - 3 Use - BagADTand Casting Using a general ADT and its implementation to instantiate a container: Write a statement thatmakes a Bag ADT container named bag.Assume Dieis a class representing dice and has a zero parameter constructor.Write a code fragmentthat adds 6 dice to bag. Assume the bag has had items added to it.Why doesn’t the following code compile? while (!bag.isEmpty()) { Die myDie = bag.remove(); myDie.roll(); }
Background image
Copyright 2014-2016 Jim SkrentnyCS 367 (S16): L2 - 4 Java Generics - A Better Way to Make a General Bag ADT What changes are needed to make the interface below generic? import java.util.*; public interface BagADT { void add(Object item); Object remove() throws NoSuchElementException; boolean isEmpty(); } 1. 2.
Background image
Copyright 2014-2016 Jim SkrentnyCS 367 (S16): L2 - 5 Implementation - Generic BagADTWhat changes are needed to make the implementation below generic? public class ArrayBag implements BagADT { private Object[] items; private int numItems; public ArrayBag() { items = new Object[100]; numItems = 0; } boolean isEmpty(){ return numItems == 0; } void add(Object item) { ... } Object remove() throws NoSuchElementException { ... } } 1.
Background image
Image of page 6

You've reached the end of your free preview.

Want to read all 9 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture