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.
- Read the border character (one character) on the first line.
- Read the width (whole number) on the second line.
- Read the message (text) on the third line.
- Print a line of the border character repeated exactly
widthtimes. - Print the message.
- 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
- typed: *
- typed: 5
- typed: Hello
It prints
***** Hello *****
8 hyphens as borders (rule 4)
You type
- typed: -
- typed: 8
- typed: Test
It prints
-------- Test --------
3 hash characters (rule 4)
You type
- typed: #
- typed: 3
- typed: Go
It prints
### Go ###