Java Reference
In-Depth Information
Programming Projects
Many of these Programming Projects can be solved using AW's CodeMate.
To access these please go to:
www.aw-bc.com/codemate
.
1.
The Babylonian algorithm to compute the square root of a number
n
is as follows:
1. Make a
guess
at the answer (you can pick
n
/2 as your initial guess).
2. Compute
r = n / guess
3. Set
guess
= (
guess +r
)
/ 2
4. Go back to step 2 for as many iterations as necessary. The more you repeat
steps 2 and 3, the closer
guess
will become to the square root of
n
.
Write a program that inputs an integer for
, iterates through the Babylonian
algorithm five times, and outputs the answer as a
n
to two decimal places.
double
Your answer will be most accurate for small values of
n
.
2.
(This is a version with input of an exercise from Chapter 1.) Write a program
that inputs two string variables,
, which the user should enter
with their name. First, convert both strings to all lowercase. Your program
should then create a new string that contains the full name in pig latin with the
first letter capitalized for the first and last name. Use only the pig latin rule of
moving the first letter to the end of the word and adding “ay”. Output the pig
latin name to the screen. Use the
and
first
last
and
methods to con-
substring
toUpperCase
struct the new name.
For example, if the user inputs “Walt” for the first name and “Savitch” for the last
name, then the program should create a new string with the text “Altway
Avitchsay” and print it.
3.
Write a program that reads in two integers typed on the keyboard and outputs
their sum, difference, and product.
4.
An automobile is used for commuting purposes. Write a program that takes as
input the distance of the commute in miles, the automobile's fuel consumption
rate in miles per gallon, and the price of a gallon of gas. The program should
then output the cost of the commute.
5.
The straight-line method for computing the yearly depreciation in value
D
for
an item is given by the formula
PS
-
Y
=
−−−−−−−
D
where
is the number of years
the item is used. Write a program that takes as input the purchase price of an
item, the expected number of years of service, and the expected salvage value.
The program should then output the yearly depreciation for the item.
P
is the purchase price,
S
is the salvage value, and
Y
6.
(This is a better version of an exercise from Chapter 1.) A government research
lab has concluded that an artificial sweetener commonly used in diet soda pop
Search WWH ::




Custom Search