Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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. Feel free to copy the code you used previously.

The code will not compile until you have replaced all of the placeholders and filled in the basic information about the incomplete methods. You need to at least do that before it will run.

This is the code given:

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

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. */ 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 ; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

java This ObjectOriented 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 ... 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

Fundamentals of Cost Accounting

Authors: William Lanen, Shannon Anderson, Michael Maher

3rd Edition

9780078025525, 9780077517359, 77517350, 978-0077398194

More Books

Students also viewed these Programming questions

Question

What is the difference between stereotypes and prejudice? (p. 351)

Answered: 1 week ago