Programming Assignment 8


Due: Wed. Nov 1 before lab


  1. (25 points) Problem 18.5 of the book. Sum series. Write a recursive method to compute the following series:
    m(i) = 1/3 + 2/5 + 3/7 + ... + i/(2i+1)
    Write a test program that displays m(i) for i=1, 2, ...10.
    • Name the file and program ch18pr5.java.
    • Example execution (java ch18pr5):
        1  0.333333
        2  0.733333
        3  1.161905
        4  1.606349
        5  2.060895
        6  2.522433
        7  2.989100
        8  3.459688
        9  3.933372
       10  4.409563
  2. (25 points) Problem 18.11 of the book. Sum the digits in an integer using recursion. Write a recursive method that computes the sum of the digits in an integer. Use the following method header:
    public static int sumDigits(long n);
    Write a test program that prompts the user to enter an integer and displays its sum.
    • Name the file and program ch18pr11.java.
    • Example execution (java ch18pr11) user provides input:
      Enter an integer: 234
      The sum of the digits is 9
  3. (25 points) Problem 7.3 of the book. Count occurrence of numbers. Write a program that reads the integers between 1 and 100 and counts the occurrences of each. Assume the input ends with 0. Note that if a number occurs more than one time, the plural word "times" is used in the output. You must use an array in this problem, no other container can be used, e.g., ArrayList.
    • Name the file and program ch7pr3.java.
    • Example execution (java ch7pr3) user provides input:
      Enter the integers between 1 and 100: 2 5 6 4 3 23 43 2 0
      2 occurs 2 times
      3 occurs 1 time
      4 occurs 1 time
      5 occurs 1 time
      6 occurs 1 time
      23 occurs 1 time
      43 occurs 1 time
  4. (25 points) Problem 7.25 of the book. Algebra: solve quadratic equation. Write a method for solving a quadratic equation using the following header:
    public static int solveQuadratic(double[] eqn, double[] roots);
    The coefficients of a quadratic equation ax2 + bx + c = 0 are passed to the array eqn and the real roots are stored in roots. The method returns the number of real roots. Write a program that prompts the user to enter values for a, b, and c and displays the number of real roots and all real roots.
    • Name the file and program ch7pr25.java.
    • Example execution (java ch7pr25) user provides input:
      Enter values for a, b, and c of ax^2 + bx + c = 0: 5.4 6.2 1.5
      5.400000x^2 + 6.200000x + 1.500000 = 0 has 2 real roots.
      Real root 0: x = -0.346515
      Real root 1: x = -0.801633
    • Example execution (java ch7pr25) user provides input:
      Enter values for a, b, and c of ax^2 + bx + c = 0: 5 2 1
      5.000000x^2 + 2.000000x + 1.000000 = 0 has 0 real roots.
    • Example execution (java ch7pr25) user provides input:
      Enter values for a, b, and c of ax^2 + bx + c = 0: -3 -24 -48
      -3.000000x^2 + -24.000000x + -48.000000 = 0 has 1 real roots.
      Real root 0: x = -4.000000
  5. Bonus. (20 points) Problem 7.35 of the book. Game: hangman. Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to store words as follows:
    // Add any words you wish in this array
    String[] words = {"write", "that", "program"};
    • Name the file and program ch7pr35.java.
    • Example execution (java ch7pr35) user provides input:
      (Guess) Enter a letter in word ******* > p
      (Guess) Enter a letter in word p****** > r
      (Guess) Enter a letter in word pr**r** > p
           p is already in the word
      (Guess) Enter a letter in word pr**r** > o
      (Guess) Enter a letter in word pro*r** > g
      (Guess) Enter a letter in word progr** > n
           n is not in the word
      (Guess) Enter a letter in word progr** > m
      (Guess) Enter a letter in word progr*m > a
      The word is program. You missed 1 time
      Do you want to guess another word? Enter y or n> n


General Instructions, Turning in assignments, and Grading

General Instructions

Turn in Instructions

Each assignment will be turned in to both Blackboard (soft copy) and in class (hard copy). Assignments are due BEFORE, let me repeat, before class starts. This does not mean five minutes after class starts.

Points