Programming Assignment 8


Due: Week of Mar. 20 before lab


  1. (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.
  2. (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.
  3. (25 points) Problem 7.27 of the book. Identical arrays. The arrays list1 and list2 are identical if they have the same contents. Write a method that returns true if list1 and list2 are identical, using the following header:
    public static boolean equals(int[] list1, int[] list2);
    Write a test program that prompts the user to enter two lists of integers and displays whether the two are identical. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list. You are allowed to use Array.sort, but at least consider trying to think of how to solve it without that function.
  4. (25 points) Problem 7.31 of the book. Merge two sorted lists. Write the following method that merges two sorted lists into a new sorted list.
    public static int[] merge(int[] list1, int[] list2);
    Implement the method in a way that takes at most list1.length + list2.length comparisons. Write a test program that prompts the user to enter two sorted lists and displays the merged list. Note that the first number in the input indicates the number of the elements in the list. This number is not part of the list. You cannot use Arrays.sort().
  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"};