The Artima Developer Community
Sponsored Link

Java Answers Forum
Cleaned code up, but still need help.

4 replies on 1 page. Most recent reply: Apr 11, 2003 8:50 AM by Matt Gerrans

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 4 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Cleaned code up, but still need help. Posted: Apr 10, 2003 3:56 PM
Reply to this message Reply
Advertisement
I have to implement the Collection interface and make an Iterator inner class. I am using an array for the Collection. I am getting an error with my addAll() function and I am having trouble understanding the use of the Iterator in this case. Also if someone could help me out with some of the functions that would be greatly appreciated. Thanks.

import java.util.*;
 
/*********************************************************
 Class Name: Bag
 Functions: All of the functions in the Collection Interface
 Purpose: Implement the Collection Interface.
 *********************************************************/
public 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 Collection.
	 *****************************************************/
	public boolean add(Object o)
	{	
		wallet[index]=o;
		++index;
		return true;
	};
	
	/*****************************************************
	 Function name: addAll();
	 Parameters: Collection
	 Declared variables: no variables, one Iterator.
	 Return value: boolean
	 Purpose: Adds all Objects from the specified Collection
	 	to this Collection.
	 *****************************************************/
	public boolean addAll(Collection c)
	{
		Iterator i= c.iterator();
		index=-1;
		while(i.hasNext())
		{
			this.add(i.next());
		};
		return true;
	};
	
	/*****************************************************
	 Function name: clear();
	 Parameters: none
	 Declared variables: 
	 Return value: void
	 Purpose: Clears all Objects from the Collection
	 *****************************************************/
	public void clear()
	{
		Iterator it= this.iterator();
		for(int i=0;it.hasNext()!=false; ++i){
			wallet[i]=null;
		}
		
	};
	
	//These to be implemented later.
	/*public boolean contains(Object o){};
	public boolean containsAll(Collection c){};
	public boolean equals(Object o){};*/
	
	/*****************************************************
	 Function name: iterator();
	 Parameters: none
	 Declared variables: 
	 Return value: Iterator
	 Purpose: Returns an Iterator over the elements in this
	 	Collection.
	 *****************************************************/
	public Iterator iterator()
	{
		return new Iterd();//Returns a new instance of the class.
	};
	
	public boolean remove(Object o)
	{
		return true;
	};
	
	//These to be implemented later.
	/*public boolean removeAll(Collection c){};
	public boolean retainAll(Collection c){};
	public int size(){};*/
	
	/*****************************************************
	 Function name: toArray();
	 Parameters: none
	 Declared variables: no variables, one Object[] declared
	 Return value: Object[]
	 Purpose: Returns an Array containing all elements 
	 	in the Collection.
	 *****************************************************/
	public Object[] toArray()
	{
		Object[] aa= new Object[size];
		return aa;
		
	}; 
	
	/*********************************************************
 	Class Name: Iterd
 	Functions: All of the functions in the Iterator Interface
 	Purpose: Implement the Iterator Interface.
 	*********************************************************/
	public class Iterd implements Iterator
	{
		/*****************************************************
	 	Function name: hasNext();
	 	Parameters: none
	 	Declared variables: none
	 	Return value: boolean
	 	Purpose: Returns true if there is more than one element 
	 		in the array.
	 	*****************************************************/
		public boolean hasNext()
		{
			if(next()!=null)
				return true;
			else 
				return false;
		};
		
		/*****************************************************
	 	Function name: next();
	 	Parameters: none
	 	Declared variables: increments the index. 
	 	Return value: Object
	 	Purpose: Returns the next Object in the array.
	 	*****************************************************/
		public Object next()
		{
			
			++index;
			return wallet[index];
			
		};
		public void remove(){};
	}; //end Iterd 
	
	
}; //end Bag 


jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: Cleaned code up, but still need help. Posted: Apr 10, 2003 4:16 PM
Reply to this message Reply
I spoke too soon, these functions are not that hard to write. I am still having an error with my addAll(). And it would be nice if someone could look over my code to make sure. thanks here goes.

import java.util.*;
 
/*********************************************************
 Class Name: Bag
 Functions: All of the functions in the Collection Interface
 Purpose: Implement the Collection Interface.
 *********************************************************/
public 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 Collection.
	 *****************************************************/
	public boolean add(Object o)
	{	
		wallet[index]=o;
		++index;
		return true;
	};
	
	/*****************************************************
	 Function name: addAll();
	 Parameters: Collection
	 Declared variables: no variables, one Iterator.
	 Return value: boolean
	 Purpose: Adds all Objects from the specified Collection
	 	to this Collection.
	 *****************************************************/
	public boolean addAll(Collection c)
	{
		Iterator i= c.iterator();
		index=-1;
		while(i.hasNext())
		{
			this.add(i.next());
		};
		return true;
	};
	
	/*****************************************************
	 Function name: clear();
	 Parameters: none
	 Declared variables: 
	 Return value: void
	 Purpose: Clears all Objects from the Collection
	 *****************************************************/
	public void clear()
	{
		Iterator it= this.iterator();
		for(int i=0;it.hasNext()!=false; ++i){
			wallet[i]=null;
		}
		
	};
	
	//These to be implemented later.
	/*public boolean contains(Object o){};
	public boolean containsAll(Collection c){};
	public boolean equals(Object o){};*/
	
	/*****************************************************
	 Function name: iterator();
	 Parameters: none
	 Declared variables: 
	 Return value: Iterator
	 Purpose: Returns an Iterator over the elements in this
	 	Collection.
	 *****************************************************/
	public Iterator iterator()
	{
		return new Iterd();//Returns a new instance of the class.
	};
	
	public boolean remove(Object o)
	{
		return true;
	};
	
	/*****************************************************
	 Function name: removeAll();
	 Parameters: Collection
	 Declared variables: 
	 Return value: boolean
	 Purpose: removes all Objects from the specified Collection.
	 *****************************************************/
	public boolean removeAll(Collection c)
	{
		Iterator it= c.iterator();
		for(int i=0;it.hasNext()!=false; ++i)
		{
			wallet[i]=null;
		}
		return true;
			
	};
	//public boolean retainAll(Collection c){};
	
	/*****************************************************
	 Function name: size();
	 Parameters: none
	 Declared variables: int s
	 Return value: int
	 Purpose: Iterates through every item in the Collection
	 	incrementing the running counter at every item, then
	 		returns the counter value.
	 *****************************************************/
	public int size()
	{
		int s;
		Iterator it= this.iterator();
		for(int i=0;it.hasNext()!=false; ++i)
		{
			++s;
		}
		return s;
		
	};
	
	/*****************************************************
	 Function name: toArray();
	 Parameters: none
	 Declared variables: no variables, one Object[] declared
	 Return value: Object[]
	 Purpose: Returns an Array containing all elements 
	 	in the Collection.
	 *****************************************************/
	public Object[] toArray()
	{
		Object[] aa= new Object[size];
		aa.addAll(this);
		return aa;
		
	}; 
	
	/*********************************************************
 	Class Name: Iterd
 	Functions: All of the functions in the Iterator Interface
 	Purpose: Implement the Iterator Interface.
 	*********************************************************/
	public class Iterd implements Iterator
	{
		/*****************************************************
	 	Function name: hasNext();
	 	Parameters: none
	 	Declared variables: none
	 	Return value: boolean
	 	Purpose: Returns true if there is more than one element 
	 		in the array.
	 	*****************************************************/
		public boolean hasNext()
		{
			if(next()!=null)
				return true;
			else 
				return false;
		};
		
		/*****************************************************
	 	Function name: next();
	 	Parameters: none
	 	Declared variables: increments the index. 
	 	Return value: Object
	 	Purpose: Returns the next Object in the array.
	 	*****************************************************/
		public Object next()
		{
			
			++index;
			return wallet[index];
			
		};
		public void remove(){};
	}; //end Iterd 
	
	
}; //end Bag 

Adam Duffy

Posts: 168
Nickname: adamduffy
Registered: Feb, 2003

Re: Cleaned code up, but still need help. Posted: Apr 11, 2003 2:32 AM
Reply to this message Reply
>
>
> 
 
-- snip --
 
> /*****************************************************
> Function name: add();
> Parameters: Object
> Declared variables: none
> Return value: boolean
> Purpose: Add an Object to the Collection.
> *****************************************************/
> public boolean add(Object o)
> {
> wallet[index]=o;
> ++index;
> return true;
> };
> 
> /*****************************************************
> Function name: addAll();
> Parameters: Collection
> Declared variables: no variables, one Iterator.
> Return value: boolean
> Purpose: Adds all Objects from the specified Collection
> to this Collection.
> *****************************************************/
> public boolean addAll(Collection c)
> {
> Iterator i= c.iterator();
> index=-1;
> while(i.hasNext())
> {
> this.add(i.next());
> };
> return true;
> };
> 
 
-- snip --
 
> 


From following the flow of the program, I spotted a potential source of your error.

If a call is made to addAll, then
1. the Iterator is obtained from the collection
2. the index is set to -1
3. a call to Iterator method hasNext is made
4. a call to add method is made

When call is made to add, then
5. the Object is placed into the wallet at the index which is -1

This should result in an ArrayOutOfBoundsException.

Perhaps you should change step 2 to set the index to 0?

Adam

Michael Wiedmer

Posts: 15
Nickname: micks
Registered: Aug, 2002

Re: Cleaned code up, but still need help. Posted: Apr 11, 2003 8:47 AM
Reply to this message Reply
Hi...

I am having a few difficulties following your train of thought. At first, I thought I could concentrate on the matter at hand, but to me the code is so obscure that I digressed more and more. Finally, I came up with so many questions, because of contradicitons that I had already reimplemented the whole bag. I now decided to drop my catalouge of points and instead just give you a basic guide.

1. Use an IDE that helps you code. Eclipse (www.eclipse.org) or Netbeans (www.netbeans.org) are only a couple and can be obtained freely.

2. What do you think is the correlation between size, index and size()?

3. Make sure the iterator and the bag don't share variables (except for maybe(!) the wallet).

4. Make sure Iterator.hasNext() doesn't have any side effects...

5. Don't assume, the client fully traverses the collection using the iterator. She might as well only go half way.

6. Consider multithreading, too.

7. DRY - cf. Andy Hunt: "The Pragmatic Programmer".


regards,

Michael

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Cleaned code up, but still need help. Posted: Apr 11, 2003 8:50 AM
Reply to this message Reply
The iterator object should have its own index (or better, "position") and not change that of the containing Collection class (imagine what happens when you have two iterators going at once).

The Collection should not even have an "index." Instead it should have a currentSize. The array bounds can be obtained from the Object array itself. You will find that doing it this way will facilitate completing several of the other Collection methods, as well as allowing you to add the ability for the Collection to grow.

Flat View: This topic has 4 replies on 1 page
Topic: need help...I'm new. Previous Topic   Next Topic Topic: Confused, small boolean problem

Sponsored Links



Google
  Web Artima.com   

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