Java Reference
In-Depth Information
6.29 (Coin Tossing) Write an application that simulates coin tossing. Let the program toss a coin
each time the user chooses the “ Toss Coin ” menu option. Count the number of times each side of
the coin appears. Display the results. The program should call a separate method flip that takes no
arguments and returns a value from a Coin enum ( HEADS and TAILS ). [ Note: If the program realistically
simulates coin tossing, each side of the coin should appear approximately half the time.]
6.30 (Guess the Number) Write an application that plays “guess the number” as follows: Your
program chooses the number to be guessed by selecting a random integer in the range 1 to 1000.
The application displays the prompt Guess a number between 1 and 1000 . The player inputs a first
guess. If the player's guess is incorrect, your program should display Too high. Try again. or Too
low. Try again. to help the player “zero in” on the correct answer. The program should prompt the
user for the next guess. When the user enters the correct answer, display Congratulations. You
guessed the number! , and allow the user to choose whether to play again. [ Note: The guessing tech-
nique employed in this problem is similar to a binary search, which is discussed in Chapter 19,
Searching, Sorting and Big O.]
6.31 (Guess the Number Modification) Modify the program of Exercise 6.30 to count the num-
ber of guesses the player makes. If the number is 10 or fewer, display Either you know the secret
or you got lucky! If the player guesses the number in 10 tries, display Aha! You know the secret!
If the player makes more than 10 guesses, display You should be able to do better! Why should it
take no more than 10 guesses? Well, with each “good guess,” the player should be able to eliminate
half of the numbers, then half of the remaining numbers, and so on.
6.32 (Distance Between Points) Write method distance to calculate the distance between two
points ( x1 , y1 ) and ( x2 , y2 ). All numbers and return values should be of type double . Incorporate
this method into an application that enables the user to enter the coordinates of the points.
6.33 (Craps Game Modification) Modify the craps program of Fig. 6.8 to allow wagering. Ini-
tialize variable bankBalance to 1000 dollars. Prompt the player to enter a wager . Check that wager
is less than or equal to bankBalance , and if it's not, have the user reenter wager until a valid wager
is entered. Then, run one game of craps. If the player wins, increase bankBalance by wager and dis-
play the new bankBalance . If the player loses, decrease bankBalance by wager , display the new bank-
Balance , check whether bankBalance has become zero and, if so, display the message "Sorry. You
busted!" As the game progresses, display various messages to create some “chatter,” such as "Oh,
you're going for broke, huh?" or "Aw c'mon, take a chance!" or "You're up big. Now's the time
to cash in your chips!" . Implement the “chatter” as a separate method that randomly chooses the
string to display.
6.34 (Table of Binary, Octal and Hexadecimal Numbers) Write an application that displays a
table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1
through 256. If you aren't familiar with these number systems, read online Appendix J first.
Making a Difference
As computer costs decline, it becomes feasible for every student, regardless of economic circum-
stance, to have a computer and use it in school. This creates exciting possibilities for improving the
educational experience of all students worldwide, as suggested by the next five exercises. [ Note:
Check out initiatives such as the One Laptop Per Child Project ( www.laptop.org ). Also, research
“green” laptops—what are some key “going green” characteristics of these devices? Look into the
Electronic Product Environmental Assessment Tool ( www.epeat.net ) , which can help you assess
the “greenness” of desktops, notebooks and monitors to help you decide which products to pur-
chase.]
6.35 (Computer-Assisted Instruction) The use of computers in education is referred to as com-
puter-assisted instruction ( CAI ). Write a program that will help an elementary school student learn
 
Search WWH ::




Custom Search