Assignment #3 - Chapter 4 Exercises - Methods
Due: Wednesday, June 4
From chapter 4, do programming exercises
4.2, 4.3, 4.7, 4.11 (starting on page 171),
noting the added instructions below.
Filenames should be
- Prob4_2.java
- Prob4_3.java
- Prob4_7.java
- Prob4_11.java
To do these exercises, you will need the
MyInput class that was used in
lecture. and in HW 1. Paste this class into each of your own program
files.
Extra Instructions
- For 4.2, in addition to the method, write a test program that asks the
user to input a number (stored in a variable of type long), and
then which calls the method and prints out the answer (see sample
output)
- For 4.3, note that this uses the interest formula in exercise
2.9.
- For 4.7, in addition to the method, write a main method that asks the
user to input a number (num) and then runs the method to compute
the suqare root, and prints the answer. For the method, continue until
the difference between nextGuess and lastGuess is
less than 0.0001.
- For 4.11, do only the recursive method. Do not do the
iterative method. Also write a sample main program that asks the user to
input two positive integers and then calls the GCD method and prints the
result. (Note: positive number means > 0). The recursive algorithm is
given almost verbatim in the problem. There are also a couple other
possible recursive algorithms for computing GCD, as follows (you may use
any of them, but it is suggested to try all of them):
- Euclid's Algorithm
GCD(m,n) is m, if n is equal to zero.
GCD(m,n) is GCD(n, m%n) otherwise
- Dijkstra's Algorithm
GCD(m,n) is m, if m equals n.
GCD(m,n) is GCD(m - n, n) if m is greater than n.
GCD(m,n) is GCD(m, n - m) otherwise.
Sample Outputs
Sample user input is underlined.
Sample output for 4.2
Programming Exercise 4.2 'Summing digits'
Input number : 2345
Sum of digits = 14
Sample output for 4.3
See textbook (sample output printed in the problem)
Sample output for 4.7
Programming Exercise 4.7 'Square root'
Input number : 9
square root = 3.0000000000393214
Sample output for 4.11
Programming Exercise 4.11 'GCD recursive'
Input first number : 45
Input second number : 10
GCD(45, 10) = 5
Compiling
Remember that the compile command is "javac", at the unix command prompt.
Compile your code on program.cs.fsu.edu, and then run your programs with
the "java" command.
Submitting:
Submit programs through the submission web page.