/* Christopher Moretti cmoretti P01A/P06 SortAndDraw takes in integers on StdIn, prints them out in sorted order on StdOut, and plots a histogram of them on StdDraw. */ public class SortAndDraw { public static void main(String[] args) { // the allowable range of integers int NUM_BINS = 100; // offset to center of square: half side-length. double OFFSET = .5; // array to hold counts for each possible int 0-99 int [] arr = new int[NUM_BINS]; // maximum count. int max = 0; // calculate count of each integer // and maximum count while(!StdIn.isEmpty()) { int i = StdIn.readInt(); arr[i]++; if(arr[i] > max) max = arr[i]; } // Task 1: print an integer for // each integer from the input sequence for(int j = 0; j < NUM_BINS; j++) { for(int k = 0; k < arr[j]; k++) { StdOut.print(j + " "); } } StdOut.println(); /* NB: The body of the loop below * (line 64) could have been included * in the body of the loop above, with * the setup (lines 55-58) above the * loop. However most students will * have structured it using 2 loops * because of the separate tasks in * the specifications. */ // set up drawing window StdDraw.setXscale(0, NUM_BINS); StdDraw.line(0, 0, NUM_BINS, 0); StdDraw.setYscale(0, max + 1); // Task 2: draw a box for // each integer from the input sequence for(int j = 0; j < NUM_BINS; j++) { for(int k = 0; k < arr[j]; k++) { StdDraw.square(j + OFFSET, k + OFFSET, OFFSET); } } } }