|
Fundamental Programming Concepts
Summer 2000 |
|
Updated to correct errors in part I and II.
Overview | |
This lab requires the use of the various loop constructs that have been discussed in lecture. Read the submission requirements below -- they are different from previous labs!
|
|
Part I: SquareRoot | |
Write a program that approximates square roots using the
Newton-Raphson method, described below.
InputThe input to your program is the number for which the user wants to find the square root, a guess for the square root, and the desired accuracy of the final result. You should validate the number by making sure it is legal to take the square root of it. You should also validate that the desired accuracy is greater than zero. ProcessingAn early method of approximating square roots was the Newton-Raphson method. This method consisted of starting with an approximation to the square root of a number and then getting successively better approximations until the desired degree of accuracy was achieved. Each new guess is defined to be: newGuess = (oldGuess + number / oldGuess) * .5 So, if the number entered were 34 and the first approximation were 5, the second and third approximations would be: (5 + 34 / 5) * .5 = 5.9 (5.9 + 34/5.9)*.5 = 5.83135593... We can get as close to the actual square root as we want by defining a tolerance value that represents how much error we are willing to tolerate in our answer. Letting this value be named desiredAccuracy, we would keep computing new approximations until | newGuess - oldGuess | < desiredAccuracy. You can compute absolute values with the Java library function Math.abs(). OutputOutput each successive approximation, including the initial guess, the final approximation, the actual square root as computed by Math.sqrt(), and the absolute value of the difference between the final approximation and the sqrt()-computed value. Each output value should have an appropriate label preceding it in the output, e.g.: Approximation #2: 5.9 Sample Run
|
|
Part II: PrimeFactorization | |
Write a program that produces the prime factorization of an
integer given by the user. After each number, you should prompt the user
whether he or she wants to compute another factorization. The program
should keep running until the user responds negatively.
Note: this problem will use the caret (^) to indicate exponentiation. For example, 4^2 = 16. InputThe input is a single number, n, which must be >= 2. You should validate this input. After each computation, you will also need to prompt the user about quitting. You may ask for yes or no, Y or N, or some other values -- it's up to you -- but you must validate the input. ProcessingTo help you, here is pseudocode for an algorithm that performs prime factorization. Recall that the left arrow (<-) is pseudocode for assignment. Prime-Factor(n) test-factor <- 2 do until n = 1 if n is divisible by test-factor p <- highest power of test-factor that evenly divides n output factor and power n <- n / test-factor^p test-factor <- test-factor + 1 You should implement this algorithm as a method. The algorithm also suggests that you'll need two other methods:
OutputPrint the prime factorization as a product of exponentiations. Sample Run
Extra CreditFor a small amount of extra credit, you may:
|
|
Submitting | |
In our continuing quest to make submitting labs as easy
as possible, we are now only asking for the .java file for each program.
You should gather the .java file for each of the two programs into a single folder called Lab6, then submit that entire folder.
Your files and the classes they contain must be named exactly as shown
below.
Your folder should contain exactly the following files:
It is our hope that having to submit fewer files will mean fewer errors and thus improve your grades on labs. You will submit your lab using FTP.
|