created 07/30/99

Chapter 12 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
Input the radius: 3
The radius is: 3 The area is: 28.274333882308138

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

Click here to go back to the main menu.

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:
That is  3 dollars and 24 cents.

(Use integer arithmetic and avoid floating point arithmetic. Review the integer remainder operator % if you are unsure how to proceed.)

Click here to go back to the main menu.

Exercise 3 — Correct Change

When cashiers make change they 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:

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

Write a program that reads how much change is due (in cents) and writes out that amount as dollars, quarters, dimes, nickels, and pennies. Use integers for all variables and all arithmetic. If you are stuck, do an example problem with paper and pencil.

Click here to go back to the main menu.

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.

Click here to go back to the main menu.

Exercise 5 — Further substring Fun

Review Exercise 4 from the previous chapter. It describes the substring() method of class String

String newString = originalString.substring( int beginIndex, int endIndex )

This method creates a new String object based on the original object, but containing only the characters that start at beginIndex and end at endIndex-1. It creates a completely new object by selectively copying characters from the original string.

Write a program that asks the user for a string, a beginning index, and an ending index and then writes out the original string and the new substring:

Enter a string:
Eva, can I stab bats in a cave?
Enter beginning index: 5
Enter ending index: 20

Original string:
Eva, can I stab bats in a cave?

can I stab bats

Use Scanner.nextLine() to read in the user's string.