Problem 27
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. [Hint: You might want to use Euclid’s Algorithm. You can find information about the algorithm at en.wikipedia.org/wiki/Euclidean_algorithm.] Incorporate the method into an application that reads two values from the user and displays the result.
Problem 28
Write a method quality Points that inputs a student’s average and returns 4 if the 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 application that reads a value from the user and displays the result.
Problem 29
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 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.]
Problem 30
Computers are playing an increasing role in education. Write a program that will help an elementary school student learn multiplication. Use a Random object to produce two positive one- digit integers. The program should then prompt the user with a question, such as How much is 6 times 7?
Problem 33
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 technique employed in this problem is similar to a binary search, which is discussed in Chapter 16, Searching and Sorting.]
Problem 34
An integer is said to be prime if it is divisible by only 1 and itself. For example, 2,3,5 and 7 are prime, but 4,6,8 and 9 are not: a) Write a method that determines whether a number is prime. b) Use this method in an application i that determines and displays all the prime numbers less than \(10,000 .\) How many numbers up to 10,000 do you have to test to ensure that you have found all the primes? c) Initially, you might think that \(n / 2\) is the upper limit for which you must test to see whether a number is prime, but you need only go as high as the square root of \(n .\) Why? Rewrite the program, and run it both ways.
Problem 35
Exercise 6.30 through Exercise 6.32 developed a computer-assisted instruction program to teach an elementary school student multiplication. Perform the following enhancements: a) Modify the program to allow the user to enter a school grade-level capability. A grade level of 1 means that the program should use only single-digit numbers in the problems, a grade level of 2 means that the program should use numbers as large as two digits, and so on. b) Modify the program to allow the user to pick the type of arithmetic problems he or she wishes to study. An option of 1 means addition problems only, 2 means subtraction problems only, 3 means multiplication problems only, 4 means division problems only and 5 means a random mixture of problems of all these types.
Problem 36
Write method distance to calculate the distance between two points \((x I, y I)\) and \((x 2, y 2)\) 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.
Problem 38
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 are not familiar with these number systems, read Appendix E first.