The Artima Developer Community
Sponsored Link

Java Answers Forum
need help with some simple code.

1 reply on 1 page. Most recent reply: Apr 9, 2003 6:38 AM by Adam Duffy

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

need help with some simple code. Posted: Apr 8, 2003 11:47 PM
Reply to this message Reply
Advertisement
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){

if(o==null){return false;}

else if(wallet[index]==null){
wallet[index]=o;
}
++index;

return true;

};
public boolean addAll(Collection c){

return true;
};
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(){};*/

//Inner class that is implementing the Iterator interface.
class Iterd implements Iterator{

public boolean hasNext(){

return true;
};
//public Object next(){};
public void remove(){};
};


};


any help would be great thanks.


Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: need help with some simple code. Posted: Apr 9, 2003 6:38 AM
Reply to this message Reply
> 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;
> };

The method addAll may be implemented as follows

public boolean addAll( Collection c )
{
    Iterator iterator = c.iterator();
    while( iterator.hasNext() )
    {
        add( iterator.next() );
    }
}


Adam

Flat View: This topic has 1 reply on 1 page
Topic: download image by java program? Previous Topic   Next Topic Topic: ArrayList Performance

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use