created 04/29/00

Programming Exercises

Exercise 1 — User-Friendly Division Practice

Enhance the DivisionPractice program from the chapter:

  1. Put in a loop so that the user is repeatedly asked for the numerator and the divisor. For each set of data, the program prints out the result, or an informative error message if there is a problem (division by zero or poor input data).
  2. The program contines looping, even if there is a problem
  3. Exit the loop when data entered for the numerator start with characters "q" or "Q". Don't print out an error message in this case.
  4. Don't ask for the divisor if the user just asked to quit.

Here is sample output from one run:

Enter the numerator: 12
Enter the divisor:  4
12 / 4 is 3

Enter the numerator: 12
Enter the divisor  : 0
You can't divide 12 by 0

Enter the numerator: glarch
You entered bad data.
Please try again.

Enter the numerator: quit

You will need to use the method charAt() from the String class.

Click here to go back to the main menu.