created 08/04/99; revised 07/20/2017


Chapter 19 Programming Exercises

Exercise 1 — Run of Integers

Write a program that asks the user for a starting value and an ending value and then writes all the integers (inclusive) between those two values.

Enter Start:
5
Enter End:
9

5
6
7
8
9

Click here to go back to the main menu.


Exercise 2 — Repeatedly Echo a Word

Write a program that asks the user to enter a word. The program will then repeat word for as many times as it has characters:

Enter a word:
Hello

Hello
Hello
Hello
Hello
Hello

To do this you will need to use the length() method of Stringthat counts the number of characters in a string:

String inputString;
int times;

 . . . .

times = inputString.length()

Click here to go back to the main menu.


Exercise 3 — Words Separted by Dots

Write a program that asks the user to enter two words. The program then prints out both words on one line. The words will be separated by enought dots so that the total line length is 30:

Enter first word:
turtle
Enter second word
153

turtle....................153

This could be used as part of an index for a book. To print out the dots, use System.out.print(".") inside a loop body.

Click here to go back to the main menu.


Exercise 4 — One Letter per Line

Write a program that asks the user to enter a word. The program then writes that word one character per line:

Enter a word: turtle
t
u
r
t
l
e

Use the length() method that counts the number of characters in a string and the charAt( int index ) that returns the character at index. Recall that String indexes start at zero.

Click here to go back to the main menu.


End of exercises.