•siliconvalleyguy
Strain
Karma: 2
Deconectat
Mesaje: 6


« Răspunde #4 : Octombrie 19, 2011, 11:55:47 » 

Chapter 5 Methods
1. Write a program that displays the value of the mathematical constant ƒÓ = (1 + sqrt(5)) / 2 This constant ƒÓ is called the golden ratio. Classical mathematicians believed that this number represented the most aesthetically pleasing ratio for the dimensions of a rectangle, but it also turns up in computational mathematics. 2. In highschool algebra, you learned that the standard quadratic equation ax2 + bx + c = 0 has two solutions given by the formula x = (b +/ sqrt (b.2 +/ 4ac)) / 2a The first solution is obtained by using + in place of +/ the second is obtained by using  in place of +/. Write a Java program that accepts values for a, b, and c, and then calculates the two solutions. If the quantity under the square root sign is negative, the equation has no real solutions, and your program should display a message to that effect. You may assume that the value for a is nonzero. 3. The Fibonacci sequence, in which each new term is the sum of the preceding two, was introduced in Chapter 4, exercise 9. Rewrite the program requested in that exercise, changing the implementation so that your program calls a method fibonacci(n) to calculate the nth Fibonacci number. In terms of the number of mathematical calculations required, is your new implementation more or less efficient that the one you used in Chapter 4? 4. Write a method raiseIntToPower that takes two integers, n and k, and returns nk. Use your method to display a table of values of 2k for all values of k from 0 to 10. 5. Write a method raiseRealToPower that takes a floatingpoint value x and an integer k and returns xk. Implement your method so that it can correctly calculate the result when k is negative. Use your method to display a table of values of ðk for all values of k from –4 to 4. 6. Write a method nDigits(n) that returns the number of digits in the integer n, which you may assume is positive. Design a main program to test your method. For hints about how to write this program, you might want to look back at the DigitSum program that was given in Figure 46. 7. Write a predicate method isPerfectSquare(n) that returns true if the integer n is a perfect square. Remember that the method Math.sqrt returns a double, which is therefore only an approximation of the actual square root. 8. Write a predicate method askYesNoQuestion(str) that prints out the string str as a question for the user and then waits for a response. If the user enters the string "yes", the askYesNoQuestion method should return true; if the user enters "no", the method should return false. If the user enters anything else, askYesNoQuestion should remind the user that it is seeking a yesorno answer and then repeat the question. 9. The values of the combinations method used in the text are often displayed in the form of a triangle using the following arrangement: C(0,0) C(1,0) C(1,1) C(2,0) C(2,1) C(2,2) C(3,0) C(3,1) C(3,2) C(3,3) C(4,0) C(4,1) C(4,2) C(4,3) C(4,4) and so on. This figure is called Pascal’s Triangle after the seventeenthcentury French mathematician Blaise Pascal, who described it, even though it was known by Chinese mathematicians over 2000 years ago. Pascal’s Triangle has the interesting property that every interior entry is the sum of the two entries above it. Write a Java GraphicsProgram that uses GLabel objects to display the first eight rows of Pascal’s Triangle. 10. An integer greater than 1 is said to be prime if it has no divisors other than itself and one. The number 17, for example, is prime, because it is divisible only by 1 and 17. The number 91, however, is not prime because it is divisible by 7 and 13. Write a predicate method isPrime(n) that returns true if the integer n is prime, and false otherwise. As an initial strategy, implement isPrime using a bruteforce algorithm that simply tests every possible divisor. Once you have that version working, try to come up with improvements to your algorithm that increase its efficiency without sacrificing its correctness. 11. Greek mathematicians took a special interest in numbers that are equal to the sum of their proper divisors (a proper divisor of n is any divisor less than n itself). They called such numbers perfect numbers. For example, 6 is a perfect number because it is the sum of 1, 2, and 3, which are the integers less than 6 that divide evenly into 6. Similarly, 28 is a perfect number because it is the sum of 1, 2, 4, 7, and 14. Write a predicate method isPerfect(n) that returns true if the integer n is perfect, and false otherwise. Test your implementation by writing a main program that uses the isPerfect method to check for perfect numbers in the range 1 to 9999 by testing each number in turn. Whenever it identifies a perfect number, your program should display that number on the screen. The first two lines of output should be 6 and 28. Your program should find two other perfect numbers in that range as well. 12. Although Euclid’s algorithm and the problem of finding perfect numbers from the preceding exercise are both drawn from the domain of mathematics, the Greeks were fascinated with algorithms of other kinds as well. In Greek mythology, for example, Theseus of Athens escapes from the Minotaur’s labyrinth by taking in a ball of string, unwinding it as he goes along, and then following the path of string back to the exit. Theseus’s strategy represents an algorithm for escaping from a maze, but it is not the only algorithm he could have used to solve this problem. For example, if a maze has no internal loops, you can always escape by keeping your right hand against a wall at all times. This algorithm is called the righthand rule.
