created 07/29/99

# Chapter 17 Programming Exercises

## Exercise 1 — Internet Delicatessen

You have been asked to write a program for the Sam and Ella Delicatessen. The program takes deli orders from the Internet. It asks for the item, its price in cents, and if express delivery is wanted. The program writes out the order and the charges. Regular delivery for items under \$10 is \$2.00; for items \$10 or more delivery is free. For express delivery add \$3.00.

```Enter the item: Tuna Salad
Enter the price: 4.50
Overnight delivery (0==no, 1==yes): 1

Invoice:
delivery      5.00
total         9.50
```

## Exercise 2 — Steam Engine Efficiency

The maximum possible efficiency of a steam engine depends on the temperature of the steam in the boiler and the temperature of the outside air:

```efficiency = 1 - Tair / Tsteam
```

where `Tair ` is the air temperature and `Tsteam ` is the steam temperature. The temperatures are give in degrees above absolute zero. Normal air temperature is about 300oK. Boiling is 373oK. Write a program that asks the user for the air temperature and the steam temperature and writes out the maximum possible efficiency of a steam engine. However, if the steam temperature is less than 373oK there is no steam, so the efficiency is zero.

Use integer or floating point input, but do the calculations in floating point.

## Exercise 3 — Microwave Oven Heating Time

A microwave oven manufacturer recommends that when heating two items, add 50% to the heating time, and when heating three items double the heating time. Heating more than three items at once is not recommended.

Write a program that asks the user for the number of items and the single-item heating time. The program then writes out the recommended heating time.

The user enters an integer for the heating time. Any value under 100 is the number of seconds to heat. A value 100 or higher shows seconds as the last two digits and minutes and the higher digits. So 100 means 1:00 (one minute zero seconds), 134 means 1:34 (one minutes 34 seconds), 1238 means 12:38 (twelve minutes 38 seconds), and so on.

Allow the user to enter values like 389 (three minutes 89 seconds).

First translate the value that the user entered into seconds. Then use four successive single-branch `if` statements each of which tests for one of the four cases: 1 item, 2 items, 3 items, more than three items. Look at the sports car purchase program in the chapter to see how to do this, if you are stuck.

After determining the seconds of heating time, translate into minutes and seconds and print out that recommended time. Integer division and remainder (`/` and `%`) will be useful here.

```C:\Source> java Microwave
How many items to heat: 1
Time for one item: 34
Heat for 0 minutes 34 seconds
C:\Source> java Microwave
How many items to heat: 2
Time for one item: 34
Heat for 0 minutes 51 seconds
C:\Source> java Microwave
How many items to heat: 3
Time for one item: 120
Heat for 2 minutes 40 seconds
```

## Exercise 4 — Fantasy Game

In a new role-playing fantasy game players must design their character by picking a point value for each of three characteristics:

• Strength, from 1 to 10
• Health, from 1 to 10
• Luck, from 1 to 10

Write a program that asks for a name for the character and asks for the point value of for each of the three characteristics. However, the total points must be less than 15. If the total exceeds 15, then 5 points are assigned to each characteristic

```Welcome to Yertle's Quest
Enter the name of your character:
Chortle
Enter strength (1-10):
8
Enter health (1-10):
4
Enter luck (1-10):
6

You have give your character too many points!  Default values have been assigned:
Chortle, strength: 5, health: 5, luck: 5
```

(This user interface could get much more complicated. You might want to implement some of your own ideas.)