Can somebody please help me understand what the output of this code would be. I believe I am getting the wrong output due to some technical problems with my computer.

public class SetDemo {

	public static void main(String[] args) {
		SetInherit inherit = new SetInherit();
	      SetComposition comp = new SetComposition();
	      System.out.println(inherit);
	      inherit.add(1);
	      System.out.println(inherit);
	      inherit.add(1);
	      System.out.println(inherit);
	      inherit.add(2);
	      System.out.println(inherit);
	      inherit.add(3);
	      System.out.println(inherit);
	      inherit.add(3);
	      System.out.println(inherit);
	      inherit.add(4);
	      
	      System.out.println(comp);
	      comp.add(1);
	      System.out.println(comp);
	      comp.add(1);
	      System.out.println(comp);
	      comp.add(2);
	      System.out.println(comp);
	      comp.add(3);
	      System.out.println(comp);
	      comp.add(3);
	      System.out.println(comp);
	      comp.add(4);

	}
}

Recommended Answers

All 2 Replies

This is SetComposition;

public class SetComposition implements ISet {
	private BoundedBag b;

	public boolean add(int theValue) {
		return b.add(theValue);
	}

	public SetComposition() {
		b = new BoundedBag();
	}

	public boolean contains(int theValue) {
		return b.contains(theValue);
	}

	public boolean remove(int theValue) {
		return b.remove(theValue);
	}

	public int size() {
		return b.size();
	}
}

This is SetInherit;

public class SetInherit extends BoundedBag implements ISet {
	public SetInherit(){
		super();
	}

This is ISet:

public interface ISet {
	boolean add(int theValue);	
	boolean contains(int theValue);	
	boolean remove(int theValue);	
	int size();	String toString();
}

and what is 'BoundedBag'? most times, trouble in output are not technical problems of your computer, it's just a difference between what a developer should've written, and what he actually wrote.

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.