So i have been assigned a program where i create a interface a bag and the main or tester i have been trying to get this done for a while now and so far two other people have checked my work and have no idea why my list prints the null something like this

:These is the output i get after running the code:
true
Tomatoes
Lettuce
Milk
Hello
ipad
Hello
windows
soap

ipad
Hello
windows
soap

ipad
Hello
windows
soap

ipad
Hello
windows
soap

null
null
null
null
null
............

This eventually stops and wont let the rest of the program to continue. :( I know that the problem is with the method union.

So here is my interface this should be working fine :)

public interface Bag {

	public void add(Object object);//adds an object 
	public boolean contains(Object object);//checks if contains certain object
	public Object getFirst();//gets first object
	public Object getNext();//gets next object
	public boolean remove(Object object);//removes an object
	public int size();//gets size

Here is my New_Bag that implements Bag interface

public class New_Bag implements Bag{
	private Object [] objects;
	private int nextIndex = 0;
	
	//public New_Bag(){}
	public New_Bag(int maxObject){
		objects = new Object[maxObject];
	}
	public New_Bag() {
		objects = new Object[100];
	}

	@Override
	public void add(Object object) {
		for(int index = 0; index < objects.length; index++){
			 if (objects[index] == null){
				 objects[index] = object;
	             break;
	         }
		}
	}
	@Override
	//checks if the bag contains a given object 
	public boolean contains(Object object) {
		for (int index = 0; index < objects.length; index++){
            // if the object is found return true, otherwise false.
        		if (objects[index]==object){
        			return true;
        			}
        	}
		return false;
	}

	@Override
	//gets the first object of the bag 
	public Object getFirst() {
		 for (int index = 0; index < objects.length; index++){
	        	// if it is not null, return the first item in the array
	            if (objects[index] != null)
	            {    	
	            	nextIndex = index + 1;
	                return objects[index];
	            }
	        }
	        return null;
	}

	@Override
	public Object getNext() {
		for(int index = nextIndex; index<objects.length; index++){
			Object item = objects[index];
			if(item != null && item!= (getFirst())){
				// save next index so we know where to start next time
				nextIndex = index+1;
				return item;
			}
		}
		return null;
	}

	@Override
	public boolean remove(Object object) {
		for (int index = 0; index < objects.length; index++){
            // if the item is found, delete the items, and return true
            if (objects[index] == object){//if given index is same to the argument of object 
                // this remove the item from the list
            	objects[index] = null;
                return true;//returns true
            }
        }
		return false;
	}

	@Override
	public int size() {
		int number = 0;//sets the number to 0
        for (int index = 0; index < objects.length; index++){//runs trough the whole array
            if (objects[index] != null){//if array index is not null
                number++;//number adds 1
            }
        }
        return number;//returns the total of objects in array
	}
	@Override
	public String toString(){
		for(int index = 0; index < objects.length; index ++){
			System.out.println(objects[index] + " ");
		}
		return "";
	}
	//creates a new bag with only the objects that are in both of them 
	public New_Bag difference(New_Bag bag2) {
		New_Bag baggie1 = this;//this bag for this array
		New_Bag baggie2 = bag2;//this is setting the argument   
		New_Bag baggie3 = new New_Bag();//new bag
		//iterates trough baggie1
		for(Object objects = baggie1.getFirst(); objects != null; objects = baggie1.getNext()){
			boolean isFound = false;
			//iterates trough baggie2
			for(Object objects1 = baggie2.getFirst(); objects !=null; objects1 = baggie2.getFirst()){
				//if baggie1 and baggie2 have same then is found is true
				if(objects.equals(objects1)){
					isFound = true;
					break;
				}
				//else if not found add to baggie3 
				if(!isFound){
					baggie3.add(objects);		
				}
			}
		}
		return baggie3;
	}
	
	//this method adds two bags into a single one
	public New_Bag union(New_Bag bag2){
		New_Bag baggie1 = this;//defines baggie1 to the bag of this class 
		New_Bag baggie2 = bag2;//baggie2 defines to the bag of the input from another bag
		New_Bag baggie3 = new New_Bag();//creates a new Bag
		//iterates trough the objects and sets baggie1 to the first and if not null adds next
		for(Object objects = baggie1.getFirst(); objects !=null; objects = baggie1.getNext()){
			baggie3.add(objects);//adds objects from bag1 to baggie 		
		}
		//iterates trough the objects and sets baggie2 to the first and if not null adds next
		for(Object objects = baggie2.getFirst(); objects !=null; objects = baggie2.getNext()){
			baggie3.add(baggie2);//adds objects from bag2 to baggie3
		}
		return baggie3;//returns baggie3 	
	}
	
	//this method takes two arrays of bag and takes only those objects that are the same and prints them out in a new 
	//array called baggie3
	/**public New_Bag intersection(New_Bag bag1){
		New_Bag baggie1 = this;//sets baggie1 to this 
		New_Bag baggie2 = bag1;//sets baggie2 to the argument 
		New_Bag baggie3 = new New_Bag();//creates new bag
		//for loop inside a for loop to get both the arrays and check which one to put into the third array
		for(Object objects = baggie1.getFirst(); objects !=null; objects = baggie1.getNext()){
			for(Object objects1 = baggie2.getFirst(); objects1 != null; objects1 = baggie2.getFirst()){
				if(objects.equals(objects1)){//if objects form baggie1 = objects1 then add to baggie3
					baggie3.add(objects);
					break;
				}//end of if 		
			}//end of for loop 2
		}//end of for loop 1
		return baggie3;
	}**/

}

And here is my tester

public class BagTest {
	public static void main (String[]args){
		
		New_Bag bag = new New_Bag(4);
		New_Bag bag2 = new New_Bag(4);
		//bag 1 
		bag.add("Tomatoes");
		bag.add("Lettuce");
		bag.add("Milk");
		bag.add("Hello");
		//bag 2
		bag2.add("ipad");	
		bag2.add("Hello");
		bag2.add("windows");
		bag2.add("soap");
		System.out.println(bag2.contains("Hello"));
		New_Bag bagForU = new New_Bag();
		bagForU = bag.union(bag2);
		System.out.println(bagForU);
		
		//New_Bag bagForD = new New_Bag();
		//bagForD = bag.difference(bag2);
		//System.out.println(bagForD);
		//New_Bag bagForI = new New_Bag();
		//bagForI = bag.intersection(bag2);
		//System.out.println("inters should print below");
		//System.out.println(bagForI);
		
		
	}
}

and this is the method i think i am having problems with :(
Remember that this method is in the New_Bag class

public New_Bag union(New_Bag bag2){
		New_Bag baggie1 = this;//defines baggie1 to the bag of this class 
		New_Bag baggie2 = bag2;//baggie2 defines to the bag of the input from another bag
		New_Bag baggie3 = new New_Bag();//creates a new Bag
		//iterates trough the objects and sets baggie1 to the first and if not null adds next
		for(Object objects = baggie1.getFirst(); objects !=null; objects = baggie1.getNext()){
			baggie3.add(objects);//adds objects from bag1 to baggie 		
		}
		//iterates trough the objects and sets baggie2 to the first and if not null adds next
		for(Object objects = baggie2.getFirst(); objects !=null; objects = baggie2.getNext()){
			baggie3.add(baggie2);//adds objects from bag2 to baggie3
		}
		return baggie3;//returns baggie3 	
	}

if anyone could please help me i would appreciate the help :)

well i have solve from this
true
Tomatoes
Lettuce
Milk
Hello
ipad
Hello
windows
soap

ipad
Hello
windows
soap

ipad
Hello
windows
soap

ipad
Hello
windows
soap

null
null
null
null
null
............

to this

true
Tomatoes
Lettuce
Milk
Hello
ipad
Hello
windows
soap


null
null
null
null
null
............

so i have discovered why the bag 2 was printing twice :)

it was the smallest thing in the union method

for(Object objects1 = baggie2.getFirst(); objects !=null; objects1 = baggie2.getFirst()){
				//if baggie1 and baggie2 have same then is found is true
				if(objects.equals(objects1)){
					isFound = true;
					break;

the problem was the if statmemnt it had

if(objects.equals(baggie2)

and i needed

if(objects.equals(objects1)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.