A more OO Histogram (Required Exercise)
The big limitation of the histogram we implemented last week is
that it could only maintain one histogram at a time, due to the
reliance on static variables.
In this exercise, we will use object oriented principles to
create a class that can be instantiated multiple times, to maintain
and draw multiple histograms. You can see this in action in the
main method of the Runner class, which reads through a file
containing (fictitious) student grades, and plots seperate
histograms for each subject as well as a histogram that combines
all subjects. It would be impossible to manage these multiple
histograms simultaneously with the static version of the Histogram
from last week.
You have three tasks for this exercise:
Task 1: Decide what should or should not be declared as
static.
For every field and method in this class, replace the
static_or_not placeholder with either static (if you think it
should be static) or nothing (if not).
When making your decisions, try to think about what values and
methods should be shared across all instances of a Histogram vs.
which should be seperate for each Histogram. Try to make the
methods and fields static as much as possible, unless there is a
clear reason to make them not static.
Hint: there is at least one field and a couple
of methods that should be declared as static. Marking will fail
unless you choose the correct fields and methods to be static.
Task 2: Decide what should be declared as public or
private.
For every field and method in this class, replace the
public_or_private placeholder with either public or private.
When making your decisions about each field or method, try to
think about whether code that is external to the Histogram
class would need to know about that field or method, or whether
that field or method could be treated as an internal detail that
only the Histogram class needs to know about. Try to make the
methods and fields private as much as possible, unless there is a
clear reason to make them public.
For example, take a look at the Runner class. This class needs
to be able to create histograms, add values to it, and print the
histogram. So these are things that need to be public. What other
parts of Histogram do external classes like Runner need access to?
What parts are instead internal details that they don't really need
to know/care about?
Hint: there are multiple fields and methods
that should be declared as private. Again, marking will fail unless
you choose the correct fields and methods to be private.
Task 3: Fill in the gaps
There are some methods that are incomplete. These are the same
gaps that were present in the exercise from last week, so if you
completed that one this task should be trivial.
A more OO Histogram (Required Exercise) The big limitation of the histogram we implemented last week is that it could on
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am