Skip to content
ExamSpine

Lesson 4.1 · Write it

Greeting with a border

A whole programPython or Java6 tests, 3 of them hidden

On a phone? Put the lines in order instead

Write a program that reads a name and prints a greeting framed by two identical border lines.

  1. Read one name on a single line.
  2. Print ---------- (ten hyphens) on the first line.
  3. Print Hello, followed by the name, then ! on the second line.
  4. Print ---------- (ten hyphens) on the third line.

Define a function that prints the border line, and call it for both borders.

The name is always one word. Your program is run several times. Only what it prints is checked.

Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.

Examples

Your program is run 6 times: the 3 examples below, and 3 hidden ones that check the rules above. Only what it prints is checked.

  • a name with a border above and below (rules 2, 3, 4)

    You type

    1. typed: Alice

    It prints

    ----------
    Hello, Alice!
    ----------
  • a different name (rule 3)

    You type

    1. typed: Bob

    It prints

    ----------
    Hello, Bob!
    ----------
  • a single-character name (rule 3)

    You type

    1. typed: X

    It prints

    ----------
    Hello, X!
    ----------
solution.pySolution.java