public void print ( ) { // Get the longest bar's length. int maxCount = 0 ; for ( int i = 0 ; i < m_n ; ++ i ) { maxCount = Math . max ( maxCount , m_aCounters [ i ] ) ; } // Compute the scaling factor. final float factor = ( ( float ) MAX_BAR_SIZE ) / maxCount ; // Loop to print each bar. for ( int i = 0 ; i < m_n ; ++ i ) { final int b = m_aCounters [ i ] ; // Interval number. m_aAlignRight . print ( i , 2 ) ; m_aAlignRight . print ( b , 7 ) ; System . out . print ( ": " ) ; // Bar. final int length = Math . round ( factor * b ) ; for ( int j = 0 ; j < length ; ++ j ) System . out . print ( "*" ) ; System . out . println ( ) ; } }
Print the counter values as a horizontal bar chart . Scale the chart so that the longest bar is MAX_BAR_SIZE .