The big limitation of the histogram we implemented last week is that it could only maintain one histogram at a time, due to the reliance on static variables. In this exercise, we will use object oriented principles to create a class that can be instantiated multiple times, to maintain and draw multiple histograms. You can see this in action in the main method of the Runner class, which reads through a file containing (fictitious) student grades, and plots seperate histograms for each subject as well as a histogram that combines all subjects. It would be impossible to manage these multiple histograms simultaneously with the static version of the Histogram from last week. 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. i 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? i 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.
8 9 1 /** 2 * This Object-oriented" version of the "Histogram" class 3 * is intended to test student understanding of when to make fields 4 + and methods static or not. 5 */ 6 public class Histogramoo { 7 /** * This array stores the counts of values for each row (aka "bin") in the histogram. 10 * * The first element stores the number of values that are >= 0% and < 10% 11 * * The second element stores the number of values that are >=10% and < 20% 12 * * etc, etc.... 13 */ 14 public_or_private static_or_not int[] bins ; 15 16 public_or_private static_or_not final int LABEL_WIDTH = 10; 17 18 public_or_private static_or_not Histogramoo () { 19 this.bins = new int[10]; 20 } 21 22 /** 23 * Receives a percentage value and updates the histogram accordingly. 24 25 * If the value is within bounds (i.e. is less than 0 or greater than 100), 26 * then the value of the correspoinding bin should be increased by one. 27 28 * If the value is not within bounds, the method should not do anything. 29 30 * @param value A value to be inserted into the histogram 31 / */ 32 public_or_private static_or_not void add(double value) 33 { 34 //add code to this method 35 //you should just be able to copy this from last week's histogram exercise 36 } * + *
37 38 39 /** * Receives multiple percentage values. Any values that are within bounds are + added to the appropriate histogram bin. 40 41 * 42 43 * @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 } 46 47 50 51 52 /** * 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 } 53 54 588944949499的m8858858的gaa8b666882222 56 57 /** * Returns true if the given value is out of bounds (i.e. less than 0 or greather than 100) or false if not. 59 + 60 61 - 63 64 65 * @param It needs: t - 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 } 67 69 70 71 /** 72 * Returns the index of the bin that the given percentage value belongs to. 73 *
* * For example, it will return: - when given a percentage value between (incl) and 10 (excl), a - 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 ; } 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 /* * Returns a string which, if printed to the console or to the file, provides a rough graphical * representation of the histogram. * 1 * 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 + "#" ; ; } 109 rendered = rendered + "\n"; 110 }
return rendered } 111 112 113 114 115 116 117 118 119 /* * 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) { 120 int minValue = binIndex+10 ; int maxValue = minValue+10 ; String label = minValue + "-" + maxValue + ":" ; 121 122 123 124 125 126 127 128 129 130 131 132 133 } //add padding so the labels line up nicely while (label.length() <LABEL_WIDTH) { label = " " + label ; } return label ; }
The big limitation of the histogram we implemented last week is that it could only maintain one histogram at a time, due
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
The big limitation of the histogram we implemented last week is that it could only maintain one histogram at a time, due
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!