Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please add Commenting (code documentation) on this Histogram, takes an integer n, and two integers left and right, and uses StdDraw to plot a histogram

Please add Commenting (code documentation) on this Histogram, takes an integer n, and two integers left and right, and uses StdDraw to plot a histogram of the count of the numbers in the standard input stream that fall in each of the n intervals defined by dividing (left, right) into n equal-sized intervals.

Note: The program is to read the values from an input file whose first line would contain n, left, and right values. The remaining lines would contain the sequence of integer values.A sample run would be as follows.>

more data.txt 4 40 80 52 41 72 61 71 60 50 52 61 77 41 61 70 79 41 67 60 50 61 76

Code:

import stdlib.StdDraw;

import stdlib.StdIn;

public class MakeHistogram {

public static void main(String[] args) {

int N = StdIn.readInt();

int left = StdIn.readInt();

int right = StdIn.readInt();

double[] samples = make_samples(left, right);

int[] intervals = fillSamplesInIntervals(samples, N, left, right);

draw_histogram(intervals, samples);

}

public static double[] make_samples(int left, int right) {

String allstr = "";

while(!StdIn.isEmpty()) {

String str = StdIn.readString();

double n = Double.parseDouble(str);

if (n >= left && n <= right) {

allstr = allstr + str + " ";

}

}

String[] str_array = allstr.split(" ");

int len = str_array.length;

double[] double_array = new double[len];

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

double_array[i] = Double.parseDouble(str_array[i]);

}

return double_array;

}

public static int[] fillSamplesInIntervals(double[] samples, int N, double left, double right) {

int[] result = new int[N];

int p;

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

p = findIntervalIndex(samples[i], N, left, right);

result[p] += 1;

}

return result;

}

public static int findIntervalIndex(double sample, int N, double left, double right) {

int k = 0;

double avg = (right - left) / N;

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

if (sample >= avg * i && sample < avg * (i + 1)) {

k = i;

break;

}

}

if (sample == right) {

k = N - 1;

}

return k;

}

public static void draw_histogram(int[] intervals, double[] samples) {

StdDraw.setPenRadius(.006);

StdDraw.line(0, 0, 1, 0);

StdDraw.line(0, 0, 0, 1);

int n = intervals.length;

int total = samples.length;

double interval_width = 1.0 / n;

double x, y;

double halfWidth = interval_width / 2;

StdDraw.setPenColor(StdDraw.GRAY);

StdDraw.setPenRadius();

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

x = i * interval_width + halfWidth;

y = (double)intervals[i] / total / 2;

StdDraw.rectangle(x, y, halfWidth, y);

}

}

}

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

Demystifying Databases A Hands On Guide For Database Management

Authors: Shiva Sukula

1st Edition

8170005345, 978-8170005346

More Books

Students also viewed these Databases questions