I am supposed to implement the Collection interface using an Array, and create an inner class for the Iterator. I am not getting how to write the addAll(Collection c) function, and how to deal with the Iterator inner class and the function that uses it in the Bag class...ahhhhhhh, I know the parameter for addAll(Collection c) is gonna be a second Array that I make in my driver, but am I supposed to just go c.array[0] to access it I don't know here is the code.
import java.util.*;
//Collection Interface and all of its functions. interface Collection{
public boolean add(Object o); public boolean addAll(Collection c); public void clear(); /*public boolean contains(Object o); public boolean containsAll(Collection c); public boolean equals(Object o); public int hashCode(); public boolean isEmpty();*/ public Iterator iterator(); /*public boolean remove(Object o); public boolean removeAll(Collection c); public boolean retainAll(Collection c); public int size(); public Object[] toArray(); public Object[] toArray(Object[] a);*/ };
//Iterator interface and all of its functions. interface Iterator{ public boolean hasNext(); //public Object next(); public void remove(); };
/********************************************************* Class Name: Bag Functions: All of the functions in the Collection Interface Purpose: Implement the Collection Interface. *********************************************************/ class Bag implements Collection{
//Class variables. int size; int index; Object[] wallet;
/***************************************************** Function name: Bag(); Parameters: Collection c Declared variables: none Return value: none Purpose: Create a new array + Bag object. *****************************************************/ public Bag(){ size=10; index=0; wallet=new Object[size]; };
/***************************************************** Function name: add(); Parameters: Object Declared variables: none Return value: boolean Purpose: Add an Object to the wallet array. *****************************************************/ public boolean add(Object o){
/*public boolean contains(Object o){}; public boolean containsAll(Collection c){}; public boolean equals(Object o){}; public int hashCode(){}; public boolean isEmpty(){};*/ public Iterator iterator(){
}; /*public boolean remove(Object o){}; public boolean removeAll(Collection c){}; public boolean retainAll(Collection c){}; public int size(){}; public Object[] toArray(){};*/
//Inner class that is implementing the Iterator interface. class Iterd implements Iterator{
public boolean hasNext(){
return true; }; //public Object next(){}; public void remove(){}; };
> I am supposed to implement the Collection interface using > an Array, and create an inner class for the Iterator. I am > not getting how to write the addAll(Collection c) > function, and how to deal with the Iterator inner class > and the function that uses it in the Bag class...ahhhhhhh, > I know the parameter for addAll(Collection c) is gonna be > a second Array that I make in my driver, but am I supposed > to just go c.array[0] to access it I don't know here is > the code. > > /***************************************************** > Function name: add(); > Parameters: Object > Declared variables: none > Return value: boolean > Purpose: Add an Object to the wallet array. > *****************************************************/ > public boolean add(Object o){ > > if(o==null){return false;} > > else if(wallet[index]==null){ > wallet[index]=o; > } > ++index; > > return true; > > }; > public boolean addAll(Collection c){ > > return true; > };