CS211 Bootcamp Solutions
Step 3
Prepared by:  Alvin Law (ajl56@cornell.edu)

3.3
public class MyProgram
{
	public static void main(String[] args)
	{
		System.out.println(new Person(args[0],args[1]));
	}
}

class Person
{
	private String first;
	private String last;

	public Person(String f, String l)
	{
		first = f;
		last = l;
	}

	public String toString()
	{
		return first + " " + last;
	}
}

Running this program via command line with:

	java MyProgram "Dimmu" "Borgir"

Will output:

	Dimmu Borgir

as the result.  Note how the program was run through the main class (MyProgram) where the main function is located.  I place the two string arguements in quotes.  This is unnecessary, however it helps me remember that they are indeed strings.

A few other tidbits:  If this were one java file, class Person cannot be made public.  Only one public class can be contained per java document, and the filename of the java document must be named appropriately.  Example, a class Blah must be placed in Blah.java (case sensitive).  In most cases, it will not be necessary for one to put more than one class in a file, so this doesn't become an issue, but nonetheless it is possible.

Also note the toString function in class Person.  Each System.out.println call attempts to call this function from each class, so the Person class can be appropriately converted to a String through this method.  This can be particularly useful for debugging purposes, as it enables us to output information about classes without having to messily sift through the entire object.