Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Recall that a histogram is a visual representation of a distribution of discrete data. For example, the data set {3,2,1,2,3,0,1,5,3} over the range [0,5] will
Recall that a histogram is a visual representation of a distribution of discrete data. For example, the data set {3,2,1,2,3,0,1,5,3} over the range [0,5] will have the following histogram: 0:1:2:3:4:5: Figure 1: Example Histogram Your histogram will be stored as an array. Remember, an array element has essentially two components: its index, the order in which it appears in the array (starting at 0 ), and its value, the mutable piece of data that corresponds to each index. For the histogram, notice that the value of each array element is the frequency of the corresponding number in the data set. When programming it is important to break your problems down into small pieces. For this section, you will create a Histogram class with a way to store the information in a histogram as follows: - Add a constructor public Histogram(int lowerbound, int upperbound) which will initialize your histogram by setting the range. This constructor is also where you should create the class member array that keeps track of the frequency of each number between lowerbound and upperbound inclusive. - Add a method public boolean add(int i) - If i is between lowerbound and upperbound inclusive, then add i to the histogram and return true. Otherwise, return false. Hint: If you are trying to increase the frequency of a number i in the histogram array, then you will need to subtract the number i by the lower bound to access the correct array index. For example, if lowerbound =5, upperbound =10, and i=6, then to increase the frequency of i=6 in our histogram array, we would need to access the array at index [i1 owerbound ]=[1]. - Add a method public String toString() - This will return a String formatted as in Figure 1. That means you are not printing anything in this method. Make sure your data points are in order from lowerbound to upperbound. You can use the character to add newlines. - Create the main method. Inside, implement some tests on an instance of Histogram to confirm that everything is working correctly. For instance, the following code would create the example histogram as seen in figure 1 : public static void main(String args []){ Histogram histo = new Histogram(0,5); histo.add (3); histo.add(2); histo.add(1); histo.add (2); histo.add(3); histo.add (0); histo.add(1); histo.add(5); histo.add(3); System.out.println(histo); \} Hint: In the constructor if the upperbound is smaller than the lowerbound, simply swap the bounds. Also, your data range should not include negative numbers or fractions. Milestone 3: Show a TA the Histogram class code and the tests you wrote to ensure the histogram is working correctly. This should include (at a minimum) printing a histogram after adding 10 numbers to it
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started