Pages

Thursday, September 22, 2016

A Close Encounter of the Java Kind

// DEFINE A CONCEPTUAL PERSON
abstract class Person {

    // FIELDS

    String myName;
    String myLanguage;

    // CONSTRUCTOR

    Person(String name, String language) {
        this.myName = name;
        this.myLanguage = language;
    }

    // METHODS

    abstract String helloInMyLanguage();

    String helloInEnglish() {
        return "Hello!";
    }

    boolean speaks(String language) {
        if (language == "English") {
            return true;
        }
        else if (language == this.myLanguage) {
            return true;
        }
        else {
            return false;
        }
    }

    void sayHello(Person otherPerson) {
        String hello;
        if (otherPerson.speaks(this.myLanguage)) {
            hello = this.helloInMyLanguage();
        }
        else {
            hello = this.helloInEnglish();
        }
        System.out.println(this.myName + ": " + hello);
    }
}

// DEFINE A CONCEPTUAL PERSON FROM CHINA
class PersonFromChina extends Person {

    // CONSTRUCTOR

    PersonFromChina(String name) {
        super(name, "Chinese");
    }

    // METHODS

    // Implements abstract method Person.helloInMyLanguage
    String helloInMyLanguage() {
        return "Ni Hao";
    }

    // Overrides Person.helloInEnglish
    String helloInEnglish() {
        return "Hi";
    }
}

// DEFINE A CONCEPTUAL PERSON FROM THE UNITED STATES
abstract class PersonFromUnitedStates extends Person {

    // CONSTRUCTOR

    PersonFromUnitedStates(String name) {
        super(name, "English");
    }

    // METHODS

    // Implements abstract method Person.helloInMyLanguage
    String helloInMyLanguage() {
        return helloInEnglish();
    }
}

// DEFINE A CONCEPTUAL AMERICAN COWBOY
class AmericanCowboy extends PersonFromUnitedStates {

    // CONSTRUCTOR

    AmericanCowboy(String name) {
        super(name);
    }

    // METHODS

    // Overrides PersonFromUnitedStates.helloInEnglish
    String helloInEnglish() {
        return "Howdy!";
    }
}

// DEFINE A CONCEPTUAL AMERICAN STUDENT
class AmericanStudent extends PersonFromUnitedStates {

    // CONSTRUCTOR

    AmericanStudent(String name) {
        super(name);
    }

    // METHODS

    // Overrides PersonFromUnitedStates.helloInEnglish
    String helloInEnglish() {
        return "What's up?";
    }
}

// CREATE A COLLECTION OF ACTUAL PEOPLE
class People {

    // FIELDS

    static AmericanCowboy cowboy = new AmericanCowboy("John Wayne");
    static AmericanStudent student1 = new AmericanStudent("David Spurgeon");
    static AmericanStudent student2 = new AmericanStudent("Lara Spurgeon");
    static PersonFromChina student3 = new PersonFromChina("Wang Xuesheng");
    static PersonFromChina student4 = new PersonFromChina("Sun Xuesheng");
}

// DEFINE A CONCEPTUAL ENCOUNTER AND MAKE ONE HAPPEN
class Encounter {

    // METHODS

    static void begin(Person person1, Person person2) {
        person1.sayHello(person2);
        person2.sayHello(person1);
    }

    static void close() {
        System.out.print("THE END");
    }

    public static void main(String[] args) {
        begin(People.student2, People.student3); // BEGIN AN ACTUAL ENCOUNTER...
        close(); // ...THEN CLOSE THE ENCOUNTER.
    }
}