Skip to content
ExamSpine

Lesson 4.2 · Write it

Custom 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 border character, a width, and a message, then prints the message between two border lines.

  1. Read the border character (one character) on the first line.
  2. Read the width (whole number) on the second line.
  3. Read the message (text) on the third line.
  4. Print a line of the border character repeated exactly width times.
  5. Print the message.
  6. Print the same border line again.

Define a function that takes the character and width and prints one border line. Call it for both borders.

The width is a whole number from 1 to 50. 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.

  • 5 stars above and below Hello (rules 4, 5, 6)

    You type

    1. typed: *
    2. typed: 5
    3. typed: Hello

    It prints

    *****
    Hello
    *****
  • 8 hyphens as borders (rule 4)

    You type

    1. typed: -
    2. typed: 8
    3. typed: Test

    It prints

    --------
    Test
    --------
  • 3 hash characters (rule 4)

    You type

    1. typed: #
    2. typed: 3
    3. typed: Go

    It prints

    ###
    Go
    ###
solution.pySolution.java