class BarChart {

    private String title;
    private int[] frequencies;
    
    public BarChart(String title, int[] frequencies) {
        this.title = title;
        this.frequencies = frequencies;
    }
    
    public String toString() {
        String s = title + "\n\n";
        int axisWidth = (frequencies.length + "").length();
        for (int i = 0; i < frequencies.length; i++) {
            s += rightJustify(i, axisWidth) + " ";
            for (int j = 0; j < frequencies[i]; j++) {
                s+= "*";
            }
            s += "\n";
        }
        return s;
    }
    
    private static String rightJustify(int x, int width) {
        String s = "" + x;
        for (int i = s.length(); i < width; i++) {
            s = " " + s;
        }
        return s;
    }
}
