Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

B. A histogram is used to plot tabulated frequencies. Create a Histogram class t

ID: 3704135 • Letter: B

Question

B. A histogram is used to plot tabulated frequencies. Create a Histogram class that can be used to maintain and plot the frequencies of numbers that fall within a specified range. The Histogram class contain will an array of counters with equal intervals within the specified range (For example, see Test 1 and Test 2). All counters are first initialized to zero. When the number added to the Histogram falls within the range of a particular counter that counter is incremented by one. The Histogram class should contain the following methods: A constructor that accepts the number of counters and the maximum and minimum limits of the numbers (i.e., the range of numbers). .A second constructor that accepts the maximum and minimum limits of the numbers and initializes the Histogram with 10 counters for the specified range. An add(double x) method that increments the corresponding counter. . A reset) method that sets all counter frequencies to zero. .A plotFrequency method that plots the counter frequencies. A plotCumulative method that plots the cumulative frequencies. Write a RandomNumberTester class that uses the Histogram class to test the Random numbetr class provided in the java SDK. Test 1: Create 10 counters with the following limits Bin Counter 0 Counter 1 Counter 2 Counter 3 Counter 4 Counter 5 Counter 6 Counter 7 Limit 0

Explanation / Answer

Here the below java code is a basic logic to your core requirement, you can modify it according to specifications:

public class Histrogram {

   private int numCounter;

   private double[] limits;

   private int[] frequencyCounts;

   private int cumulativeCount;

   public Histrogram(int minLimit, int maxLimit, int numCounter) {

       this.numCounter = numCounter;

       limits = new double[numCounter+1];

       frequencyCounts = new int[numCounter];

       double interval = ((double)(maxLimit - minLimit))/numCounter;

       populateLimits(limits, interval, minLimit);

       reset();

   }

   public Histrogram(int minLimit, int maxLimit) {

       this(minLimit, maxLimit, 10);

   }   

   private void populateLimits(double[] limits, double interval, int minLimit) {

       limits[0] = minLimit;

       for(int i=1; i<limits.length; i++) {

           limits[i] = limits[i-1]+interval;

       }

   }   

   public void reset() {

       for(int i=0; i<frequencyCounts.length; i++) {

           frequencyCounts[i] = 0;

       }

   }   

   public void add(double x) {

       for(int i=1; i<limits.length; i++) {

           if(limits[i-1]<=x && limits[i]>x) {

               frequencyCounts[i-1]++;

               break;

           }

       }

   }

   public void plotFrequency() {

       //i assumed that you just need to print frequency counts here line by line, you can use formatting and graph plotting according to your use.

       for(int i=0; i<numCounter; i++) {

           System.out.println("Counter"+i+" "+frequencyCounts[0]);

       }

   }

   public void plotCumulative() {

       //i assumed that you just need to print cumulative count of frequency counts here line by line, you can use formatting and graph plotting according to your use.

       for(int i=0; i<numCounter; i++) {

           cumulativeCount = cumulativeCount + frequencyCounts[i];

           System.out.println("Counter"+i+" "+cumulativeCount);

       }

   }

}

public class RandomNumberTester {

   private Histrogram histrogram;

   public void testCase1() {

       histrogram = new Histrogram(0, 1);

       for (int i=0; i<1000; i++) {

           double randomNum = random(0, 1);

           histrogram.add(randomNum);

       }

       System.out.println("Counter Frequency");

       histrogram.plotFrequency();

       System.out.println("cumulative Frequency");

       histrogram.plotCumulative();

   }   

   public void testCase2() {

       //similarly you can create this test case for the given scenario

   }

   public double random(double min, double max) {

       double diff = max - min;

       return min + Math.random() * diff;

   }   

   public static void main(String[] args) {

       //then call all of your test case method from here as below.

       RandomNumberTester tester = new RandomNumberTester();

       tester.testCase1();

   }

}