public class MarbleBag
{
	private int marbles;
	
	public MarbleBag ( )
	{
		this(0);
	}
	
	public MarbleBag(int m)
	{
		marbles = m;
	}
	
	public MarbleBag(MarbleBag mb)
	{
		marbles = mb.marbles;
	}
	  
	public void move(MarbleBag other)
	{
		marbles += other.marbles;
		other.marbles = 0;
	}
	  
	public String toString ( )
	{
		return "This marble bag has " + marbles + " marbles.";
	}
}

public class MarbleMover
{
	public static void main(String [] args)
	{
		MarbleMover run = new MarbleMover();
		run.rearrange();
	}
	  
	public void rearrange ( )
	{
		MarbleBag emily = new MarbleBag(23);
		MarbleBag shuang = new MarbleBag(14);
		MarbleBag aarvind = new MarbleBag();
			
		System.out.println("\n\n");
		System.out.println(emily + "\n");
		System.out.println(shuang + "\n");
		System.out.println(aarvind + "\n");
		System.out.println("\n\n");
			
		aarvind = combine(emily,shuang);
			
		System.out.println(emily + "\n");
		System.out.println(shuang + "\n");
		System.out.println(aarvind + "\n");
		System.out.println("\n\n");
	}
	  
	public MarbleBag combine(MarbleBag one, MarbleBag two)
	{
		MarbleBag copyOne = new MarbleBag(one);
		MarbleBag copyTwo = new MarbleBag(two);
		copyOne.move(copyTwo);
		return copyOne;
	}
}

Back to APCS Main Page