Index
Code


Here is the code for a Greeter class.

This is a class that can give a greeting in various languages.

This code is ready to run -- just copy it into a file named Greeter.java.


public class Greeter {

	//-------------------------------------------

	/** Helper method that prints the object to the output console */
	public static void println(Object o) {
		System.out.println(o);
	}

	//-------------------------------------------

	/** Prints a hello type greeting in Spanish */
	public void sayHelloInSpanish() {
		println("Spanish: Hola");
	}

	/** Prints a hello type greeting in French */
	public void sayHelloInFrench() {
		println("French: Bonjour");
	}

	/** Prints a hello type greeting in French */
	public void sayHelloInIndonesian() {
		println("Indonesian: Selamat siang");
	}

	/** Prints a hello type greeting in French */
	public void sayHelloInSwahili() {
		println("Swahili: Shikamoo");
	}

	/** Prints a hello type greeting in French */
	public void sayHelloInChinese() {
		println("Chinese: Nin hao");
	}

	//-------------------------------------------

	/** Prints a good morning type greeting in Twi */
	public void sayGoodMorningInTwi() {
		println("Twi: Maakye");
	}

	/** Prints a good morning type greeting in Somali */
	public void sayGoodMorningInChinese() {
		println("Chinese: Zao an");
	}

}


Code Doc


Here is the code doc (i.e., code documentation) for the Greeter class.

It is much more concise -- does not include the code (method bodies).

For message sending, this code doc is all we need.

To learn more about the method headers shown below go here.

public void sayHelloInSpanish()
Prints a hello type greeting in Spanish
public void sayHelloInFrench()
Prints a hello type greeting in French
public void sayHelloInIndonesian()
Prints a hello type greeting in French
public void sayHelloInSwahili()
Prints a hello type greeting in French
public void sayHelloInChinese()
Prints a hello type greeting in French
public void sayGoodMorningInTwi()
Prints a good morning type greeting in Twi
public void sayGoodMorningInChinese()
Prints a good morning type greeting in Somali


Test (Example Message Sends)


Below we send some messages to a Greeter object.

This code is ready to run -- just copy it into a file named GreeterTests.java.

For example, the code doc tells us that the Greeter object type (class) understands the message sayHelloInIndonesian -- thus we send it as shown below.



public class GreeterTests {

	/** Test the hello greeting for a Greeter object */
	public void testHello() {
		println("\nStarting -- testHello()");
		Greeter greeter = new Greeter();
		greeter.sayHelloInIndonesian();
		greeter.sayHelloInSwahili();
	}

	/** Test the good morning greeting for a Greeter object */
	public void testGoodMorning() {
		println("\nStarting -- testGoodMorning()");
		Greeter greeter = new Greeter();
		//TODO -- add code here
	}

	//-------------------------------------------

	/** Helper method that prints the object to the output console */
	public static void println(Object o) {
		System.out.println(o);
	}

	//-------------------------------------------

	/** Entry point (i.e., the point where
	Java starts running this program) */
	public static void main(String[] args) {
		GreeterTests tests = new GreeterTests();
		tests.testHello();
		tests.testGoodMorning();
	}

}


Try-It


Try sending some messages here