created 08/09/99; revised 11/04/2012, 07/04/2014, 07/23/2017


Chapter 22 Programming Exercises



Exercise 1 — Adding Integers

AddUpNumbers, the program that adds integers from the user could be improved. In some situations it would not be correct to say that the sum of integers is zero when the user, in fact, entered no integers. (For example, teachers sometimes distinguish between an assignment that got zero points and an assignment that was not turned in.) Modify the program so that it writes a message if the first value entered by the user is the sentinal value of zero. Otherwise, the program proceeds as before.

Enter first integer (enter 0 to quit): 0
No integers were entered.
bye

Enter first integer (enter 0 to quit): 3
Enter an integer (or 0 to quit): -3
Enter an integer (or 0 to quit): 0
Sum of the integers: 0
bye

Click here to go back to the main menu.



Exercise 2 — Miles per Gallon

Write a program that calculates miles per gallon for a list of cars. The data for each car consists of initial odometer reading, final odometer reading, and number of gallons of gas. The user signals that there are no more cars by entering a negative initial odometer reading.

Miles Per Gallon Program
Initial miles:
15000
Final miles:
15250
Gallons
10
Miles per Gallon: 25.0

Initial miles:
107000
Final miles:
107450
Gallons
15
Miles per Gallon: 30.0

Initial miles:
-1
bye

Click here to go back to the main menu.


Exercise 3 — In-range Adder

Write a program that asks the user for the low and high integer in a range of integers. The program then asks the user for integers to be added up. The program computes two sums:

The user signals the end of input with a 0.

In-range Adder
Low end of range:
20
High end of range:
50
Enter data:
21
Enter data:
60
Enter data:
49
Enter data:
30
Enter data:
91
Enter data:
0
Sum of in range values: 100
Sum of out of range values: 151

Click here to go back to the main menu.



Exercise 4 — Shipping Cost Calculator

A mail order company charges $3.00 for handling, free shipping for orders 10 pounds or less, plus $0.25 for each pound over 10. Write a program that repeatedly asks the user for the weight of an order, then writes out the shipping charge. The program stops when a weight of zero or less is entered.

Weight of Order:
5
Shipping Cost: $3.00

Weight of Order
20
Shipping Cost: $5.50

Weight of Order
0

bye

Click here to go back to the main menu.



Exercise 5 — Area of Rectangles

graph paper

A computer aided design program expects users to enter the coordinates two corners for each of several of rectangles (see diagram.) The sides of the rectangles are assumed to be parallel to the X and Y axes. The coordinates of each corner is entered as a pair of integers, first the X coordinate and then the Y coordinate. The origin of the coordinate system (0,0) is in the upper left, so Y increases going downward, and X increases to the right.

For each rectangle, the program calculates and writes out the height, the width, and the area of the rectangle. The two corners entered for each rectangle must be diagonally opposite (upper left and lower right, or upper right and lower left), but which choice is made for each rectangle is up to the user. The user can enter the corners in any order. Height and width are always positive (the program will have to adjust its calculations so that this is true.)

The program ends gracefully when the user enters corners which cannot be those of a rectangle (either the height is zero, the width is zero, or both.)

Computer Aided Design Program
First corner X coordinate:
100
First corner Y coordinate:
100
Second corner X coordinate:
250
Second corner Y coordinate
200
Width:  150  Height: 100   Area: 15000

First corner X coordinate:
250
First corner Y coordinate:
200
Second corner X coordinate:
100
Second corner Y coordinate
100
Width:  150  Height: 100   Area: 15000

First corner X coordinate:
100
First corner Y coordinate:
200
Second corner X coordinate:
250
Second corner Y coordinate
100
Width:  150  Height: 100   Area: 15000

First corner X coordinate:
100
First corner Y coordinate:
100
Second corner X coordinate:
100
Second corner Y coordinate
100
Width:  0  Height: 0   Area: 0

finished

Click here to go back to the main menu.


Exercise 6 — Login Simulator

Write a program that simulates the "login" process of a computer. A loop continuously asks the user for their user name and password. Assume that each is a single word. If the user name and password match one of the correct pairs, allow the user to log on (actually: just print a simulated message). A logged-on user has a priority from 1 (low) to 5 (high). Otherwise print a failure message. If the user name is "quit" with a password "exit", exit the loop and print a final message.

Use a if   else if structure nested inside a while loop to do this. Use the equals() method of String to compare user data with allowed user names and passwords. User names and passwords are case sensitive. "Hard code" the user names and passwords (that it, make them string literals that are part of each if statement. Here are some sample users and passwords:

Here is a sample run of the program:

User Name: gates
Password : money
Logon failed
User Name: gates
Password : monopoly
You have logged on with priority 1
User Name: hacker
Password : crack
Logon failed
User Name: jobs
Password : apple
You have logged on with priority 3
User Name: quit
Password : exit
System shutting down.
Bye.

The if   else if structure is really nothing new; it is a way of indenting your program to emphasize that a choice is being made out of a list of options. Here is a code excerpt that does one of four things, depending on whether choice is 0, 1, 2, or 3. The final else handles the situation where choice is out of range:

choice = scan.nextInt();

if ( choice==0 )
{
  // code for choice 0
}
else if ( choice==1 )
{
  // code for choice 1
}
else if ( choice==2 )
{
  // code for choice 2
}
else if ( choice==3 )
{
  // code for choice 3
}
else
{
  // error handling
}


Click here to go back to the main menu.


End of exercises.