Java Reference
In-Depth Information
variable initially assigned to the number of coupons you win. Next, the program
should output how many candy bars and gumballs you can get if you spend all of
your coupons on candy bars first, and any remaining coupons on gumballs.
3. Write a program that starts with the string variable first set to your first name
and the string variable last set to your last name. Both names should be all lower-
case. Your program should then create a new string that contains your 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, given
first = "walt";
last = "savitch";
the program should create a new string with the text “Altway Avitchsay” and print it.
4. A government research lab has concluded that an artificial sweetener commonly
used in diet soda pop will cause 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 possible to drink without dying as a result. Write a
program to supply the answer. The program has no input but does have defined
constants for the following items: the amount of artificial sweetener needed to kill
a mouse, the weight of the mouse, the starting weight of the dieter, and the desired
weight of the dieter. To ensure the safety of your friend, be sure the program uses
the weight at which the dieter will stop dieting, rather than the dieter's current
weight, to calculate how much soda pop the dieter can safely drink. You may use
any reasonable values for these defined constants. Assume that diet soda contains
1/10th of 1% artificial sweetener. Use another named constant for this fraction.
You may want to express the percent as the double value 0.001. (If your program
turns out not to use a defined constant, you may remove that defined constant
from your program.)
5. Write a program that starts with 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 output might be
The line of text to be changed is:
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 will replace only the first occur-
rence of "hate" . Since we will not discuss input until Chapter 2 , use a defined
constant for the string to be changed. To make your program work for another
string, you should only need to change the definition of this defined constant.
Search WWH ::




Custom Search