Advertisement
akosiraff

TestDigits & TestMeanVal JAVA

Oct 29th, 2013
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1.  
  2. Download: http://solutionzip.com/downloads/testdigits-testmeanval-java/
  3. Write a recursive method nmbDigits which takes an integer value as parameter and returns the number of digits of the parameter value (for example nmbDigits(2054) is 4). Write a driver program TestDigits.java to test the method. In the driver program, ask the user to enter an integer value, invoke the method (passing the entered value as parameter) and display the result.
  4. Note. If an illegal value is entered, a message should be shown and the user will be required to enter a correct value.
  5. Hint: Divide the number by 10 and count for how many times you can do that.
  6. Consider a method which calculates and returns the arithmetic mean of an array of integer values.
  7. a) Design and implement meanIter, the iterative version of the method.
  8. b) Design and implement meanRec, the recursive version of the method.
  9. c) Write a driver program TestMeanVal.java to test the two methods. Consider an array of size 20 populated with random integer values in the range 1 .. 99. Invoke the two methods and display the result.
  10. Hint:
  11. For the recursive method, try to deduce a recurrence relation between meanRec(n) and meanRec(n-1).
  12. Start from meanRec(n) formula (consider an array a of size n)
  13. meanRec(n) = (a[n-1] + a[n-2] + … + a[0]) / n
  14. and organize the right side of the above assignment statement to highlight there the expression of meanRec(n-1) which is: (a[n-2] + a[n-3] + … + a[0]) / (n-1).
  15. Submission requirements
  16. Your completed assignment consist of the TestDigits.java and TestMeanVal.java files.
  17.  
  18.  
  19. Download: http://solutionzip.com/downloads/testdigits-testmeanval-java/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement