How to Create Histogram Program in Java

Yashaswi Kothari Feb 02, 2024
How to Create Histogram Program in Java

Histograms allow us to maintain the count of different categories of values. We can also represent them graphically.

In this article, we will use Java to create a histogram that will store the sum of the dice thrown.

In order to check the values, we will use the if-else ladder. The if-else ladder is an efficient way of comparing one element with several values. We maintain the count of each sum in different integer variables. Each variable is incremented when the match occurs.

We ask the user the input the size of the histogram array. This array will represent the total number of times each dice will be rolled. We will then initialize the numbers that will occur as the sum of two dies thrown simultaneously, starting from 2 to 12.

The user enters the value for both dice, and we will compute their sum. The if-else ladder will compare the sum with different possibilities and increment every time a match occurs.

We will implement all this in the following program.

import java.io.*;
class Main {
  public static String toStars(int number) {
    StringBuilder temp = new StringBuilder();
    for (int i = 0; i < number; i++) {
      temp.append("*");
    }
    return temp.toString();
  }
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    System.out.print("Total rolls for each dice?");

    int n = Integer.parseInt(br.readLine());
    int[] rolls = new int[n];
    int d1;
    int d2;
    int two = 0;
    int three = 0;
    int four = 0;
    int five = 0;
    int six = 0;
    int seven = 0;
    int eight = 0;
    int nine = 0;
    int ten = 0;
    int eleven = 0;
    int twelve = 0;

    for (int roll = 0; roll < rolls.length; roll++) {
      System.out.println("First dice roll");
      d1 = Integer.parseInt(br.readLine());
      System.out.println("Second dice roll");
      d2 = Integer.parseInt(br.readLine());
      System.out.println(" The first dice rolled a " + d1 + " the second dice rolled a " + d2);

      int sum;
      sum = d1 + d2;

      if (sum == 2)
        two++;
      if (sum == 3)
        three++;
      if (sum == 4)
        four++;
      if (sum == 5)
        five++;
      if (sum == 6)
        six++;
      if (sum == 7)
        seven++;
      if (sum == 8)
        eight++;
      if (sum == 9)
        nine++;
      if (sum == 10)
        ten++;
      if (sum == 11)
        eleven++;
      if (sum == 12)
        twelve++;
    }
    System.out.println("Histogram of rolls:");
    System.out.println("2 occurred " + toStars(two) + " times");
    System.out.println("3 occurred " + toStars(three) + " times");
    System.out.println("4 occurred " + toStars(four) + " times");
    System.out.println("5 occurred " + toStars(five) + " times");
    System.out.println("6 occurred " + toStars(six) + " times");
    System.out.println("7 occurred " + toStars(seven) + " times");
    System.out.println("8 occurred " + toStars(eight) + " times");
    System.out.println("9 occurred " + toStars(nine) + " times");
    System.out.println("10 occurred " + toStars(ten) + " times");
    System.out.println("11 occurred " + toStars(eleven) + " times");
    System.out.println("12 occurred " + toStars(twelve) + " times");
  }
}

Output:

Total rolls for each dice?5

First dice roll

1

Second dice roll

2

 The first dice rolled a 1 the second dice rolled a 2

First dice roll

2

Second dice roll

1

 The first dice rolled a 2 the second dice rolled a 1

First dice roll

5

Second dice roll

4

 The first dice rolled a 5 the second dice rolled a 4

First dice roll

1

Second dice roll

1

 The first dice rolled a 1 the second dice rolled a 1

First dice roll

3

Second dice roll

1

 The first dice rolled a 3 the second dice rolled a 1

Histogram of rolls:

2 occurred * times

3 occurred ** times

4 occurred * times

5 occurred  times

6 occurred  times

7 occurred  times

8 occurred  times

9 occurred * times

10 occurred  times

11 occurred  times

12 occurred  times

Note that to display the final output, we create a separate function toStars() that converts the frequency for every possibility into the number of stars. This method is visually appealing and gives a good representation of a histogram.