A tiny OOP challenge (Python, Java, C++, any language with class) /
Let's try something different. A task.
Write a class "Averager" which must be able to compute the average of a whole bunch of numbers. Here are the methods to be implemented:
- void add(double number) ... add a number to the dataset
- double getAverage() ... gives the average of all numbers
- double getSum() ... gives the sum of all numbers
- int getNum() ... gives the count of all numbers so far
- void reset() ... empty data set
Here's an example call and use of this calls in C++:
int main()
{
double testvals[] = { 3.0, 2.0, 1.0, 0.0, 4.0 };
Averager *averager = new Arerager();
int num = sizeof(testvals) / sizeof(double);
for (int i = 0; i < num; i++)
{
averager->add(testvals[i]);
}
cout << "Durchschnitt: " << averager->getAverage() << endl; // 2.0
cout << "Summe: " << averager->getSum() << endl; // 10.0
cout << "Anzahl: " << averager->getNum() << endl; // 5
averager.reset();
for (int i = 0; i < num; i++)
{
averager->add(testvals[i]);
averager->add(testvals[i]);
}
cout << "Durchschnitt: " << averager->getAverage() << endl; // 2.0
cout << "Summe: " << averager->getSum() << endl; // 20.0
cout << "Anzahl: " << averager->getNum() << endl; // 10
}
I wish you guys a nice saturday.
PS you can make code blocks with 3 ` in a row.
like so
I think of this challenge because I have to learn C++ for work.
Take care
@qed
In Python (my personal favorite...also check out sage and https://www.cocalc.com), here's how I would do it:
Same sort of list comprehensions as C# :) Class looks similar in implementation as well.
Writing these sorts of things can definitely help with learning programming. Small little programs like this are good practice.
One way to expand on this is to think about the exercise itself. Is this class a good example of object oriented design? What constraint do the interface place on us?
For example,
void add(double number)
, forces us to implement our object as a mutable object. Sometimes we may not want a mutable object, perhaps we want our objects to be thread safe or believe immutability helps with reasoning.Imagine we change the interface instead to
Averager add(double number)
. Now we are no longer constrained to implement our our class mutably. In fact, we get some nice syntactic features as well.But I think the real benefits come in when we remove ourselves from the OO constraint. This class inherently limits what interpretations we can give to our data. What if we need the product, the median, and the average, but we don't own the code to this object. We must proxy our object, (or write a new one) ultimately, duplicating our state in two places, making ourselves potentially out of sync.
This sort of encapsulation actually makes your code less reusable and more susceptible to error.
Why not just use a list and write functions that operate on that list? We now don't constrain ourselves on future functionality.