Created
November 23, 2015 01:04
-
-
Save connlark/3537f2fed5e65e34e6a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** | |
* Created by Connor on 11/19/15. | |
*/ | |
public class Histogram { | |
private int [] values; | |
private int interval; | |
public Histogram(){ | |
values = new int[100]; | |
interval = 10; | |
} | |
public int getInterval(){ | |
return this.interval; | |
} | |
public void add(int x){ | |
values[x-1]++; | |
} | |
public void changeInterval(int newInterval){ | |
this.interval = newInterval; | |
} | |
public int countValues(int start, int end){ | |
int counter = 0; | |
for (int i = start-1; i < end; i++) { | |
counter += values[i]; | |
} | |
return counter; | |
} | |
public void print(){ | |
for (int i = 1; i <= 100 ; i+=interval) { | |
System.out.print(i + " - " + (i + interval-1)); | |
if (i == 1) | |
System.out.print(" |"); | |
else if (i+interval-1 == 100) | |
System.out.print(" |"); | |
else System.out.print(" |"); | |
for (int j = 0; j < countValues(i,i + interval-1); j++) { | |
System.out.print("*"); | |
} | |
System.out.println(); | |
} | |
} | |
public static void main(String [] args) | |
{ | |
Histogram histo = new Histogram(); | |
Random rand = new Random(); | |
for(int i = 1; i<=100; i++) | |
{ | |
histo.add(rand.nextInt(100)+1); //generating 100 random integers between 1 and 100. | |
//Adding them to the histogram as well | |
} | |
histo.print(); | |
System.out.println("\nChanging the interval size to 20 and re-printing...\n"); | |
histo.changeInterval(20); | |
histo.print(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment