310 Methods Chapter 6 responses typed by the (Web host server)

310 Methods Chapter 6 responses typed by the student. After the student types 10 answers, your program should calculate the percentage of correct responses. If the percentage is lower than 75%, print Pleaseaskyourinstructorforextrahelp and reset the program so another student can try the program. 6.34 Write an applet that plays the guess the number game as follows: Your program chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The applet displays the prompt Guessanumberbetween1and1000 next to a JTextField. The player types a first guess into the JTextField and presses the Enter key. If the player’s guess is incorrect, your program should display Toohigh.Tryagain.or Toolow.Tryagain. in the status bar to help the player zero in on the correct answer and should clear the JTextField so the user can enter the next guess. When the user enters the correct answer, display Congratulations.You guessed the number! in the status bar and clear the JTextField so the user can play again. [Note: The guessing technique employed in this problem is similar to a binary search.] 6.35 Modify the program of Exercise 6.34 to count the number of guesses the player makes. If the number is 10 or fewer, print Eitheryouknowthesecretoryougotlucky! If the player guesses the number in 10 tries, print Aha!Youknowthesecret! If the player makes more than 10 guesses, print 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. Now show why any number from 1 to 1000 can be guessed in 10 or fewer tries. 6.36 Write a recursive method power(base,exponent) that when invoked returns base exponent For example, power(3,4)=3*3*3*3. Assume that exponent is an integer greater than or equal to 1. (Hint: The recursion step should use the relationship base exponent = base base exponent - 1 and the terminating condition occurs when exponent is equal to 1, because base1 = base Incorporate this method into an applet that enables the user to enter the base and exponent.) 6.37 (Towers of Hanoi) Every budding computer scientist must grapple with certain classic problems, and the Towers of Hanoi (see Fig. 6.23) is one of the most famous. Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack has 64 disks threaded onto one peg and arranged from bottom to top by decreasing size. The priests are attempting to move the stack from this peg to a second peg under the constraints that exactly one disk is moved at a time and at no time may a larger disk be placed above a smaller disk. A third peg is available for temporarily holding disks. Supposedly, the world will end when the priests complete their task, so there is little incentive for us to facilitate their efforts. Let us assume that the priests are attempting to move the disks from peg 1 to peg 3. We wish to develop an algorithm that will print the precise sequence of peg-to-peg disk transfers. If we were to approach this problem with conventional methods, we would rapidly find ourselves hopelessly knotted up in managing the disks. Instead, if we attack the problem with recursion in mind, it immediately becomes tractable. Moving n disks can be viewed in terms of moving only n 1 disks (and hence the recursion) as follows: a) Move n 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding area. b) Move the last disk (the largest) from peg 1 to peg 3. c) Move the n 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding area. The process ends when the last task involves moving n = 1 disk (i.e., the base case). This task is accomplished by simply moving the disk, without the need for a temporary holding area. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply