created 07/30/99

# Chapter 10 Programming Exercises

General Instructions:   Each of these programs calls for reading integer data from the keyboard. (Of course you will have to read in character data and convert it to an int using the wrapper class Integer.) Once you have read in an integer it is OK to do double precision arithmetic with it. Just be sure that your arithmetic expressions don't accidentally call for integer math when you don't want it.

## Exercise 1 — Area of a Circle

Write a program that calculates the area of a circle from its radius. The radius will be an integer read in from the keyboard. The user dialog will look like this:

```D:\users\default>java  CircleArea
The radius is: 3 The area is: 28.274333882308138
```

You will need to use the constant PI which you get by using `Math.PI`.

## Exercise 2 — Cents to Dollars

Write a program that reads in a number of cents. The program will write out the number of dollars and cents, like this:

```D:\users\default>java  Dollars
Input the cents:
324
That is  3 dollars and 24 cents.
```

(For this program you will use integer arithmetic and will need to avoid floating point arithmetic. Review the integer remainder operator % if you are unsure how to proceed.)

## Exercise 3 — Correct Change

When cashiers in a store give you change they try first try to "fit" dollars into the amount you get back, then try to "fit" quarters (25 cent coins) into what is left over, they try to "fit" dimes (10 cent coins) into what is now left over, then try to "fit" nickels (5 cent coins) into what is left, and finally are left with a few odd cents. For example, say that your change is 163 cents:

• One dollar fits into 163, leaving 63 cents.
• Two quarters fit into 63 cents, leaving 13 cents.
• One dime fits into 13 cents, leaving 3 cents.
• No nickels are needed.
• Three cents are left.

Your change is : 1 dollar, two quarters, one dime, and three cents.

Write a program that reads change due to a user (in cents) and writes out how many dollars, quarters, dimes, nickels, and pennies she is due. All variables and all math in this program will be integers. If you are stuck, it will help to do an example problem with paper and pencil.

## Exercise 4 — Ohm's Law

Ohm's law relates the resistance of a electrical device (like a heater) to the electric current flowing through the device and the voltage applied to it. The law is:

```I = V/R
```

Here, V is the voltage (measured in volts), I is the current (measured in amps), and R is the resistance (measured in ohms.) Write a program that asks the user for the voltage and the resistance of a device. The program will then write out the current flowing through it. Use floating point math.

Since V and R are integers (converted from user input) you must use a trick to do floating point division. Change the equation to this:

```I = (V + 0.0)/R
```

The math inside parentheses is done first. So V + 0.0 is done first, and since 0.0 is floating point, so will be the result.