COMP 2100 Lab Exercise 2: Extending the Statistic class
(12 points)
Turn in before the next lab period.

A subclass for weighted statistics

Introduction

In Project 1, you created the Statistic class from a specification. For this short exercise, you will create a subclass to represent a specialization of Statistic, the weighted statistic.

A weighted statistic is one in which each collected value carries a weight along with it, as a measure of its significance. This technique is frequently used to measure phenomena that vary discretely over a period of time.

Example of weighted statistic

For example, suppose you wanted to collect statistics concerning the length of a line of people waiting for a bank teller. The length of the line changes whenever a person enters or leaves it. The average length of the line should reflect not only the number of people, but also the amount of time that they were in line.

Suppose we are interested in the time period from 3:00 pm until 3:10 pm. At 3:00 one person gets into the empty line and at 3:02 a second person gets in line. At 3:10, both are still in line. If we were monitoring line length and collected a value at each change, the collected values would be 1 and 2, and the average line length from 3:00 until 3:10 would be (1+2)/2 = 1.5.

This is misleading, since for 8 of the 10 minutes the line actually contained two people. The average should be 1.8 (one person for 2 minutes and two people for 8 minutes). If we collect a weight of 2 (minutes) with the first value (line length 1) and a weight of 8 (minutes) with the second value (line length 2), the average length will be: ((1*2)+(2*8))/(2+8) = (2+16)/(10) = 1.8.

Note that this is equivalent to taking one unweighted sample per minute, regardless of whether a change occurred or not. At 3:00 pm collect 1; at 3:01 collect 1; at 3:02 collect 2; at 3:03 collect 2, at 3:04 collect 2, etc. You'd end up with value 1 being collected twice and value 2 being collected eight times.

Details

1. Making the most of inheritance

You are to define a class called WeightedStatistic which extends Statistic. It offers its clients all the Statistic methods, along with an overloaded collect method and its own constructor. Here are more details.

2. Protected instance variables

You will need to modify Statistic.java to make its instance variables protected instead of private. That will allow methods in WeightedStatistic to use them. Otherwise, leave Statistic.java alone.

3. Inheriting or overriding the methods inherited from Statistic

The WeightedStatistic class will inherit all the methods from Statistic. Here is what the Statistic methods mean for weighted statistics. Most (or all, if you do it right) can be used as inherited, and for those you don't need to do anything!

Design your WeightedStatistic implementation to override as few inherited methods as possible. Any overriding method you write should be documented by placing the @Override annotation directly above the method definition. Note that annotations are not Javadoc.

4. WeightedStatistic methods

In addition to any overrides required from item #3 (there may be none), you need to define

5. Testing

I've updated the StatisticDriver class from Project 1 to add more tests for WeightedStatistic. Download StatisticDriverEx2.zip (right-click, Save Link As...), then unzip (right-click the zip icon, Extract All...) and put it into the same folder with your other classes.

Note it will run all the Project 1 tests too. This will assure that none of your changes to Statistic.java affect its correctness. We call this regression testing.

Scoring

Maximum Point totals are as follows:
WeightedStatistic componentmax points
Constructor1
Method(s) 9
Javadoc for class and methods2

To Turn In

Drop both Statistic.java and WeightedStatistic.java into your I drive dropbox.


[ COMP 2100 | Peter Sanderson | Math Sciences home page | Otterbein ]

Last updated:
Peter Sanderson (PSanderson@otterbein.edu)