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
Before you have filled in the basic information for each of
these methods, the code will not compile. You need to at least do
that before it will run.
Existing Code:
/*
* Prints a histogram showing the distribution of values that are
added to it.
* It prints the histogram sideways, mainly because it's easier that
way.
*
* It is intended for percentage values and will ignore any values
that are
* less than 0 or greather than 100.
*
* This version uses static values and methods only, and is intended
as a
* simple introduction to organising code into methods, and
understanding
* the use of public vs private fields and methods.
*/
public class HistogramStatic {
/**
* 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 static int[] bins = new int[10];
/*
* Note: Feel free to change this main method to test
different
* parts of your code and check that it is behaving
correctly.
*
* The automatic marking will work regardless of what
you do to this method
* (as long as the code still compiles,
obvs.)
*/
public static void main(String[] args) {
//reset the histogram, and make sure
it looks empty
reset() ;
System.out.println("This should look
empty");
System.out.println(renderToString());
//add some values manually
add(5.0);
addAll(12.3, 18.8);
add(25.5);
addAll(31.23, 35.8, 37.9);
add(47.2);
addAll(50.3, 58.9, 55.0, 59.99);
add(60.0);
addAll(70.0, 71.0, 72.0, 73.0,
79.999);
add(89.999);
addAll(90.0, 90.01, 91.3, 91.3, 91.3,
91.3);
System.out.println("This should look
like a bisected christmas tree (?!?)");
System.out.println(renderToString());
//add some out-of-bounds values, and
make sure they are ignored
addAll(-20.3, -0.01, 100.3,
1000000.4);
System.out.println("This should not
look any different") ;
System.out.println(renderToString());
//reset the histogram, and make sure
it looks empty again
reset() ;
System.out.println("This should look
empty again") ;
System.out.println(renderToString());
//add a whole bunch of random
numbers
for (int i=0 ; i< 100 ; i++) {
double value =
Math.random() * 100 ;
add(value);
}
System.out.println("This will look
different every time!") ;
System.out.println(renderToString());
}
/**
* Returns true if the given value is out of
bounds
* (i.e. less than 0 or greather than 100) or false if
not.
*
* It needs:
* - a proper return type (maybe void)
* - a proper list of parameters (maybe empty)
* - code to actually do what it should
*/
public static returntype
isOutOfBounds(parametersIfAny) {
//Add whatever code is required here in
the method body
}
/**
* 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.
*
* This function has been completed for you and does
not require any changes
*/
public static int getBinIndex(double value) {
int index = (int) Math.floor(value /
10);
if (index < 0) index = 0 ;
if (index > 9) index = 9 ;
return index ;
}
/**
* Receives a percentage value (as a double) 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 corresponding bin should be
increased by 1.
*
* If the value is not within bounds, the method
should not do anything.
*
* It needs:
* - a proper return type (maybe void)
* - a proper list of parameters (maybe empty)
* - code to actually do what it should
*/
public static returntype add(parametersIfAny)
{
/*
* Add whatever code is required here in
the method body
*
* Hints:
* If you completed everything above
this method then you already have a method that
* checks whether a value is in bounds
or not, and another method that figures out
* which bin a value belongs to.
* Don't repeat, reuse!
*/
}
/**
* Receives multiple percentage values. Any values
that are within bounds are
* added to the appropriate histogram bin.
*
* Note: The values parameter here may look strange,
but it is just an array of doubles.
* Treat it like you would treat any other array. If
you want to know more, check out the
* optional material on varargs.
*
* It needs:
* - code to actually do what it should
*/
public static void addAll(double... values) {
/*
* Add whatever code is required here in
the method body
*
* Hints:
* If you got addAll working then you
already have a method that adds a single value.
* If you implemented it right, it
already checks whether the value is in bounds or not.
* Again, don't repeat, reuse!
*/
}
/**
* Clears the histogram, removing all values that have
been added to it.
*
* It needs:
* - a proper return type (maybe void)
* - a proper list of parameters (maybe empty)
* - code to actually do what it should
*/
public static returntype reset(parametersIfAny) {
//Add whatever code is required here
in the method body
}
/*
* 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 static 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.
*
* This function has been completed for you and does
not require any changes
*/
public static 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() < 10) {
label = " " + label
;
}
return label ;
}
}
Histogram Static (Required Exercise) Purpose This exercise is designed to help you understand how methods can make your
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am