Reputation: 77
I need to print a histogram like so:
My Histogram
12.0 #### 2.3
12.5 ### 1.55
13.0 ######################################## 21.9
13.5 ########################### 13.33
14.0 ################################## 17.25
14.5 ########## 5.21
I have the histogram to scale. Overall, the Title will be first, and below the bottom row in position 40 or thereabouts will be the scale, i.e. a "nice" number near the maximum value. Each bar should be followed by it's length expressed.
Here is my code. I need help with getting the numbers on the right and left side of the histogram to print and then the title. I have the equation needed for the scale of numbers on the left but it won't print. Along with the title and data on the right.
import java.util.Scanner;
public class Array3
{
public static void main(String[] arg)
{
Histogram h = new Histogram();
System.out.println(h.readHistogram());
} // end main
} // end Array
class Histogram
{
private String title;
private double start, stop;
private double[] data;
public Histogram(String title, double start, double stop, double[] data, int numBars)
{
this.title = title;
this.start = start;
this.stop = stop;
this.data = new double[numBars];
for(int i = 0; i < numBars; ++i)
{
if(data.length > i )
{
this.data[i] = data[i];
} // end if
} // end for
} // end constructor Histogram
public Histogram(){}
public String toString()
{
String numOfHash = "";
int a;
double max = data[0];
double step1 = (stop-start)/(data.length-1);
for(int k = 0; k < data.length; ++k)
{
if(data[k] > max)
{
max = data[k];
}
}
for(int i = 0; i < data.length; ++i)
{
a = (int) (data[i] + .5);
double startNum = start + step1*i;
System.out.println(startNum);
for(int j = 0; j < a * 40/max ; ++j)
{
numOfHash = numOfHash + "#";
} // end for loop
numOfHash = numOfHash + "\n";
} // end for loop
return numOfHash;
} // end toString
public Histogram readHistogram()
{
int num = 0;
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a histogram.");
System.out.println("Title: ");
String title = stdin.nextLine();
System.out.println("Start value: ");
double start = stdin.nextDouble();
System.out.println("End Value: ");
double stop = stdin.nextDouble();
System.out.println("Data: ");
double[] data = new double[6];
for(int i = 0; i < 6; ++i)
{
data[i] = stdin.nextDouble();
num++;
}
return new Histogram(title, start, stop, data, num);
}
} // end Histogram
Upvotes: 0
Views: 945
Reputation: 51515
Your toString method needed a bit of work. First, you need to find the maximum value. Next, you can calculate the scaling factor. Now, you can draw the histogram.
Here's the code.
import java.util.Scanner;
public class Array3 {
public static void main(String[] arg) {
Histogram h = new Histogram();
System.out.println(h.readHistogram());
} // end main
} // end Array3
class Histogram {
private String title;
private double start, stop;
private double[] data;
public Histogram(String title, double start, double stop, double[] data,
int numBars) {
this.title = title;
this.start = start;
this.stop = stop;
this.data = new double[numBars];
for (int i = 0; i < numBars; ++i) {
if (data.length > i) {
this.data[i] = data[i];
} // end if
} // end for
} // end constructor Histogram
public Histogram() {
}
public String toString() {
String numOfHash = title + "\n";
double max = data[0];
for (int k = 1; k < data.length; ++k) {
max = Math.max(max, data[k]);
}
double scaleFactor = max / 40D;
double a = start;
for (int i = 0; i < data.length; ++i) {
numOfHash += String.format("%.1f", a) + " ";
int count = (int) Math.round(data[i] / scaleFactor);
for (int j = 0; j < count; ++j) {
numOfHash += "#";
} // end for loop
numOfHash += " " + String.format("%.2f", data[i]) + "\n";
a += 0.5D;
} // end for loop
return numOfHash;
} // end toString
public Histogram readHistogram() {
int num = 0;
Scanner stdin = new Scanner(System.in);
System.out.println("Enter a histogram.");
System.out.println("Title: ");
String title = stdin.nextLine();
System.out.println("Start value: ");
double start = stdin.nextDouble();
System.out.println("End Value: ");
double stop = stdin.nextDouble();
System.out.println("Data: ");
double[] data = new double[5];
for (int i = 0; i < 5; ++i) {
data[i] = stdin.nextDouble();
num++;
}
stdin.close();
return new Histogram(title, start, stop, data, num);
}
} // end Histogram
Upvotes: 1