CSE 283 Introduction to Object Oriented Design

Barbara Nostrand, Ph.D.

CSE 283 Labs
Welcome to the site for CSE 283 Labs!


Lab 9: Projects

Prev | Next | Lab 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 called Analyze 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. Once the file is read, your program should display a histogram for each of the 26 modern English letters. For extra credit, produce a list of the letters sorted by frequency.

There are several possible methods for detecting the end of a file. One involves catching the EOFException. Another depends on a special return value when you attempt to read past the end of the file. This method is used in a demonstration program List.java which displays the contents of a file on the screen.

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 in a file called Analysis.doc 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.

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 called Blackjack that plays blackjack against a human opponent. The computer will take the role of the dealer. In black Jack, the cards have the following values:

  • An Ace can count as either 1 or 11, as demonstrated below.
  • The cards from 2 through 9 are valued as indicated.
  • The 10, Jack, Queen, and King are all valued at 10.
After the initial deal of a face down card followed by a face up card to each of the players and the dealer, the players take turns drawing cards. Two aces can total either two or twelve. Here are the victory conditions for each play:
  • Players win immediately if they are initially dealt twenty one uless the dealer also has twenty one in which case a draw is declared.
  • Anyone (player or dealer) whose cards total more than twenty one points looses immediately.
  • The players win if they have a higher score than the dealer when the dealer finishes drawing cards.
This is a gambling game. Each play possibly results in money changing hands. The payoffs are as follows:
  • If a player is delt 21 and the dealer was not delt a 21, the player is paid 1.5 times their bet.
  • If the dealer goes bust by going over 21, then all remaining players are paid 1.0 times their bet.
  • If a player's hand totals more than the dealers, then he receives 1.0 times his wager
  • If the player and deal tie, then no money changes hands
  • If the player goes bust by going over 21, the player loses his wager
  • If at the end of play the dealer's hand is higher the players hand, then the player loses his wager
The dealer plays with a fixed strategy:
  • The dealer first receives a secret card whose value is hidden until the player has finished taking cards and one public card.
  • The dealer takes cards once the player has finished taking cards.
  • The dealer must continue to take cards ("hit") until his total is 17 or greater.
  • An Ace in the dealer's hand is always counted as 11 if possible without the dealer going over 21.
    • (Ace,8) would be 19 and the dealer would stop drawing cards ("stand").
    • (Ace,6) is 17 and again the dealer will stand.
    • (Ace,5) is only 16, so the dealer would hit. He will continue to draw cards until the hand's value is 17 or more.
    • (Ace,5,7) is only 13 so he hits again. (Ace,5,7,5) makes 18 so he would stop ("stand") at that point.
Write a program called Blackjack which shuffles the cards at the begining of the game and reshuffles the cards between hands if more than half of the deck has been dealt.  At the begining of the game give the player as the player the total amount of money (points) that they want to play with and allow them to bet one or all of their remaining points at the begining of each play.  At the end of each play allow them to cash in and quit the game.  You may implement a GUI version of Blackjack for 5 points extra credit.  You can download a collection of card images. 

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

Prev | Next | Lab 9




Lab 0

Lab 1

Lab 2

Lab 3

Lab 4

Lab 5

Lab 6

Lab 7-8

Lab 9

Lab 10

Lab 11

Lab 12

Lab 13

Last modified: 2007 OCT 30