Hands On Java: Project 9



  1. Practice using sequences of values.
  2. Practice reading data from a file.


Your instructor will tell you which one of the following projects to do.


9.1. When people talk about an average value, most often they are referring to the arithmetic mean as we computed in the exercise. There are, however, other "measures of central tendency" that are in use. For example, the geometric mean of a sequence of n values is the n'th root of the product of all the values. A simple algorithm to compute the geometric mean of a sequence of values is

   a. Let seq be a sequence with at least one value.
   b. Let n be the number of values in the sequence.
   c. Initialize gMean to 1.
   d. For each valuei in the sequence
         Multiply gMean by valuei(1/n)
   e. Return gMean.

Write a method that, given an array of numbers, computes the geometric mean of those numbers. Then use this method to write a program that, given the name of a file containing an arbitrary sequence of numbers, computes and displays the geometric mean of that sequence.

9.2. A certain on-line testing program records student exam results in a text file, each line of which has the form:

  name examScore

Write a program that (using an array) analyzes student performance on an exam using the information from such a file. The program should input the name of the text file, and then display the worst score, the best score, the average score, the standard deviation, and a histogram -- a bar graph indicating the frequency with which a given score occurred. For example, if three people scored 74, five people scored 75, six people scored 76, no one scored 77 and two people scored 78, then that portion of the histogram should appear as:

   74: ***
   75: *****
   76: ******
   78: **

Entries below the worst or above the best should not be displayed.

9.3. Each year, the well-known meteorologist Dr. H. Tu Oh creates a file containing the year's 12 monthly precipitation totals (e.g., rain1997.data). Write a program that, given the names of two of these files, will create a file containing an easy-to-read analysis comparing the two sets of readings, including which of the two years was the wettest (and by how much), the average monthly precipitation for each year, and the wettest and driest months in each year.

9.4. Write a program that, given the name of a text file, reads that file and counts the number of occurrences of each alphabetic letter in the file. Your program should use an array of length 26 to count the occurrences of the 26 alphabetic letters, and treat upper and lower-case instances of a letter as the same letter.

Run your program on several large text files (e.g., save on-line help files or manuals and use them as inputs to your program). Study the results looking for patterns; then write a paragraph explaining how your observations could be useful to a person

  1. trying to decode a message encoded using the Caesar cipher; or
  2. competing on the Wheel of Fortune game show.

Submit this paragraph along with your program and its output.

9.5. A playing card has two attributes, its rank (e.g., 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A) and its suit (e.g., clubs, diamonds, hearts, spades). Design and build a PlayingCard class that models a playing card. Your class should provide operations to construct, input, output, compare, and access the attributes of a PlayingCard object.

A deck of cards is simply a sequence of cards. Design and create a class DeckOfCards that represents such objects by using an array to store a sequence of PlayingCard values. Your DeckOfCards class should provide operations to construct, shuffle, and take the top card. The class constructor should initialize the DeckOfCards as a new deck of 52 cards (i.e., 2-clubs, 3-clubs, ..., A-clubs, 2-diamonds, ..., A-diamonds, 2-hearts, ..., A-hearts, 2-spades, ..., A-spades). The shuffle operation should rearrange the cards in a deck in random order. The final operation should remove the top card from the deck, and return that card.

To test your classes, write a program that plays a simple card game (i.e., blackjack, go fish, etc.) against a human opponent.

Turn In: A hard copy of this grade sheet, attached to hard copies of

  1. all source files you created for this project; and
  2. the output from an execution of your executable program.

Don't forget to clean up your directory when you are all finished...

Back to the Lab Exercise

Back to This Lab's Home Page


Back to the Table of Contents

Back to the Introduction

Copyright 2000 by Prentice Hall. All rights reserved.