Java Reference
In-Depth Information
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 causes
death in laboratory mice. A friend of yours is desperate to lose weight but cannot
give up soda pop. Your friend wants to know how much diet soda pop it is pos-
sible to drink without dying as a result. Write a program to supply the answer. The
input to the program is the amount of artificial sweetener needed to kill a mouse,
the weight of the mouse, and the desired weight of the dieter. Assume that diet
soda contains 1/10th of 1% artificial sweetener. Use a named constant for this
fraction. You may want to express the percent as the double value 0.001 .
7. Write a program that determines the change to be dispensed from a vending
machine. An item in the machine can cost between 25 cents and 1 dollar, in 5-cent
increments (25, 30, 35, . . . 90, 95, or 100), and the machine accepts only a single
dollar bill to pay for the item. For example, a possible sample dialog might be the
following:
Enter price of item
(from 25 cents to a dollar, in 5-cent increments):
45
You bought an item for 45 cents and gave me a dollar,
so your change is
2 quarters,
0 dimes, and
1 nickels.
8. Write a program that reads in a line of text and then outputs that line of text first
in all uppercase letters and then in all lowercase letters.
9. (This is a better version of an exercise from Chapter 1.) Write a program that
reads in a line of text and then outputs that line of text with the first occurrence
of "hate" changed to "love" . For example, a possible sample dialog might be the
following:
Enter a line of text.
I hate you.
I have rephrased that line to read:
I love you.
You can assume that the word "hate" occurs in the input. If the word "hate"
occurs more than once in the line, your program should replace only the first
occurrence of "hate" .
10. Write a program that inputs the name, quantity, and price of three items. The
name may contain spaces. Output a bill with a tax rate of 6.25%. All prices should
be output to two decimal places. The bill should be formatted in columns with
30 characters for the name, 10 characters for the quantity, 10 characters for the
price, and 10 characters for the total. Sample input and output is shown as follows:
Input name of item 1:
lollipops
Search WWH ::




Custom Search