Histogram Static (Required Exercise) Purpose This exercise is designed to help you understand how methods can make your
Posted: Fri May 20, 2022 10:36 am
Histogram Static (Required Exercise)
Purpose
This exercise is designed to help you understand how methods can
make your code a lot more readable and easy to follow.
Background
Good code is easy to read and understand, since readable code it
easier to debug and maintain. One of the best ways to improve the
readability of code is to use methods well.
Ideally, each method should be as small as possible, and do only
one very clear job. The name of the method should make it clear
what that job is. This will make your intentions very clear to
anyone reading the code, they can look at the method name and know
exactly what the method is trying to achieve.
Sometimes the job that a method needs to do is pretty complex,
but that doesn't mean that the code within that method needs to be!
There is no reason why that poor method has to do the whole complex
job on its own. It can call other methods to help get the job
done.
Task
We've provided the skeleton of a class that can draw
histograms.
It's designed to take percentage values (like student grades)
and render a simple graph showing the distribution of these values.
Like so:
0%-10%: #### 10%-20%: ########## 20%-30%: ######### 30%-40%:
############### 40%-50%: ########## 50%-60%: ##### 60%-70%:
############# 70%-80%: ############ 80%-90%: ############ 90%-100%:
##########
Here the first row shows the number of students who scored
between 0% and 10% (uh-oh) while the last row shows the number of
students who scored between 90% and 100%. Each # represents one
student grade (or whatever types of values you are putting into the
histogram). We just use them as a lazy way to render the bars of
the histogram.
Your job is to complete the following methods:
add - for adding a single value to the histogram
addAll - for adding multiple values to the histogram
reset - for clearing all values from the histogram
isOutOfBounds - for checking whether a value should be excluded
from the histogram for being to small or too large
Purpose
This exercise is designed to help you understand how methods can
make your code a lot more readable and easy to follow.
Background
Good code is easy to read and understand, since readable code it
easier to debug and maintain. One of the best ways to improve the
readability of code is to use methods well.
Ideally, each method should be as small as possible, and do only
one very clear job. The name of the method should make it clear
what that job is. This will make your intentions very clear to
anyone reading the code, they can look at the method name and know
exactly what the method is trying to achieve.
Sometimes the job that a method needs to do is pretty complex,
but that doesn't mean that the code within that method needs to be!
There is no reason why that poor method has to do the whole complex
job on its own. It can call other methods to help get the job
done.
Task
We've provided the skeleton of a class that can draw
histograms.
It's designed to take percentage values (like student grades)
and render a simple graph showing the distribution of these values.
Like so:
0%-10%: #### 10%-20%: ########## 20%-30%: ######### 30%-40%:
############### 40%-50%: ########## 50%-60%: ##### 60%-70%:
############# 70%-80%: ############ 80%-90%: ############ 90%-100%:
##########
Here the first row shows the number of students who scored
between 0% and 10% (uh-oh) while the last row shows the number of
students who scored between 90% and 100%. Each # represents one
student grade (or whatever types of values you are putting into the
histogram). We just use them as a lazy way to render the bars of
the histogram.
Your job is to complete the following methods:
add - for adding a single value to the histogram
addAll - for adding multiple values to the histogram
reset - for clearing all values from the histogram
isOutOfBounds - for checking whether a value should be excluded
from the histogram for being to small or too large