// Class Merge:
//     Merge two increasing sequences of integers

// Code does not include statements supporting trace display

// Include Stream, Times, Successor classes here

import java.awt.*;

class IntObject
{
   int number;
   
   public IntObject (int n) { number = n; }
   public int valueOf () { return number; }
}

// Notice: putIt() does not have to be at end of run()

class Merge extends Stream
{
   Object val1, val2;
   Stream stream1;
   Stream stream2;
   
   public Merge (Stream s1, Stream s2)  { stream1 = s1; stream2 = s2; }

   public void run ()
   {
      val1 = stream1.next();
      val2 = stream2.next();
      
      while (true)
      {
	 if (val1 == null && val2 == null)  putIt(null);
	 else
	 if (val1 == null)  {  putIt(val2);  val2 = stream2.next();  }
	 else
	 if (val2 == null)  {  putIt(val1);  val1 = stream1.next();  }
	 else
         if (((IntObject)(val1)).valueOf() < ((IntObject)(val2)).valueOf())
	 {  putIt(val1);  val1 = stream1.next();  }
	 else
	 {  putIt(val2);  val2 = stream2.next();  }
      }
   }
}

public class Mrg extends java.applet.Applet
{
   Stream merge, t1, t2;
   TextField out[];
   Object object;
   ...
   
   public void init ()
   {
      ...
      t1 = new Times(3, new Successor(1));
      t2 = new Times(5, new Successor(1));
      merge = new Merge(t1, t2);
      ...
   }
   ...
   public boolean action (Event evt, Object obj)
   {
      ...
      object = merge.next();
      int n = ((IntObject)(object)).valueOf();
      out.setText(out.getText()+" "+n);
      ...
      return super.action(evt, obj);
   }
}