Geocities web hosting - Chapter 6 Methods 309 6.28 The greatest common
Chapter 6 Methods 309 6.28 The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers. Write a method gcd that returns the greatest common divisor of two integers. Incorporate the method into an applet that reads two values from the user. Display the result of the method in the status bar. 6.29 Write a method qualityPointsthat inputs a student s average and returns 4 if a student’s average is 90 100, 3 if the average is 80 89, 2 if the average is 70 79, 1 if the average is 60 69 and 0 if the average is lower than 60. Incorporate the method into an applet that reads a value from the user. Display the result of the method in the status bar. 6.30 Write an applet that simulates coin tossing. Let the program toss a coin each time the user presses the Toss button. 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 false for tails and true for heads. [Note: If the program realistically simulates coin tossing, each side of the coin should appear approximately half the time.] 6.31 Computers are playing an increasing role in education. Write a program that will help an elementary school student learn multiplication. Use Math.randomto produce two positive one-digit integers. The program should then display a question in the status bar, such as How much is 6 times 7? The student then types the answer into a JTextField. Next, the program checks the student’s answer. If it is correct, draw the string “Verygood!”on the applet and ask another multiplication question. If the answer is wrong, draw the string “No.Please try again.” on the applet and let the student try the same question again repeatedly until the student finally gets it right. A separate method should be used to generate each new question. This method should be called once when the applet begins execution and each time the user answers the question correctly. All drawing on the applet should be performed by the paint method. 6.32 The use of computers in education is referred to as computer-assisted instruction (CAI). One problem that develops in CAI environments is student fatigue. This problem can be eliminated by varying the computer’s dialogue to hold the student’s attention. Modify the program of Exercise 6.31 so the various comments are printed for each correct answer and each incorrect answer as follows: Responses to a correct answer Very good! Excellent! Nice work! Keep up the good work! Responses to an incorrect answer No. Please try again. Wrong. Try once more. Don’t give up! No. Keep trying. Use random-number generation to choose a number from 1 to 4 that will be used to select an appropriate response to each answer. Use a switch structure in the paint method to issue the responses. 6.33 More sophisticated computer-aided instruction systems monitor the student s performance over a period of time. The decision to begin a new topic is often based on the student s success with previous topics. Modify the program of Exercise 6.32 to count the number of correct and incorrect Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01