Java Reference
In-Depth Information
Programming Projects
Visit www.myprogramminglab.com to complete select exercises online and get instant
feedback.
1. The Babylonian algorithm to compute the square root of a positive 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 a double for n , iterates through the Babylonian algo-
rithm five times, and outputs the answer as a double to two decimal places. 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, first and last , which the user should enter with his or
her name. First, convert both strings to all lowercase. Your program should then cre-
ate 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
substring and toUpperCase methods to construct 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 following formula:
D = P - S
Y
where P is the purchase price, S is the salvage value, and Y 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 pro-
gram should then output the yearly depreciation for the item.
 
Search WWH ::




Custom Search