A more OO Histogram (Java)
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.
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?
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.
Provided Code Skeleton:
/**
* This Object-Oriented" version of the "Histogram"
class
* is intended to test student understanding of when to make
fields
* and methods static or not.
*/
public class HistogramOO {
/**
* This array stores the counts of values for each row
(aka "bin") in the histogram.
* * The first element stores the number of values
that are >= 0% and < 10%
* * The second element stores the number of values
that are >=10% and < 20%
* * etc, etc....
*/
public_or_private static_or_not int[] bins ;
public_or_private static_or_not final int
LABEL_WIDTH = 10;
public_or_private static_or_not HistogramOO()
{
this.bins = new int[10];
}
/**
* Receives a percentage value and updates the
histogram accordingly.
*
* If the value is within bounds (i.e. is less
than 0 or greater than 100),
* then the value of the correspoinding bin
should be increased by one.
*
* If the value is not within bounds, the method
should not do anything.
*
* @param value A value to be
inserted into the histogram
*/
public_or_private static_or_not void add(double
value)
{
//add code to this method
//you should just be able to copy this
from last week's histogram exercise
}
/**
* Receives multiple percentage values. Any
values that are within bounds are
* added to the appropriate histogram
bin.
*
* @param values An array of values
to be added to the histogram
*/
public_or_private static_or_not void addAll(double...
values) {
//add code to this method
//you should just be able to copy this
from last week's histogram exercise
}
/**
* Clears the histogram, removing all values that have
been added to it.
*/
public_or_private static_or_not void reset() {
//add code to this method
//you should just be able to copy this
from last week's histogram exercise
}
/**
* Returns true if the given value is out of
bounds
* (i.e. less than 0 or greather than 100) or false if
not.
*
* @param It needs:
* - a proper return type (maybe void)
* - a proper list of parameters (maybe empty)
* - code to actually do what it should
*/
public_or_private static_or_not boolean
isOutOfBounds(double value) {
//add code to this method
//you should just be able to copy this
from last week's histogram exercise
}
/**
* Returns the index of the bin that the given
percentage value belongs to.
*
* For example, it will return:
* - 0 when given a percentage value between
0(incl) and 10(excl),
* - 1 when given a percentage value between
10(incl) and 20(excl)
* - ...
*
* It assumes it will be given a value that is within
bounds. No guarantees are made
* for how it will behave with values that are out of
bounds.
*/
public_or_private static_or_not int
getBinIndex(double value) {
int index = (int) Math.floor(value /
10);
if (index < 0) index = 0 ;
if (index > 9) index = 9 ;
return index ;
}
/*
* Returns a string which, if printed to the console
or to the file, provides a rough graphical
* representation of the histogram.
*
* This function has been completed for you and does
not require any changes
*/
public_or_private static_or_not String
renderToString() {
String rendered = "";
for (int binIndex=0 ;
binIndex<bins.length ; binIndex++) {
rendered = rendered +
getLabel(binIndex) ;
for (int x=0 ;
x<bins[binIndex] ; x++) {
rendered =
rendered + "#" ;
}
rendered = rendered +
"\n" ;
}
return rendered ;
}
/*
* Returns a label for the given bin, which shows the
minimum and maximum values
* that the bin represents.
*/
public_or_private static_or_not String getLabel(int
binIndex) {
int minValue = binIndex*10 ;
int maxValue = minValue+10 ;
String label = minValue + "%-" +
maxValue + "%: " ;
//add padding so the labels line up
nicely
while (label.length() < LABEL_WIDTH)
{
label = " " + label
;
}
return label ;
}
}
A more OO Histogram (Java) You have three tasks for this exercise: Task 1: Decide what should or should not be declared
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
A more OO Histogram (Java) You have three tasks for this exercise: Task 1: Decide what should or should not be declared
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!