Hands on Testing Java: Lab #9

Homework Projects

Configure Your Project

  1. Create a file named Design/project09.txt.
  2. Create a new package in your project named edu.INSTITUTION.USERNAME.hotj.project09.
  3. Create the classes needed for the assigned project as part of the edu.INSTITUTION.USERNAME.hotj.project09 package.

The Projects

Your instructor will assign you one of the problems below. To solve your problem, write a program that reads the necessary information to compute and output the indicated values, as efficiently as possible. Following the pattern in the lab exercise, first, design using OCD. Then code your design in Java using stepwise translation. Test your program thoroughly.

When executing your program to hand in as part of the project, a JUnit test case should test each method at least five times, but the test case itself needs to be executed just once. A driver program should be run at least five times with different inputs each time.

Project #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". 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

Algorithm
  1. Let values be a sequence with at least one value.
  2. Let n be the number of values in the sequence.
  3. Initialize gMean to 1.
  4. For each value i:
        Set gMean to be the product of gMean and value i.
    End for.
  5. Return the nth root of gMean.

Write a method for the GradeBook class that computes the geometric mean of the grades, using the array of students as values. Add this computation to the output. Also be sure to add a test method to test this new computation.

Project #9.2: Implement a Histogram class that records a histogram of integer values. A histogram counts the number of times each of the values occur.

Usually we see histograms as a graph:

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

This graph of the histogram indicates that the value 74 was seen three times, the value 75 was seen five times, 76 seen six times, 77 seen zero times, and 78 seen two times. All other values were never seen. (Note that these values could be temperatures, test scores, ages, or some other kind of data; the histogram doesn't care!)

Write a Histogram class that keeps track of the number of times that particular values occur in a data set. (Do not store the graph! Store the counts!) The constructor for the class should receive minimum and maximum values for the data range; this makes saving the counts and creating a graph a bit easier.

Write a Histogram#toString() method that displays the raw data; for example,

74:3 75:5 76:6 78:2

Then write Histogram#toGraph() to return a graph of the histogram as a String.

Both methods should include only the values between the minimum and maximum (inclusive). Histogram#toString() should not include values with a 0 count.

Write test methods for both of these methods.

Project #9.3: 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

Turn the following things:

  1. This grade sheet
  2. An OCD design for one of your methods.
  3. All source files you created for your project.
  4. Sample executions of your test cases and drivers (as applicable).

Lab Home Page | Prelab Questions | Lab Exercise | Homework Projects