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");
}
}
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();
}
}