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

Java programming: write a program that creates a histogram that allows you to vi

ID: 3825181 • Letter: J

Question

Java programming:


write a program that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read an arbitrary number of integers from a text input file that are in the range 1 to 100 inclusive. then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 100 to 20, and so on. Print one asterisk for each value entered. 1-10 11-20 21-30 31-40 41-50 51-60 61-70 l******** 71-80 1-90 91-100

Explanation / Answer

Let this be our text file

1
2
3
4
5
6
14
12
13
16
26
24
54
35
36
45
46
56
58
69
69
79
79
91

Output:

1-10   ******
11-20   ****
21-30   **
31-40   **
41-50   **
51-60   ***
61-70   **
71-80   **
81-90  
91-100   *

Code:

package histogram;
import java.io.*;
import java.util.Scanner;

public class Histogram
{
public static void main(String[] args) throws IOException
{
//let a b c ----- j will store count of each range
int data;
int[] ar={0,0,0,0,0,0,0,0,0,0};
try
{
Scanner in = new Scanner(new File("/Users/hitansh/Desktop/input.txt"));
  
while(in.hasNextInt())
{
data=in.nextInt();

ar[(data-1)/10]++;

}
}
finally
{
}
int k;
//lets print the graph
System.out.print("1-10 ");
for(k=0;k<ar[0];k++)
System.out.print('*');
System.out.println();
  
System.out.print("11-20 ");
for(k=0;k<ar[1];k++)
System.out.print('*');
System.out.println();
  
System.out.print("21-30 ");
for(k=0;k<ar[2];k++)
System.out.print('*');
System.out.println();
  
System.out.print("31-40 ");
for(k=0;k<ar[3];k++)
System.out.print('*');
System.out.println();
  
System.out.print("41-50 ");
for(k=0;k<ar[4];k++)
System.out.print('*');
System.out.println();
  
System.out.print("51-60 ");
for(k=0;k<ar[5];k++)
System.out.print('*');
System.out.println();
  
System.out.print("61-70 ");
for(k=0;k<ar[6];k++)
System.out.print('*');
System.out.println();
  
System.out.print("71-80 ");
for(k=0;k<ar[7];k++)
System.out.print('*');
System.out.println();
  
System.out.print("81-90 ");
for(k=0;k<ar[8];k++)
System.out.print('*');
System.out.println();
  
System.out.print("91-100 ");
for(k=0;k<ar[9];k++)
System.out.print('*');
System.out.println();
}
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote