Espresso: A Concentrated Introduction to Java

Laboratory: Loops

Summary: In today's laboratory, you will ground your understanding of loops in a few short examples.



Exercise 0: Preparation

For this lab (and, possibly, for every subsequent lab), you should continue to use the project Code.

a. Start Eclipse.

b. Create the package username.loops within the Code project. You should use that package for this laboratory.

Exercise 1: Counting

a. Write a main class, SimpleCounter, that prints the numbers from 1 to 10 using a for loop.

b. Write a main class, EvenCounter, that prints the even numbers from 0 to 20 using a for loop.

c. Write a main class, DownCounter, that prints the numbers from 10 to 1 using a for loop.

Exercise 2: Counting, Revisited

a. Write a main class, Counter, that prompts the user for three integers -- a starting value, an ending value, and an increment -- and then prints all integers starting with the starting value and ending just before or at the ending value. For example,

This program counts for you.
Please enter the starting value: 5
Please enter the ending value: 10
Please enter the increment: 1
5 6 7 8 9 10


This program counts for you.
Please enter the starting value: 5
Please enter the ending value: 12
Please enter the increment: 3
5 8 11

I would recommend that you use your username.util.IO to read integers.

You may assume that the increment is positive.

Exercise 3: Repetitive Prompting

One typical simple use of loops is to validate input from the user. For example, we might want to repeatedly ask for a password until the password is guessed, or we might want to ask for a color until the user enters a color (instead of something that we could not consder a color, like "Textbook").

a. Write a main class that repeatedly asks the user for a day of the week until the user enters something acceptable. You may choose the meaning of the word acceptable, but you should minimally accept Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. You might also accept abbreviations, such as mon or M.

b. Put the code for prompting into method called readDay in a utility class called Prompter. The readDay method will probably need at least two parameters: a BufferedReader to read input and a PrintWriter to print prompts. The readDay method should return a string, preferably one of the seven full names of week days.

c. Rewrite your class from step a to use readDay. For example,

String day = Prompter.readDay(eyes,pen);
pen.println("It appears that you have entered the day " + day);

Exercise 4: Getting Numeric Input

In exercise 2, you wrote a program that prompts the user for three numbers. Unfortunately, it is likely that your program will behave poorly (e.g., crash) if the user enters something other than a number (or even something that the user might think of as a number, but that computers do not, such as Two).

a. Determine what happens if the user enters erroneous input.

b. Suggest how you might solve this problem. (You should not write code; simply describe the code you might write.)


Tuesday, 22 February 2005 [Samuel A. Rebelsky]

Wednesday, 21 September 2005 [Samuel A. Rebelsky]

Sunday, 25 September 2005 [Samuel A. Rebelsky]

Tuesday, 14 February 2006 [Samuel A. Rebelsky]

This page was generated by Siteweaver on Thu Mar 30 15:24:39 2006.
The source to the page was last modified on Tue Feb 14 10:23:32 2006.
This page may be found at

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky