Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* * Prints a histogram showing the distribution of values that are added to it. * It prints the histogram sideways, mainly because it's easier

/* * 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

for (int x=0 ; x

rendered = rendered + " " ; }

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 ; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Statistics For Engineers And Scientists

Authors: William Navidi

4th Edition

73401331, 978-0073401331

More Books

Students also viewed these Programming questions