“Hello, World” and friends.

exercise No. 1

Starting from and extending class Hello

Q:

  1. Download and unzip the Sd1.zip Maven Java project template. The resulting sub directory Sd1 contains a pom.xml file. Open this file by Idea and choose »Open as Project«.

  2. Within the resulting project open the path Sd1/src/main/java/de.hdm_stuttgart.mi/exercises.getStarted/ex01.

  3. Double click on the »Hello« class. You should see the following source code:

    void main() {
        System.out.println("Hello World!");
    }

    Don't yet bother about all the enclosing main() method. Just focus on the statement being highlighted in red. It's the only one we currently care about.

  4. Right click on the »Hello« class and select »Run 'Hello.main()'« from the context menu. Watch the resulting output.

  5. Modify the code to produce the following output:

    Those who can do.
    Those, who cannot, teach.
    Those, who cannot teach, lecture trainee teachers teaching methodology.

    Tip

    You may:

A:

There are several options for creating a whole paragraph spanning multiple text lines:

  1. Using Java 13 multiline string literals (best):

    void main() {
      IO.println("""
         Those who can do.
         Those, who cannot, teach.
         Those, who cannot teach, lecture trainee teachers teaching methodology.""" );
    }
  2. Straightforward: Using multiple println(...) statements:

    void main() {
       IO.println("Those who can do.");
       IO.println("Those, who cannot, teach.");
       IO.println("Those who cannot teach lecture trainee teachers teaching methodology.");
    }
  3. Long strings exceeding your editor's desired maximum line length may be decomposed into smaller chunks using the “+” ❶ string concatenation operator:

    void main() {
      IO.println(
        "Those who can do.\n"  + ❶
        "Those, who cannot, teach.\n"  +
        "Those, who cannot teach, " +
        "lecture trainee teachers teaching methodology."); // No \n at end, println(...) does it.
    }

    Note

    »\n« represents the newline character. Without it the three sentences would be concatenated into a single line of text.

    The IO.println(...) method adds a final line break to the content in question.