This exercise is intended to be completed in the order discussed in Class writing order.

Write a DataSet class that allows a client class (a class that uses the DataSet class) to perform each of the tasks below.

Write a DataSetTester class with a main method that tests the DataSet class.

Example values

Values in dataset: 10, 1
Sum: 11
Average: 5.5

Values in dataset: 5, 9, 3
Sum: 17
Average: 5.66...

Hints

Try the exercise above in the order discussed in Class writing order. If you’re stuck, there are hints below for each part.

Hint for method headers

It is common to write test code before writing, or while writing, the code it tests. This is known as test driven development.

If you’re stuck figuring out the method headers for the DataSet class, consider writing the test code first. If you’re still stuck, open my test code (linked below) and use it to write the method headers for your DataSet class.

DataSetTester.java

Hint for instance variables

A class’s instance variables exist to support its methods. The DataSet class accepts values one at a time. At any time, it should be able to return the sum and the average of all the values. The instance variables should support this behavior.

It is not necessary to store each value. This exercise does not require the use of an array or list.

Hint for code inside methods

Constructors

The purpose of a constructor is to initialize the instance variables. For each DataSet constructor, consider an appropriate starting value for each instance variable.

Adding a value

The method that allows a value to be added is a mutator method. It changes the value of at least 1 instance variable. Consider what needs to be updated when a new value is added. Remember, it is possible to add any number of values to a dataset, not just 1 or 2.

Computing the average

Java uses the same operator (/) for both integer and floating point division. See Division operations for additional discussion.

Solution & comments

See the Class writing exercise solution or review it with AP CS Tutor Brandon Horn.

Comment on Class writing exercise