/*******************************************************************************
 *  Title:  Constructor Test
 *  Files:  ConstructorTest.java
 *
 *  Author:  Brian Lee Bowers     blbowers@cs.wisc.edu
 *  Collaborators:  NONE
 *
 *  Due Date:  NONE
 *  Completion Date:  05 April 2001
 *
 *  Course:  CS 302, Spring 2001, Section 16
 *  TA:  Brian Lee Bowers
 *
 *  Compiler:  javac
 *  Platform:  SunOS 5.7 (Intel x86)
 ******************************************************************************/

/*****  Package Statement  *****/
/*****        NONE         *****/

/*****  Import Statements  *****/
import javabook2.*;

/**
 *  Demonstrate use of "this" in constructor calls
 */
public class ConstructorTest {
    /*  Name of the object  */
    private String myName = null;
    /*  Age of the object  */
    private int myAge = -1;
    /*  Array to be held  */
    private int[] myArray;

    /**
     *  Program entry point.  Create an object to show chain of calls.
     *
     *  @param args command line arguments
     */
    public static void main(String[] args) {
	//  create a ConstructorTest instance
	ConstructorTest constructorTest;
	constructorTest = new ConstructorTest();

	//  create an OutputBox instance
	MainWindow mainWindow = new MainWindow();
	OutputBox outputBox = new OutputBox(mainWindow);

	//  show the OutputBox instance
	mainWindow.setVisible(true);
	outputBox.setVisible(true);

	//  display the ConstructorTest instance as text (explicit)
	outputBox.printLine(constructorTest.toString());
	outputBox.waitUntilClose();
	System.exit(0);

    }

    /**
     *  Constructor.  Take no arguments to show use
     *  of "this" to call another constructor
     */
    public ConstructorTest() {
	this("No Name");
	System.out.println("Leaving ConstructorTest()");
    }

    /**
     *  Constructor.  Chain from one argument constructor
     *  to two argument constructor.
     *
     *  @param aName the name of the object
     */
    public ConstructorTest(String aName) {
	this(aName, 0);
	System.out.println("Leaving ConstructorTest(String aName)");
    }

    /**
     *  Constructor.  Chain from two argument constructor
     *  to three argument constructor.
     *
     *  @param aName the name of the object
     *  @param anAge the age of the object
     */
    public ConstructorTest(String aName, int anAge) {
	this(aName, anAge, 1);
	System.out.println("Leaving ConstructorTest(String aName, int anAge)");
    }

    /**
     *  Constructor.  Build an object given a name, age, and array size.
     *
     *  @param aName the name of the object
     *  @param anAge the age of the object
     *  @param aSize the size of the array
     */
    public ConstructorTest(String aName, int anAge, int aSize) {
	this.myName = aName;
	this.myAge = anAge;
	this.myArray = new int[aSize];
	System.out.println("Leaving ConstructorTest(String aName, int anAge,"
			   + " int aSize)");
    }

    /**
     *  Convert object to a text representation.
     *
     *  @return the text form of the object
     */
    public String toString() {
	StringBuffer sb = new StringBuffer();
	sb.append("\nName:  ");
	sb.append(myName);
	sb.append("\nAge:  ");
	sb.append(myAge);
	sb.append("\nArray Size:  ");
	sb.append(myArray.length);

	return sb.toString();
    }
}

